Merge branch 'renovate/all-minor-patch' into 'main'
[ProtonMail-WebClient.git] / packages / calendar / holidaysDirectory / index.ts
blob7415a2c39e24dd511e266cac82dd2573cc3e3be4
1 import { createSlice } from '@reduxjs/toolkit';
3 import { ModelState, UserSettingsState, getInitialModelState } from '@proton/account';
4 import type { ProtonThunkArguments } from '@proton/redux-shared-store-types';
5 import { createAsyncModelThunk, handleAsyncModel, previousSelector } from '@proton/redux-utilities';
6 import { getDirectoryCalendars } from '@proton/shared/lib/api/calendars';
7 import { CALENDAR_TYPE } from '@proton/shared/lib/calendar/constants';
8 import type { HolidaysDirectoryCalendar } from '@proton/shared/lib/interfaces/calendar';
10 const name = 'holidaysDirectory' as const;
12 export interface HolidaysDirectoryState extends UserSettingsState {
13     [name]: ModelState<HolidaysDirectoryCalendar[]>;
16 type SliceState = HolidaysDirectoryState[typeof name];
17 type Model = NonNullable<SliceState['value']>;
19 export const selectHolidaysDirectory = (state: HolidaysDirectoryState) => state[name];
21 const modelThunk = createAsyncModelThunk<Model, HolidaysDirectoryState, ProtonThunkArguments>(`${name}/fetch`, {
22     miss: ({ extraArgument }) => {
23         return extraArgument
24             .api<{ Calendars: HolidaysDirectoryCalendar[] }>({
25                 ...getDirectoryCalendars(CALENDAR_TYPE.HOLIDAYS),
26                 silence: true,
27             })
28             .then(({ Calendars }) => {
29                 return Calendars;
30             });
31     },
32     previous: previousSelector(selectHolidaysDirectory),
33 });
35 const initialState = getInitialModelState<Model>();
36 const slice = createSlice({
37     name,
38     initialState,
39     reducers: {},
40     extraReducers: (builder) => {
41         handleAsyncModel(builder, modelThunk);
42     },
43 });
45 export const holidaysDirectoryReducer = { [name]: slice.reducer };
46 export const holidaysDirectoryThunk = modelThunk.thunk;