feat: auto-persist state to localStorage on every change

This commit is contained in:
Ferdinand
2026-04-08 14:17:24 +02:00
parent 6ecf3e4409
commit dca82fe381
4 changed files with 47 additions and 7 deletions

View File

@@ -2,6 +2,8 @@ import { renderEmployees } from './employees.js';
import { renderCalendar } from './calendar.js';
import { renderDataTab } from './history.js';
const STORAGE_KEY = 'morning-planner-state';
// Shared mutable state — imported by all other modules
export const state = {
employees: [],
@@ -9,6 +11,32 @@ export const state = {
history: []
};
// 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 = parsed.employees;
if (parsed.calendar) state.calendar = parsed.calendar;
if (Array.isArray(parsed.history)) state.history = parsed.history;
}
} catch {
// Corrupt storage — start fresh
}
// Call after any state mutation to persist automatically
export function saveState() {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify({
employees: state.employees,
calendar: state.calendar,
history: state.history
}));
} catch {
// Storage full or unavailable — silently ignore
}
}
// Tab switching
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.addEventListener('click', () => {