1 import { createSlice } from '@reduxjs/toolkit';
3 import type { ProtonThunkArguments } from '@proton/redux-shared-store-types';
4 import { createAsyncModelThunk, handleAsyncModel, previousSelector } from '@proton/redux-utilities';
5 import { queryAvailableDomains, queryPremiumDomains } from '@proton/shared/lib/api/domains';
6 import { Api } from '@proton/shared/lib/interfaces';
8 import { getInitialModelState } from '../initialModelState';
9 import { ModelState } from '../interface';
11 const name = 'protonDomains' as const;
13 export interface ProtonDomainsState {
14 [name]: ModelState<{ premiumDomains: string[]; protonDomains: string[] }>;
17 type SliceState = ProtonDomainsState[typeof name];
18 type Model = NonNullable<SliceState['value']>;
20 export const selectProtonDomains = (state: ProtonDomainsState) => state[name];
21 const getDomains = async (api: Api) => {
22 const [premiumDomains, protonDomains] = await Promise.all([
23 api(queryPremiumDomains()).then(({ Domains = [] }) => Domains),
24 api(queryAvailableDomains()).then(({ Domains = [] }) => Domains),
32 const modelThunk = createAsyncModelThunk<Model, ProtonDomainsState, ProtonThunkArguments>(`${name}/fetch`, {
33 miss: ({ extraArgument }) => {
34 return getDomains(extraArgument.api);
36 previous: previousSelector(selectProtonDomains),
39 const initialState = getInitialModelState<Model>();
40 const slice = createSlice({
44 extraReducers: (builder) => {
45 handleAsyncModel(builder, modelThunk);
49 export const protonDomainsReducer = { [name]: slice.reducer };
50 export const protonDomainsThunk = modelThunk.thunk;