Use source loader for email sprite icons
[ProtonMail-WebClient.git] / packages / wallet / store / slices / walletSettings.ts
blobfbe3465127ce81c22f2f1554190deb31224500e3
1 import { createAction, createSlice } from '@reduxjs/toolkit';
3 import { type ModelState, getInitialModelState, serverEvent } from '@proton/account';
4 import type { WasmUserSettings as WalletSettings, WasmBitcoinUnit } from '@proton/andromeda';
5 import { createAsyncModelThunk, handleAsyncModel, previousSelector } from '@proton/redux-utilities';
7 import type { WalletThunkArguments } from '../thunk';
9 const name = 'wallet_settings' as const;
11 interface State {
12     [name]: ModelState<WalletSettings>;
15 type SliceState = State[typeof name];
16 type Model = NonNullable<SliceState['value']>;
18 export const selectWalletSettings = (state: State) => state[name];
20 const modelThunk = createAsyncModelThunk<Model, State, WalletThunkArguments>(`${name}/fetch`, {
21     miss: ({ extraArgument }) => {
22         return extraArgument.walletApi
23             .clients()
24             .settings.getUserSettings()
25             .then((settingsData) => {
26                 return settingsData[0];
27             });
28     },
29     previous: previousSelector(selectWalletSettings),
30 });
32 export const setBitcoinUnitEvent = createAction('setBitcoinUnit', (payload: WasmBitcoinUnit) => ({ payload }));
33 export const setTwoFaThresholdEvent = createAction('setTwoFaThreshold', (payload: number) => ({ payload }));
35 const initialState = getInitialModelState<Model>();
36 const slice = createSlice({
37     name,
38     initialState,
39     reducers: {},
40     extraReducers: (builder) => {
41         handleAsyncModel(builder, modelThunk);
42         builder
43             .addCase(serverEvent, () => {
44                 // TODO: event loop
45             })
46             .addCase(setBitcoinUnitEvent, (state, { payload }) => {
47                 if (state.value) {
48                     state.value.BitcoinUnit = payload;
49                 }
50             })
51             .addCase(setTwoFaThresholdEvent, (state, { payload }) => {
52                 if (state.value) {
53                     state.value.TwoFactorAmountThreshold = payload;
54                 }
55             });
56     },
57 });
59 export const walletSettingsReducer = { [name]: slice.reducer };
60 export const walletSettingsThunk = modelThunk.thunk;