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 }) => {
24 .api<{ Calendars: HolidaysDirectoryCalendar[] }>({
25 ...getDirectoryCalendars(CALENDAR_TYPE.HOLIDAYS),
28 .then(({ Calendars }) => {
32 previous: previousSelector(selectHolidaysDirectory),
35 const initialState = getInitialModelState<Model>();
36 const slice = createSlice({
40 extraReducers: (builder) => {
41 handleAsyncModel(builder, modelThunk);
45 export const holidaysDirectoryReducer = { [name]: slice.reducer };
46 export const holidaysDirectoryThunk = modelThunk.thunk;