From 2ec7c63119f726d4ab82a17cb5a5d5e9311afab5 Mon Sep 17 00:00:00 2001 From: nessi Date: Tue, 3 Feb 2026 15:13:21 +0100 Subject: [PATCH] Refactor tag handling and add chip localStorage support Refactored tag handling logic by introducing helper functions to improve clarity and maintainability. Added localStorage support for storing and retrieving chip values associated with entries, ensuring smoother transitions and proper state management across sessions. Simplified backend interactions for the "s" tag and improved display logic for tags with chips. --- frontend/src/App.jsx | 63 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index fc6645c..86f06f7 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -23,15 +23,45 @@ function parseTag(tag) { return { base: tag, chip: null }; } -function nextBaseTag(tag) { - const { base } = parseTag(tag); - if (!base) return "i"; - if (base === "i") return "m"; - if (base === "m") return "s"; // hier öffnen wir dann das Popup - // wenn s (egal ob s oder s.XY), dann zurück auf leer +function baseTag(tag) { + // Backend: null | "i" | "m" | "s" + if (!tag) return null; + if (tag === "i" || tag === "m" || tag === "s") return tag; return null; } +function nextBaseTag(tag) { + const b = baseTag(tag); + if (!b) return "i"; + if (b === "i") return "m"; + if (b === "m") return "s"; + return null; // s -> leer +} + +function chipStorageKey(gameId, entryId) { + return `chip:${gameId}:${entryId}`; +} + +function getChipLS(gameId, entryId) { + try { + return localStorage.getItem(chipStorageKey(gameId, entryId)); + } catch { + return null; + } +} + +function setChipLS(gameId, entryId, chip) { + try { + localStorage.setItem(chipStorageKey(gameId, entryId), chip); + } catch {} +} + +function clearChipLS(gameId, entryId) { + try { + localStorage.removeItem(chipStorageKey(gameId, entryId)); + } catch {} +} + function AdminPanel() { const [users, setUsers] = useState([]); @@ -350,14 +380,18 @@ export default function App() { const toggleTag = async (entry) => { const next = nextBaseTag(entry.note_tag); - // Wenn wir bei "s" angekommen sind -> Popup öffnen statt sofort setzen + // Wenn wir bei "s" angekommen sind -> Popup öffnen if (next === "s") { setChipPickEntry(entry); setChipPickOpen(true); return; } - // normal setzen (— / i / m) + // Wenn wir von "s" weg gehen -> Chip local löschen + if (baseTag(entry.note_tag) === "s" && next === null) { + clearChipLS(gameId, entry.entry_id); + } + await api(`/games/${gameId}/sheet/${entry.entry_id}`, { method: "PATCH", body: JSON.stringify({ note_tag: next }), @@ -374,9 +408,13 @@ export default function App() { const chooseChip = async (chip) => { if (!chipPickEntry) return; + // Chip lokal speichern + setChipLS(gameId, chipPickEntry.entry_id, chip); + + // Backend nur "s" setzen await api(`/games/${gameId}/sheet/${chipPickEntry.entry_id}`, { method: "PATCH", - body: JSON.stringify({ note_tag: `s.${chip}` }), + body: JSON.stringify({ note_tag: "s" }), }); closeChipPick(); @@ -700,7 +738,12 @@ export default function App() { );