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 type { VPNServersCountData } from '@proton/shared/lib/interfaces';
6 import { defaultVPNServersCountData, getVPNServersCountData } from '@proton/shared/lib/vpn/serversCount';
8 import { getInitialModelState } from '../initialModelState';
9 import type { ModelState } from '../interface';
11 const name = 'vpnServersCount' as const;
13 export interface VPNServersCountState {
14 [name]: ModelState<VPNServersCountData>;
17 type SliceState = VPNServersCountState[typeof name];
18 type Model = NonNullable<SliceState['value']>;
20 export const selectVpnServersCount = (state: VPNServersCountState) => state[name];
22 const modelThunk = createAsyncModelThunk<Model, VPNServersCountState, ProtonThunkArguments>(`${name}/fetch`, {
23 miss: async ({ extraArgument }) => {
24 return getVPNServersCountData(extraArgument.api);
26 previous: previousSelector(selectVpnServersCount),
29 const initialState = getInitialModelState<Model>(defaultVPNServersCountData);
30 const slice = createSlice({
34 extraReducers: (builder) => {
35 handleAsyncModel(builder, modelThunk);
39 export const vpnServersCountReducer = { [name]: slice.reducer };
40 export const vpnServersCountThunk = modelThunk.thunk;