Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / wallet / store / slices / fiatCurrenciesByProvider.ts
blob4741060cc0c03a4d414946b37003be3ff6b3c952
1 import { createSlice } from '@reduxjs/toolkit';
3 import type { ModelState } from '@proton/account';
4 import { getInitialModelState } from '@proton/account';
5 import type { WasmApiSimpleFiatCurrency, WasmGatewayProvider } from '@proton/andromeda';
6 import { createAsyncModelThunk, handleAsyncModel, previousSelector } from '@proton/redux-utilities';
8 import type { WalletThunkArguments } from '../thunk';
10 const name = 'fiat_currencies_by_provider' as const;
12 export type FiatCurrenciesByProvider = Partial<Record<WasmGatewayProvider, WasmApiSimpleFiatCurrency[]>>;
14 export interface FiatCurrenciesByProviderState {
15     [name]: ModelState<FiatCurrenciesByProvider>;
18 type SliceState = FiatCurrenciesByProviderState[typeof name];
19 type Model = NonNullable<SliceState['value']>;
21 export const selectFiatCurrenciesByProvider = (state: FiatCurrenciesByProviderState) => state[name];
23 const modelThunk = createAsyncModelThunk<Model, FiatCurrenciesByProviderState, WalletThunkArguments>(`${name}/fetch`, {
24     miss: ({ extraArgument }) => {
25         return extraArgument.walletApi
26             .clients()
27             .payment_gateway.getFiatCurrencies()
28             .then((fiatCurrencies) => {
29                 return fiatCurrencies.data.reduce((acc: FiatCurrenciesByProvider, current) => {
30                     const provider = current[0];
31                     const fiatCurrencies = current[1];
33                     return { ...acc, [provider]: fiatCurrencies.data };
34                 }, {});
35             });
36     },
37     previous: previousSelector(selectFiatCurrenciesByProvider),
38 });
40 const initialState = getInitialModelState<Model>();
41 const slice = createSlice({
42     name,
43     initialState,
44     reducers: {},
45     extraReducers: (builder) => {
46         handleAsyncModel(builder, modelThunk);
47     },
48 });
50 export const fiatCurrenciesByProviderReducer = { [name]: slice.reducer };
51 export const fiatCurrenciesByProviderThunk = modelThunk.thunk;