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
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 };
37 previous: previousSelector(selectFiatCurrenciesByProvider),
40 const initialState = getInitialModelState<Model>();
41 const slice = createSlice({
45 extraReducers: (builder) => {
46 handleAsyncModel(builder, modelThunk);
50 export const fiatCurrenciesByProviderReducer = { [name]: slice.reducer };
51 export const fiatCurrenciesByProviderThunk = modelThunk.thunk;