Files
Meeting-Mixer/js/app.js
T
ferdi2goandClaude Opus 4.8 7b53618921 fix(state): surface localStorage save failures instead of silent loss (#4)
saveState now logs a console.warn on failure and alerts the user once per
session (storage full/unavailable) so they can export their data and avoid
silent data loss.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 12:21:23 +00:00

69 lines
2.4 KiB
JavaScript

import { renderEmployees } from './employees.js';
import { renderCalendar } from './calendar.js';
import { renderDataTab } from './history.js';
import { normalizeEmployees } from './data.js';
const STORAGE_KEY = 'morning-planner-state';
// Shared mutable state — imported by all other modules
export const state = {
employees: [],
calendar: { holidays: [], companyClosures: [] },
history: [],
settings: { moderatorsPerDay: 1 }
};
// Load persisted state from localStorage on startup
try {
const saved = localStorage.getItem(STORAGE_KEY);
if (saved) {
const parsed = JSON.parse(saved);
if (Array.isArray(parsed.employees)) state.employees = normalizeEmployees(parsed.employees);
if (parsed.calendar) state.calendar = parsed.calendar;
if (Array.isArray(parsed.history)) state.history = parsed.history;
if (parsed.settings && Number.isInteger(parsed.settings.moderatorsPerDay)
&& parsed.settings.moderatorsPerDay >= 1 && parsed.settings.moderatorsPerDay <= 5) {
state.settings = { moderatorsPerDay: parsed.settings.moderatorsPerDay };
}
}
} catch {
// Corrupt storage — start fresh
}
// Call after any state mutation to persist automatically
let storageWarned = false;
export function saveState() {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify({
employees: state.employees,
calendar: state.calendar,
history: state.history,
settings: state.settings
}));
} catch (e) {
// Storage full or unavailable — warn once so the user can react (export data)
console.warn('Speichern in localStorage fehlgeschlagen:', e);
if (!storageWarned) {
storageWarned = true;
alert('Achtung: Änderungen konnten nicht gespeichert werden ' +
'(Speicher voll oder nicht verfügbar). Bitte exportiere deine Daten ' +
'als JSON, um Datenverlust zu vermeiden.');
}
}
}
// Tab switching
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.addEventListener('click', () => {
const tab = btn.dataset.tab;
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(s => s.classList.add('hidden'));
btn.classList.add('active');
document.getElementById('tab-' + tab).classList.remove('hidden');
if (tab === 'planning') renderCalendar();
if (tab === 'data') renderDataTab();
});
});
renderEmployees();