diff --git a/js/data.js b/js/data.js new file mode 100644 index 0000000..4deced3 --- /dev/null +++ b/js/data.js @@ -0,0 +1,59 @@ +export function loadFromJSON(json) { + const parsed = JSON.parse(json); + return { + employees: parsed.employees ?? [], + calendar: parsed.calendar ?? { holidays: [], companyClosures: [] }, + history: parsed.history ?? [] + }; +} + +export function saveToJSON(state) { + return JSON.stringify({ + employees: state.employees, + calendar: state.calendar, + history: state.history + }, null, 2); +} + +export function createEmployee(name) { + return { + id: crypto.randomUUID(), + name: name.trim(), + constraints: { + neverDays: [], + lowPriorityDays: [], + onlyDays: [], + maxPerMonth: null, + minGapDays: 0, + vacations: [] + } + }; +} + +export function removeEmployee(state, id) { + state.employees = state.employees.filter(e => e.id !== id); +} + +export function toggleHoliday(state, isoDate) { + const arr = state.calendar.holidays; + const idx = arr.indexOf(isoDate); + if (idx >= 0) arr.splice(idx, 1); else arr.push(isoDate); +} + +export function toggleClosure(state, isoDate) { + const arr = state.calendar.companyClosures; + const idx = arr.indexOf(isoDate); + if (idx >= 0) arr.splice(idx, 1); else arr.push(isoDate); +} + +// Adds entries to history, replacing any existing entries for the same dates +export function addHistoryEntries(state, entries) { + const dates = new Set(entries.map(e => e.date)); + state.history = state.history.filter(h => !dates.has(h.date)); + state.history.push(...entries); + state.history.sort((a, b) => a.date.localeCompare(b.date)); +} + +export function removeHistoryEntry(state, isoDate) { + state.history = state.history.filter(h => h.date !== isoDate); +}