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,
27 AcceptTermsAndConditions: 0,
28 ReceiveTransactionNotification: null,
31 export const bitcoinUnitChange = createAction('bitcoin-unit/update', (payload: { bitcoinUnit: WasmBitcoinUnit }) => ({
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
45 .settings.getUserSettings()
49 .catch(() => DEFAULT_SETTINGS);
51 previous: previousSelector(selectUserWalletSettings),
54 const slice = createSlice({
58 extraReducers: (builder) => {
59 handleAsyncModel(builder, modelThunk);
61 .addCase(bitcoinUnitChange, (state, action) => {
63 if (action.payload.bitcoinUnit) {
64 state.value.BitcoinUnit = action.payload.bitcoinUnit;
68 .addCase(acceptTermsAndConditions, (state) => {
70 state.value.AcceptTermsAndConditions = 1;
76 export const userWalletSettingsReducer = { [name]: slice.reducer };
77 export const userWalletSettingsThunk = modelThunk.thunk;