1 import { createAction, 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 = 'remaining_invites' as const;
11 export interface RemainingInvitesState {
12 [name]: ModelState<{ available: number; used: number }>;
15 type SliceState = RemainingInvitesState[typeof name];
16 type Model = NonNullable<SliceState['value']>;
18 export const selectRemainingInvites = (state: RemainingInvitesState) => {
22 export const decrementAvailableInvites = createAction('decrement available invites', () => ({ payload: {} }));
24 const modelThunk = createAsyncModelThunk<Model, RemainingInvitesState, WalletThunkArguments>(`${name}/fetch`, {
25 miss: async ({ extraArgument }) => {
26 const invites = await extraArgument.walletApi
28 .invite.getRemainingMonthlyInvitation()
29 .then((data) => ({ available: data.Available, used: data.Used }))
30 .catch(() => ({ available: 0, used: 0 }));
34 previous: previousSelector(selectRemainingInvites),
37 const initialState = getInitialModelState<Model>();
39 const slice = createSlice({
43 extraReducers: (builder) => {
44 handleAsyncModel(builder, modelThunk);
45 builder.addCase(decrementAvailableInvites, (state) => {
46 if (state.value && state.value.available > 0) {
47 state.value.available--;
53 export const remainingInvitesReducer = { [name]: slice.reducer };
54 export const remainingInvitesThunk = modelThunk.thunk;