This commit is contained in:
1 parent
03841c6e3d
commit
036a8c0e1e
32 files changed
+1805
-89
No files matched your search
@@ -1,11 +1,67 @@
|
||||
import { Injectable, signal } from '@angular/core';
|
||||
import { SelectableDayType, VacDay } from './calendar-service';
|
||||
import dayjs from 'dayjs';
|
||||
import {
|
||||
DeclaredNonWorkingDay,
|
||||
isWorkingPlanningDate,
|
||||
parseDeclaredDays,
|
||||
} from './declared-non-working-days';
|
||||
import type { LeaveType } from './leave-planner.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class UserInputService {
|
||||
private readonly declaredDays = signal(this.restoreDeclaredDays());
|
||||
readonly declaredDaysSignal = this.declaredDays.asReadonly();
|
||||
readonly declarationEntrySignal = signal(false);
|
||||
readonly declaredTypeSignal = signal<LeaveType | null>('cp');
|
||||
|
||||
private restoreDeclaredDays(): DeclaredNonWorkingDay[] {
|
||||
try {
|
||||
const saved =
|
||||
typeof localStorage === 'undefined'
|
||||
? null
|
||||
: localStorage.getItem('declaredNonWorkingDays');
|
||||
return saved === null ? [] : parseDeclaredDays(JSON.parse(saved));
|
||||
} catch (error) {
|
||||
console.warn('Failed to restore declared non-working days from localStorage.', error);
|
||||
try {
|
||||
localStorage.removeItem('declaredNonWorkingDays');
|
||||
} catch {
|
||||
// Browser storage can be unavailable; keep calendar entry usable.
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
toggleDeclaredDay(date: string, type: LeaveType): void {
|
||||
this.toggleDeclaredDays([date], type);
|
||||
}
|
||||
|
||||
toggleDeclaredDays(dates: readonly string[], type: LeaveType | null): void {
|
||||
const workingDates = new Set(dates.filter(isWorkingPlanningDate));
|
||||
if (!workingDates.size) return;
|
||||
this.declaredDays.update((days) => {
|
||||
const existing = new Map(days.map((day) => [day.date, day.type]));
|
||||
const others = days.filter((day) => !workingDates.has(day.date));
|
||||
return [
|
||||
...others,
|
||||
...(type
|
||||
? [...workingDates]
|
||||
.filter((date) => existing.get(date) !== type)
|
||||
.map((date) => ({ date, type }))
|
||||
: []),
|
||||
].sort((a, b) => a.date.localeCompare(b.date));
|
||||
});
|
||||
try {
|
||||
if (typeof localStorage !== 'undefined')
|
||||
localStorage.setItem('declaredNonWorkingDays', JSON.stringify(this.declaredDays()));
|
||||
} catch (error) {
|
||||
console.warn('Failed to save declared non-working days to localStorage.', error);
|
||||
}
|
||||
}
|
||||
|
||||
private restoreDate(value: unknown): Date {
|
||||
if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(value)) {
|
||||
const date = new Date(`${value}T00:00:00`);
|
||||
|
||||
Reference in new issue
Block a user