From 0a1f30553a9a5bf4c4585afa4623b581571ff8aa Mon Sep 17 00:00:00 2001 From: Ferdinand Date: Wed, 8 Apr 2026 10:42:53 +0200 Subject: [PATCH] feat: data module with JSON load/save and employee CRUD --- js/data.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 js/data.js 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); +}