10.23 zsola hibák + Deckek listázása megoldva

This commit is contained in:
2025-10-23 20:18:52 +02:00
parent 387ebbc64d
commit b73d1528c4
4 changed files with 244 additions and 47 deletions
@@ -11,15 +11,15 @@ import { createDeck } from '../../api/deckApi'
import { notifySuccess, notifyError, notifyWarning } from "../../components/Toastify/toastifyServices"
export default function DeckCreator() {
const { deckId } = useParams() // URL-ből deck ID (új deck esetén undefined)
const { deckId } = useParams()
const navigate = useNavigate()
// Deck alapadatok
const [deck, setDeck] = useState({
id: null,
name: "Új Pakli",
type: "QUESTION", // QUESTION, LUCK, JOKER - backend formátum
privacy: "private", // private, public
type: "QUESTION",
privacy: "private",
description: "",
cards: []
})
@@ -45,25 +45,38 @@ export default function DeckCreator() {
}
}, [deckId])
const loadDeck = async (id) => {
// Mock adatok később backendből jön majd
const mockDeck = {
id,
name: "Demo Deck",
type: "QUESTION",
privacy: "public",
description: "Ez egy teszt deck",
cards: []
}
setDeck(mockDeck)
}
const handleDeckUpdate = (updates) => {
setDeck(prev => ({ ...prev, ...updates }))
}
const handleSaveDeck = async () => {
try {
// Konvertálás: Frontend string -> Backend enum number
// Típus konverzió backendhez
const typeMapping = {
'LUCK': 0,
'JOKER': 1,
'QUESTION': 2
}
const payload = {
name: deck.name,
type: typeMapping[deck.type] ?? 2, // Alapértelmezett: QUESTION
name: deck.name?.trim() || "Névtelen pakli",
type: typeMapping[deck.type] ?? 2,
ctype: deck.privacy === 'public' ? 'PUBLIC' : 'PRIVATE',
description: deck.description || '',
cards: deck.cards
description: deck.description || "",
cards: deck.cards || []
}
const saved = await createDeck(payload)
@@ -75,16 +88,14 @@ export default function DeckCreator() {
}))
console.log('Deck saved (backend):', saved)
notifySuccess('Deck sikeresen mentve!')
notifySuccess('Pakli sikeresen elmentve!')
} catch (error) {
console.error('Mentési hiba:', error)
notifyError('Hiba történt a mentés során: ' + (error?.response?.data?.error || error.message || String(error)))
notifyError('Hiba történt a mentés során: ' + (error?.response?.data?.error || error.message))
}
}
// 🔧 Itt korábban volt confirm(), de most eltávolítottuk
const handleBack = () => {
// Egyszerű visszalépés — ha akarsz, később adhatunk hozzá saját modalt
navigate("/decks")
}
@@ -100,33 +111,34 @@ export default function DeckCreator() {
setNewCardType(null)
}
// 💡 Demo verzió: beállítások szekció kihagyva
const handleSaveCard = (cardData) => {
try {
// Biztosítjuk az alapértelmezett consequence értékeket
if (cardData.section === "settings") {
console.log("Beállítások szekció kihagyva (demo verzió)")
return
}
const defaultConsequence = { type: 0, value: 1 }
const defaultWrongConsequence = { type: 1, value: 1 }
const updatedCard = {
...cardData,
id: isCreatingCard ? Date.now() : cardData.id,
consequence: cardData.consequence || defaultConsequence,
// wrongConsequence csak QUESTION és JOKER típusoknál
...(cardData.type === 'QUESTION' || cardData.type === 'JOKER'
...(cardData.type === 'QUESTION' || cardData.type === 'JOKER' || cardData.type === 'PAIRING'
? { wrongConsequence: cardData.wrongConsequence || defaultWrongConsequence }
: {}
)
}
let wasInvalidCardDeleted = false
setDeck(prev => {
// Ellenőrizzük, vannak-e nem megfelelő típusú kártyák
const invalidCards = prev.cards.filter(card => card.type !== prev.type)
// Ha új kártyát mentünk megfelelő típussal és vannak nem megfelelők
if (isCreatingCard && cardData.type === prev.type && invalidCards.length > 0) {
wasInvalidCardDeleted = true
return {
...prev,
cards: [
@@ -135,11 +147,10 @@ export default function DeckCreator() {
]
}
}
// Alap mentési logika
return {
...prev,
cards: isCreatingCard
cards: isCreatingCard
? [...prev.cards, updatedCard]
: prev.cards.map(card => card.id === updatedCard.id ? updatedCard : card)
}
@@ -148,8 +159,7 @@ export default function DeckCreator() {
setSelectedCard(updatedCard)
setIsCreatingCard(false)
setNewCardType(null)
// Csak egy értesítés
if (wasInvalidCardDeleted) {
const invalidCount = deck.cards.filter(card => card.type !== deck.type).length
notifyWarning(`Kártya mentve! ${invalidCount} db nem megfelelő típusú kártya törlésre került.`)
@@ -162,8 +172,11 @@ export default function DeckCreator() {
}
}
// 🔧 Itt is confirm() volt — most a CardsList popupja kezeli a megerősítést
// 💬 Felugró ablak törlés előtt
const handleDeleteCard = (cardId) => {
const confirmDelete = window.confirm("Biztosan törölni szeretnéd ezt a kártyát?")
if (!confirmDelete) return
setDeck(prev => ({
...prev,
cards: prev.cards.filter(card => card.id !== cardId)
@@ -172,6 +185,8 @@ export default function DeckCreator() {
if (selectedCard?.id === cardId) {
setSelectedCard(null)
}
notifySuccess("Kártya törölve a pakliból!")
}
return (