Use source loader for email sprite icons
[ProtonMail-WebClient.git] / packages / wallet / store / slices / countriesByProvider.ts
blob7518cf6c1b128e28ebd97468f6c33718398e4cb8
1 import { createSlice } from '@reduxjs/toolkit';
3 import type { ModelState } from '@proton/account';
4 import { getInitialModelState } from '@proton/account';
5 import type { WasmApiCountry, WasmGatewayProvider } from '@proton/andromeda';
6 import { createAsyncModelThunk, handleAsyncModel, previousSelector } from '@proton/redux-utilities';
8 import type { WalletThunkArguments } from '../thunk';
10 const name = 'countries_by_provider' as const;
12 type CountriesByProvider = Partial<Record<WasmGatewayProvider, WasmApiCountry[]>>;
14 export interface CountriesByProviderState {
15     [name]: ModelState<CountriesByProvider>;
18 type SliceState = CountriesByProviderState[typeof name];
19 type Model = NonNullable<SliceState['value']>;
21 export const selectCountriesByProvider = (state: CountriesByProviderState) => {
22     return state[name];
25 const modelThunk = createAsyncModelThunk<Model, CountriesByProviderState, WalletThunkArguments>(`${name}/fetch`, {
26     miss: ({ extraArgument }) => {
27         return extraArgument.walletApi
28             .clients()
29             .payment_gateway.getCountries()
30             .then((countries) => {
31                 return countries.data.reduce((acc, current) => {
32                     const provider = current[0];
33                     const countries = current[1];
35                     return { ...acc, [provider]: countries.data };
36                 }, {} as CountriesByProvider);
37             });
38     },
39     previous: previousSelector(selectCountriesByProvider),
40 });
42 const initialState = getInitialModelState<Model>();
43 const slice = createSlice({
44     name,
45     initialState,
46     reducers: {},
47     extraReducers: (builder) => {
48         handleAsyncModel(builder, modelThunk);
49     },
50 });
52 export const countriesByProviderReducer = { [name]: slice.reducer };
53 export const countriesByProviderThunk = modelThunk.thunk;