1 import { createSlice } from '@reduxjs/toolkit';
3 import type { ModelState } from '@proton/account';
4 import { getInitialModelState } from '@proton/account';
5 import type { WasmFiatCurrencySymbol, WasmGatewayProvider, WasmPaymentMethod } from '@proton/andromeda';
6 import { createAsyncModelThunk, handleAsyncModel } from '@proton/redux-utilities';
7 import { MINUTE } from '@proton/shared/lib/constants';
9 import type { WalletThunkArguments } from '../thunk';
11 const name = 'payment_method_by_provider' as const;
13 export type PaymentMethodsByProvider = Partial<Record<WasmGatewayProvider, WasmPaymentMethod[]>>;
14 type PaymentMethodsByProviderByFiat = Partial<Record<WasmFiatCurrencySymbol, PaymentMethodsByProvider>>;
16 export interface PaymentMethodsByProviderState {
17 [name]: ModelState<PaymentMethodsByProviderByFiat>;
20 type SliceState = PaymentMethodsByProviderState[typeof name];
21 type Model = NonNullable<SliceState['value']>;
23 export const selectPaymentMethodsByProvider = (state: PaymentMethodsByProviderState) => state[name];
25 const modelThunk = createAsyncModelThunk<
27 PaymentMethodsByProviderState,
29 WasmFiatCurrencySymbol
31 miss: async ({ extraArgument, options, getState }) => {
32 const stateValue = getState()[name].value;
33 if (!options?.thunkArg) {
34 return stateValue ?? {};
37 const fiat = options?.thunkArg;
39 return extraArgument.walletApi
41 .payment_gateway.getPaymentMethods(fiat)
42 .then((paymentMethods) => {
43 const paymentMethodsByProvider = paymentMethods.data.reduce((acc, current) => {
44 const provider = current[0];
45 const countries = current[1];
47 return { ...acc, [provider]: countries.data };
48 }, {} as PaymentMethodsByProvider);
52 [fiat]: paymentMethodsByProvider,
57 previous: ({ getState, options }) => {
58 const state = getState()[name];
59 if (!options?.thunkArg || !state.value) {
63 const fiat = options?.thunkArg;
65 if (state.value[fiat]) {
73 const initialState = getInitialModelState<Model>();
74 const slice = createSlice({
78 extraReducers: (builder) => {
79 handleAsyncModel(builder, modelThunk);
83 export const paymentMethodsByProviderReducer = { [name]: slice.reducer };
84 export const paymentMethodsByProviderThunk = modelThunk.thunk;