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;
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
24 .settings.getUserSettings()
25 .then((settingsData) => {
26 return settingsData[0];
29 previous: previousSelector(selectWalletSettings),
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({
40 extraReducers: (builder) => {
41 handleAsyncModel(builder, modelThunk);
43 .addCase(serverEvent, () => {
46 .addCase(setBitcoinUnitEvent, (state, { payload }) => {
48 state.value.BitcoinUnit = payload;
51 .addCase(setTwoFaThresholdEvent, (state, { payload }) => {
53 state.value.TwoFactorAmountThreshold = payload;
59 export const walletSettingsReducer = { [name]: slice.reducer };
60 export const walletSettingsThunk = modelThunk.thunk;