szerkesztes jog megoldva+ Frontend #83
@@ -1,4 +1,4 @@
|
|||||||
import React, { useEffect } from "react"
|
import React, { useEffect, useState } from "react"
|
||||||
import { useNavigate } from "react-router-dom"
|
import { useNavigate } from "react-router-dom"
|
||||||
import {
|
import {
|
||||||
FaUser,
|
FaUser,
|
||||||
@@ -11,9 +11,11 @@ import {
|
|||||||
FaTimes,
|
FaTimes,
|
||||||
FaEdit
|
FaEdit
|
||||||
} from "react-icons/fa"
|
} from "react-icons/fa"
|
||||||
|
import { getUserProfile } from "../../api/userApi"
|
||||||
|
|
||||||
export default function DeckInfoPopUp({ deck, onClose }) {
|
export default function DeckInfoPopUp({ deck, onClose }) {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
const [currentUser, setCurrentUser] = useState(null)
|
||||||
|
|
||||||
if (!deck) return null
|
if (!deck) return null
|
||||||
|
|
||||||
@@ -32,6 +34,25 @@ export default function DeckInfoPopUp({ deck, onClose }) {
|
|||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
// Load current user to decide if Edit button should be shown
|
||||||
|
useEffect(() => {
|
||||||
|
let mounted = true
|
||||||
|
const loadUser = async () => {
|
||||||
|
try {
|
||||||
|
const data = await getUserProfile()
|
||||||
|
console.log('👤 Loaded current user:', data)
|
||||||
|
if (mounted) setCurrentUser(data)
|
||||||
|
} catch (e) {
|
||||||
|
// silently ignore - edit button will be hidden for anonymous
|
||||||
|
console.warn('Could not fetch current user profile for DeckInfoPopUp:', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadUser()
|
||||||
|
|
||||||
|
return () => { mounted = false }
|
||||||
|
}, [])
|
||||||
|
|
||||||
// Backend enum mapping
|
// Backend enum mapping
|
||||||
const deckTypeMapping = {
|
const deckTypeMapping = {
|
||||||
0: { label: "Szerencse", color: "var(--color-luck)" }, // LUCK
|
0: { label: "Szerencse", color: "var(--color-luck)" }, // LUCK
|
||||||
@@ -126,6 +147,50 @@ export default function DeckInfoPopUp({ deck, onClose }) {
|
|||||||
onClose()
|
onClose()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determine whether the current user can edit this deck
|
||||||
|
// Option 1: Use backend's 'editable' flag if available (ShortDeckDto)
|
||||||
|
// Option 2: Check userid field (DetailDeckDto) or compare user names
|
||||||
|
|
||||||
|
// Check if user is admin (state === 5)
|
||||||
|
const isAdmin = currentUser ? Number(currentUser.state) === 5 : false
|
||||||
|
|
||||||
|
// Check if deck is editable (backend provides this in ShortDeckDto)
|
||||||
|
const backendEditableFlag = rawData.editable === true
|
||||||
|
|
||||||
|
// Fallback: Check if user is the owner by userid (DetailDeckDto) or username
|
||||||
|
const deckOwnerId = rawData.userid // Only available in DetailDeckDto
|
||||||
|
const deckCreatorName = rawData.creator // Available in ShortDeckDto (username)
|
||||||
|
|
||||||
|
const isOwnerById = currentUser && deckOwnerId
|
||||||
|
? (String(currentUser.id) === String(deckOwnerId))
|
||||||
|
: false
|
||||||
|
|
||||||
|
const isOwnerByName = currentUser && deckCreatorName
|
||||||
|
? (currentUser.username === deckCreatorName)
|
||||||
|
: false
|
||||||
|
|
||||||
|
// User can edit if:
|
||||||
|
// 1. Backend says it's editable (ShortDeckDto has editable flag)
|
||||||
|
// 2. User is the owner (by ID or username)
|
||||||
|
// 3. User is an admin (state === 5)
|
||||||
|
const canEdit = backendEditableFlag || isOwnerById || isOwnerByName || isAdmin
|
||||||
|
|
||||||
|
// Debug: Check permission logic
|
||||||
|
console.log('🔍 Permission Check:', {
|
||||||
|
'Has currentUser?': !!currentUser,
|
||||||
|
'currentUser.id': currentUser?.id,
|
||||||
|
'currentUser.username': currentUser?.username,
|
||||||
|
'currentUser.state': currentUser?.state,
|
||||||
|
'deckOwnerId (userid)': deckOwnerId,
|
||||||
|
'deckCreatorName': deckCreatorName,
|
||||||
|
'backendEditableFlag': backendEditableFlag,
|
||||||
|
'isOwnerById': isOwnerById,
|
||||||
|
'isOwnerByName': isOwnerByName,
|
||||||
|
'isAdmin': isAdmin,
|
||||||
|
'canEdit': canEdit,
|
||||||
|
'rawData': rawData
|
||||||
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="fixed inset-0 bg-black/60 flex items-center justify-center z-50 backdrop-blur-sm">
|
<div className="fixed inset-0 bg-black/60 flex items-center justify-center z-50 backdrop-blur-sm">
|
||||||
<div
|
<div
|
||||||
@@ -254,7 +319,7 @@ export default function DeckInfoPopUp({ deck, onClose }) {
|
|||||||
|
|
||||||
{/* Action buttons */}
|
{/* Action buttons */}
|
||||||
<div className="mt-5 pt-4 border-t border-[color:var(--color-surface-selected)]">
|
<div className="mt-5 pt-4 border-t border-[color:var(--color-surface-selected)]">
|
||||||
<div className="grid grid-cols-2 gap-3">
|
<div className={`grid gap-3 ${canEdit ? 'grid-cols-2' : 'grid-cols-1'}`}>
|
||||||
{/* Open button */}
|
{/* Open button */}
|
||||||
<button
|
<button
|
||||||
className="px-4 py-2.5 rounded-xl font-semibold text-[color:var(--color-text-inverse)] transition-all duration-200 hover:scale-105 shadow-lg bg-[color:var(--color-success)] hover:bg-[color:var(--color-success)]/80"
|
className="px-4 py-2.5 rounded-xl font-semibold text-[color:var(--color-text-inverse)] transition-all duration-200 hover:scale-105 shadow-lg bg-[color:var(--color-success)] hover:bg-[color:var(--color-success)]/80"
|
||||||
@@ -263,7 +328,8 @@ export default function DeckInfoPopUp({ deck, onClose }) {
|
|||||||
Megnyitás
|
Megnyitás
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
{/* Edit button */}
|
{/* Edit button - only visible to owner or admin (state === 5) */}
|
||||||
|
{canEdit && (
|
||||||
<button
|
<button
|
||||||
className="px-4 py-2.5 rounded-xl font-semibold text-[color:var(--color-text)] border border-[color:var(--color-surface-selected)] transition-all duration-200 hover:scale-105 hover:bg-[color:var(--color-surface-selected)] flex items-center justify-center gap-2"
|
className="px-4 py-2.5 rounded-xl font-semibold text-[color:var(--color-text)] border border-[color:var(--color-surface-selected)] transition-all duration-200 hover:scale-105 hover:bg-[color:var(--color-surface-selected)] flex items-center justify-center gap-2"
|
||||||
onClick={handleEditDeck}
|
onClick={handleEditDeck}
|
||||||
@@ -271,6 +337,7 @@ export default function DeckInfoPopUp({ deck, onClose }) {
|
|||||||
<FaEdit className="text-sm" />
|
<FaEdit className="text-sm" />
|
||||||
Szerkesztés
|
Szerkesztés
|
||||||
</button>
|
</button>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user