1 import { createSlice } from '@reduxjs/toolkit';
3 import type { ModelState } from '@proton/account';
4 import { getInitialModelState } from '@proton/account';
5 import type { WasmApiFiatCurrency } from '@proton/andromeda';
6 import { createAsyncModelThunk, handleAsyncModel, previousSelector } from '@proton/redux-utilities';
8 import type { WalletThunkArguments } from '../thunk';
10 const name = 'fiat_currencies' as const;
12 export interface FiatCurrenciesState {
13 [name]: ModelState<WasmApiFiatCurrency[]>;
16 type SliceState = FiatCurrenciesState[typeof name];
17 type Model = NonNullable<SliceState['value']>;
19 export const selectSortedFiatCurrencies = (state: FiatCurrenciesState) => {
20 const cloned = state[name]?.value ? [...state[name]?.value] : undefined;
21 cloned?.sort((a, b) => (a.Name > b.Name ? 1 : -1));
22 return { ...state[name], value: cloned };
25 const modelThunk = createAsyncModelThunk<Model, FiatCurrenciesState, WalletThunkArguments>(`${name}/fetch`, {
26 miss: ({ extraArgument }) => {
27 return extraArgument.walletApi
29 .exchange_rate.getAllFiatCurrencies()
30 .then((currencies) => currencies[0].map((c) => c.Data));
32 previous: previousSelector(selectSortedFiatCurrencies),
35 const initialState = getInitialModelState<Model>();
36 const slice = createSlice({
40 extraReducers: (builder) => {
41 handleAsyncModel(builder, modelThunk);
45 export const fiatCurrenciesReducer = { [name]: slice.reducer };
46 export const fiatCurrenciesThunk = modelThunk.thunk;