reducers.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { createEntityAdapter, createSlice } from '@reduxjs/toolkit';
  2. import { Invitee } from 'app/types';
  3. import { fetchInvitees, revokeInvite } from './actions';
  4. export type Status = 'idle' | 'loading' | 'succeeded' | 'failed';
  5. const invitesAdapter = createEntityAdapter({ selectId: (invite: Invitee) => invite.code });
  6. export const selectors = invitesAdapter.getSelectors();
  7. export const initialState = invitesAdapter.getInitialState<{ status: Status }>({ status: 'idle' });
  8. const invitesSlice = createSlice({
  9. name: 'invites',
  10. initialState,
  11. reducers: {},
  12. extraReducers: (builder) => {
  13. builder
  14. .addCase(fetchInvitees.pending, (state) => {
  15. state.status = 'loading';
  16. })
  17. .addCase(fetchInvitees.fulfilled, (state, { payload: invites }) => {
  18. invitesAdapter.setAll(state, invites);
  19. state.status = 'succeeded';
  20. })
  21. .addCase(fetchInvitees.rejected, (state) => {
  22. state.status = 'failed';
  23. })
  24. .addCase(revokeInvite.fulfilled, (state, { payload: inviteCode }) => {
  25. invitesAdapter.removeOne(state, inviteCode);
  26. state.status = 'succeeded';
  27. });
  28. },
  29. });
  30. export const invitesReducer = invitesSlice.reducer;
  31. export default {
  32. invites: invitesReducer,
  33. };