Use source loader for email sprite icons
[ProtonMail-WebClient.git] / packages / wallet / store / slices / userEligibility.ts
blob827316e42ca66b3d175fcc53db5d848b211d8a2e
1 import { createSlice } from '@reduxjs/toolkit';
3 import type { ModelState } from '@proton/account';
4 import { getInitialModelState } from '@proton/account';
5 import { createAsyncModelThunk, handleAsyncModel, previousSelector } from '@proton/redux-utilities';
7 import type { WalletThunkArguments } from '../thunk';
9 const name = 'user_eligibility' as const;
11 export interface UserEligibilityState {
12     [name]: ModelState<boolean>;
15 type SliceState = UserEligibilityState[typeof name];
16 type Model = NonNullable<SliceState['value']>;
18 export const selectUserEligibility = (state: UserEligibilityState) => {
19     return state[name];
22 const modelThunk = createAsyncModelThunk<Model, UserEligibilityState, WalletThunkArguments>(`${name}/fetch`, {
23     miss: async ({ extraArgument }) => {
24         const isEligible = await extraArgument.walletApi
25             .clients()
26             .settings.getUserWalletEligibility()
27             .then((data) => Boolean(data))
28             .catch(() => false);
30         return isEligible;
31     },
32     previous: previousSelector(selectUserEligibility),
33 });
35 const initialState = getInitialModelState<Model>();
37 const slice = createSlice({
38     name,
39     initialState,
40     reducers: {},
41     extraReducers: (builder) => {
42         handleAsyncModel(builder, modelThunk);
43     },
44 });
46 export const userEligibilityReducer = { [name]: slice.reducer };
47 export const userEligibilityThunk = modelThunk.thunk;