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.
This commit is contained in:
@@ -23,15 +23,45 @@ function parseTag(tag) {
|
|||||||
return { base: tag, chip: null };
|
return { base: tag, chip: null };
|
||||||
}
|
}
|
||||||
|
|
||||||
function nextBaseTag(tag) {
|
function baseTag(tag) {
|
||||||
const { base } = parseTag(tag);
|
// Backend: null | "i" | "m" | "s"
|
||||||
if (!base) return "i";
|
if (!tag) return null;
|
||||||
if (base === "i") return "m";
|
if (tag === "i" || tag === "m" || tag === "s") return tag;
|
||||||
if (base === "m") return "s"; // hier öffnen wir dann das Popup
|
|
||||||
// wenn s (egal ob s oder s.XY), dann zurück auf leer
|
|
||||||
return null;
|
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() {
|
function AdminPanel() {
|
||||||
const [users, setUsers] = useState([]);
|
const [users, setUsers] = useState([]);
|
||||||
|
|
||||||
@@ -350,14 +380,18 @@ export default function App() {
|
|||||||
const toggleTag = async (entry) => {
|
const toggleTag = async (entry) => {
|
||||||
const next = nextBaseTag(entry.note_tag);
|
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") {
|
if (next === "s") {
|
||||||
setChipPickEntry(entry);
|
setChipPickEntry(entry);
|
||||||
setChipPickOpen(true);
|
setChipPickOpen(true);
|
||||||
return;
|
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}`, {
|
await api(`/games/${gameId}/sheet/${entry.entry_id}`, {
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
body: JSON.stringify({ note_tag: next }),
|
body: JSON.stringify({ note_tag: next }),
|
||||||
@@ -374,9 +408,13 @@ export default function App() {
|
|||||||
const chooseChip = async (chip) => {
|
const chooseChip = async (chip) => {
|
||||||
if (!chipPickEntry) return;
|
if (!chipPickEntry) return;
|
||||||
|
|
||||||
|
// Chip lokal speichern
|
||||||
|
setChipLS(gameId, chipPickEntry.entry_id, chip);
|
||||||
|
|
||||||
|
// Backend nur "s" setzen
|
||||||
await api(`/games/${gameId}/sheet/${chipPickEntry.entry_id}`, {
|
await api(`/games/${gameId}/sheet/${chipPickEntry.entry_id}`, {
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
body: JSON.stringify({ note_tag: `s.${chip}` }),
|
body: JSON.stringify({ note_tag: "s" }),
|
||||||
});
|
});
|
||||||
|
|
||||||
closeChipPick();
|
closeChipPick();
|
||||||
@@ -700,7 +738,12 @@ export default function App() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button onClick={() => toggleTag(e)} style={styles.tagBtn} title="i → m → s → leer">
|
<button onClick={() => toggleTag(e)} style={styles.tagBtn} title="i → m → s → leer">
|
||||||
{e.note_tag || "—"}
|
{(() => {
|
||||||
|
if (!e.note_tag) return "—";
|
||||||
|
if (e.note_tag !== "s") return e.note_tag;
|
||||||
|
const chip = getChipLS(gameId, e.entry_id);
|
||||||
|
return chip ? `s.${chip}` : "s";
|
||||||
|
})()}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user