forked from AmTote/calendar-appointments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgendaReducer.ts
52 lines (45 loc) · 1.35 KB
/
AgendaReducer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { format } from 'date-fns';
import { ImmerReducer } from 'immer-reducer';
import { Reminder } from '../models/Reminder';
import { Immutable } from '../types/immutable';
// import { debugReminders } from '../utils/debugUtils'; // FOR DEBUG
export type State = Immutable<{
agenda: {
isOpen: boolean;
reminders: Record<string, Reminder[]>;
date: Date | null;
};
}>;
export class AgendaReducer extends ImmerReducer<State> {
static readonly initialState: State = {
agenda: {
isOpen: false,
reminders: {}, //debugReminders, // FOR DEBUG
date: null
}
};
#state = this.draftState.agenda;
open(date: Date) {
this.#state.date = date;
this.#state.isOpen = true;
}
close() {
this.#state.date = null;
this.#state.isOpen = false;
}
addReminder(reminder: Reminder) {
const formattedDay = format(reminder.date, 'P');
if (!this.#state.reminders[formattedDay]) {
this.#state.reminders[formattedDay] = [reminder];
} else {
this.#state.reminders[formattedDay].push(reminder);
this.#state.reminders[formattedDay].sort((a, b) => b.date.getTime() - a.date.getTime());
}
}
}
export function getDateReminders(state: State, date: Date | null): Immutable<Reminder[]> | undefined {
if (date) {
const formattedDay = format(date, 'P');
return state.agenda.reminders[formattedDay];
}
}