1 import { createSlice } from '@reduxjs/toolkit';
3 import { type ModelState, getInitialModelState, serverEvent } from '@proton/account';
4 import type { ProtonThunkArguments } from '@proton/redux-shared-store-types';
5 import { createAsyncModelThunk, handleAsyncModel, previousSelector } from '@proton/redux-utilities';
6 import { getMailSettings } from '@proton/shared/lib/api/mailSettings';
7 import updateObject from '@proton/shared/lib/helpers/updateObject';
8 import type { MailSettings } from '@proton/shared/lib/interfaces';
10 const name = 'mailSettings' as const;
12 export interface MailSettingState {
13 [name]: ModelState<MailSettings>;
16 type SliceState = MailSettingState[typeof name];
17 type Model = NonNullable<SliceState['value']>;
19 export const selectMailSettings = (state: MailSettingState) => state[name];
21 const modelThunk = createAsyncModelThunk<Model, MailSettingState, ProtonThunkArguments>(`${name}/fetch`, {
22 miss: ({ extraArgument }) => {
23 return extraArgument.api<{ MailSettings: MailSettings }>(getMailSettings()).then(({ MailSettings }) => {
27 previous: previousSelector(selectMailSettings),
30 const initialState = getInitialModelState<Model>();
31 const slice = createSlice({
35 extraReducers: (builder) => {
36 handleAsyncModel(builder, modelThunk);
37 builder.addCase(serverEvent, (state, action) => {
38 if (state.value && action.payload.MailSettings) {
39 state.value = updateObject(state.value, action.payload.MailSettings);
45 export const mailSettingsReducer = { [name]: slice.reducer };
46 export const mailSettingsThunk = modelThunk.thunk;