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) => {
25 const modelThunk = createAsyncModelThunk<Model, CountriesByProviderState, WalletThunkArguments>(`${name}/fetch`, {
26 miss: ({ extraArgument }) => {
27 return extraArgument.walletApi
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);
39 previous: previousSelector(selectCountriesByProvider),
42 const initialState = getInitialModelState<Model>();
43 const slice = createSlice({
47 extraReducers: (builder) => {
48 handleAsyncModel(builder, modelThunk);
52 export const countriesByProviderReducer = { [name]: slice.reducer };
53 export const countriesByProviderThunk = modelThunk.thunk;