Merge branch 'pass-lifetime-fixes' into 'main'
[ProtonMail-WebClient.git] / packages / account / allowAddressDeletion / index.ts
blobdd44be01c9d47eb73c09484aab5065c7b5e50f18
1 import { createAction, createSlice } from '@reduxjs/toolkit';
3 import type { ProtonThunkArguments } from '@proton/redux-shared-store-types';
4 import { createAsyncModelThunk, handleAsyncModel, previousSelector } from '@proton/redux-utilities';
5 import { getAllowAddressDeletion } from '@proton/shared/lib/api/addresses';
7 import { getInitialModelState } from '../initialModelState';
8 import type { ModelState } from '../interface';
10 const name = 'allowAddressDeletion' as const;
12 export interface AllowAddressDeletionState {
13     [name]: ModelState<boolean>;
16 const initialState = getInitialModelState<boolean>();
18 export const selectAllowAddressDeletion = (state: AllowAddressDeletionState) => state[name];
20 const modelThunk = createAsyncModelThunk<boolean, AllowAddressDeletionState, ProtonThunkArguments>(`${name}/fetch`, {
21     miss: async ({ extraArgument }) => {
22         return extraArgument
23             .api(getAllowAddressDeletion())
24             .then((response) => response.AddressDeletion.Allow)
25             .catch(() => false);
26     },
27     previous: previousSelector(selectAllowAddressDeletion),
28 });
30 export const disableAllowAddressDeletion = createAction('allowAddressDeletion/update', () => ({ payload: {} }));
32 const slice = createSlice({
33     name,
34     initialState,
35     reducers: {},
36     extraReducers: (builder) => {
37         handleAsyncModel(builder, modelThunk);
39         builder.addCase(disableAllowAddressDeletion, (state) => {
40             state.value = false;
41         });
42     },
43 });
45 export const allowAddressDeletionReducer = { [name]: slice.reducer };
46 export const allowAddressDeletionThunk = modelThunk.thunk;