Use source loader for email sprite icons
[ProtonMail-WebClient.git] / packages / wallet / store / slices / userWalletSettings.ts
blobc247a7ae64122e4592a67c64b7edfa7d12f241a8
1 import { createAction, createSlice } from '@reduxjs/toolkit';
3 import { type ModelState, getInitialModelState } from '@proton/account';
4 import { type WasmBitcoinUnit, type WasmUserSettings } from '@proton/andromeda';
5 import { createAsyncModelThunk, handleAsyncModel, previousSelector } from '@proton/redux-utilities';
7 import { DEFAULT_DISPLAY_BITCOIN_UNIT, DEFAULT_FIAT_CURRENCY } from '../../constants';
8 import { type WalletThunkArguments } from '../thunk';
10 const name = 'user_wallet_setting' as const;
12 export interface UserWalletSettingsState {
13     [name]: ModelState<WasmUserSettings>;
16 type SliceState = UserWalletSettingsState[typeof name];
17 type Model = NonNullable<SliceState['value']> | undefined;
19 export const DEFAULT_SETTINGS: WasmUserSettings = {
20     BitcoinUnit: DEFAULT_DISPLAY_BITCOIN_UNIT,
21     FiatCurrency: DEFAULT_FIAT_CURRENCY,
22     HideEmptyUsedAddresses: 0,
23     TwoFactorAmountThreshold: null,
24     ReceiveEmailIntegrationNotification: null,
25     ReceiveInviterNotification: null,
26     WalletCreated: null,
27     AcceptTermsAndConditions: 0,
28     ReceiveTransactionNotification: null,
31 export const bitcoinUnitChange = createAction('bitcoin-unit/update', (payload: { bitcoinUnit: WasmBitcoinUnit }) => ({
32     payload,
33 }));
35 export const acceptTermsAndConditions = createAction('accept terms and conditions', () => ({ payload: {} }));
37 export const selectUserWalletSettings = (state: UserWalletSettingsState) => state[name];
39 const initialState = getInitialModelState<Model>();
41 const modelThunk = createAsyncModelThunk<Model, UserWalletSettingsState, WalletThunkArguments>(`${name}/fetch`, {
42     miss: ({ extraArgument }) => {
43         return extraArgument.walletApi
44             .clients()
45             .settings.getUserSettings()
46             .then((settings) => {
47                 return settings[0];
48             })
49             .catch(() => DEFAULT_SETTINGS);
50     },
51     previous: previousSelector(selectUserWalletSettings),
52 });
54 const slice = createSlice({
55     name,
56     initialState,
57     reducers: {},
58     extraReducers: (builder) => {
59         handleAsyncModel(builder, modelThunk);
60         builder
61             .addCase(bitcoinUnitChange, (state, action) => {
62                 if (state.value) {
63                     if (action.payload.bitcoinUnit) {
64                         state.value.BitcoinUnit = action.payload.bitcoinUnit;
65                     }
66                 }
67             })
68             .addCase(acceptTermsAndConditions, (state) => {
69                 if (state.value) {
70                     state.value.AcceptTermsAndConditions = 1;
71                 }
72             });
73     },
74 });
76 export const userWalletSettingsReducer = { [name]: slice.reducer };
77 export const userWalletSettingsThunk = modelThunk.thunk;