Files
r701/oxibadge/src/lib/components/settings/logic.ts
T
nicolabelluti 25127bee96
Check, format and test / build (push) Successful in 10m27s
feat(oxibadge): cache employee names locally and let readers be a list
Employee names are now cached locally (employees.toml, keyed by reader
index then employee id) instead of being re-fetched from the time
clock on every month: load_employees only asks the device for ids not
already in the cache. When a month has employees the cache doesn't
know yet, a new screen (NewEmployees.svelte) lets the user name them
before continuing to the review screen; the chosen names are local to
Oxibadge only, never written back to the device. The same cache can be
managed afterwards from a drawer reachable from the time clock card in
Settings ("Gestisci dipendenti"), reusing the rename-in-row pattern
from the original mockup.

Settings.readers is a list again (was flattened to a single `reader`
object) so multiple time clocks can be supported later; only the first
one is used today.

Also:
- buildEmployees() now derives the employee list from the month's
  records instead of from every cached name ever seen, so the review
  screen no longer shows employees unrelated to the selected month.
- Renaming an employee from the review screen's pencil button now
  actually persists to the cache (the callback existed but was never
  wired up).
- Translated to English a number of Rust/TS/Svelte comments that had
  been written in Italian; only the i18n locale files should contain
  Italian.
- Minor dedup: extracted PillButton.svelte (was duplicated inline in
  ReaderCard), promoted the repeated .mono CSS rule to app.css, moved
  initials() out of the review feature into $lib/text.ts.

Assisted-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-08 20:04:37 +02:00

43 lines
1.7 KiB
TypeScript

import { parseT } from '$lib/time';
import type { Shifts } from '$lib/shifts';
export type { Shifts };
export const IPV4 = /^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;
/** The 4 time fields of `Shifts` — everything but `round_step_min`, which isn't a time. */
export type ShiftTimeKey = 'in1' | 'out1' | 'in2' | 'out2';
export const SHIFT_ROWS: { titleKey: string; a: ShiftTimeKey; b: ShiftTimeKey }[] = [
{ titleKey: 'settings.shift_1', a: 'in1', b: 'out1' },
{ titleKey: 'settings.shift_2', a: 'in2', b: 'out2' }
];
export interface ShiftCheck {
minutes: Record<ShiftTimeKey, number> | null;
bad: Partial<Record<ShiftTimeKey, true>>;
errorKey: string | null;
}
export function checkShifts(shifts: Shifts): ShiftCheck {
const t: Record<ShiftTimeKey, number | null> = {
in1: parseT(shifts.in1),
out1: parseT(shifts.out1),
in2: parseT(shifts.in2),
out2: parseT(shifts.out2)
};
const bad: Partial<Record<ShiftTimeKey, true>> = {};
for (const key of Object.keys(t) as ShiftTimeKey[]) {
if (t[key] === null) bad[key] = true;
}
if (Object.keys(bad).length) return { minutes: null, bad, errorKey: 'settings.shift_error_format' };
const n = t as Record<ShiftTimeKey, number>;
if (n.out1 <= n.in1) return { minutes: null, bad: { out1: true }, errorKey: 'settings.shift_error_out1' };
if (n.in2 < n.out1) return { minutes: null, bad: { in2: true }, errorKey: 'settings.shift_error_in2' };
if (n.out2 <= n.in2) return { minutes: null, bad: { out2: true }, errorKey: 'settings.shift_error_out2' };
return { minutes: n, bad: {}, errorKey: null };
}
export const roundValid = (v: string): boolean => /^\d{1,2}$/.test(v) && Number(v) >= 1 && Number(v) <= 60;