reducers.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { createSlice, PayloadAction } from '@reduxjs/toolkit';
  2. import { Team, TeamGroup, TeamMember, TeamsState, TeamState } from 'app/types';
  3. export const initialTeamsState: TeamsState = { teams: [], searchQuery: '', searchPage: 1, hasFetched: false };
  4. const teamsSlice = createSlice({
  5. name: 'teams',
  6. initialState: initialTeamsState,
  7. reducers: {
  8. teamsLoaded: (state, action: PayloadAction<Team[]>): TeamsState => {
  9. return { ...state, hasFetched: true, teams: action.payload };
  10. },
  11. setSearchQuery: (state, action: PayloadAction<string>): TeamsState => {
  12. return { ...state, searchQuery: action.payload, searchPage: initialTeamsState.searchPage };
  13. },
  14. setTeamsSearchPage: (state, action: PayloadAction<number>): TeamsState => {
  15. return { ...state, searchPage: action.payload };
  16. },
  17. },
  18. });
  19. export const { teamsLoaded, setSearchQuery, setTeamsSearchPage } = teamsSlice.actions;
  20. export const teamsReducer = teamsSlice.reducer;
  21. export const initialTeamState: TeamState = {
  22. team: {} as Team,
  23. members: [] as TeamMember[],
  24. groups: [] as TeamGroup[],
  25. searchMemberQuery: '',
  26. };
  27. const teamSlice = createSlice({
  28. name: 'team',
  29. initialState: initialTeamState,
  30. reducers: {
  31. teamLoaded: (state, action: PayloadAction<Team>): TeamState => {
  32. return { ...state, team: action.payload };
  33. },
  34. teamMembersLoaded: (state, action: PayloadAction<TeamMember[]>): TeamState => {
  35. return { ...state, members: action.payload };
  36. },
  37. setSearchMemberQuery: (state, action: PayloadAction<string>): TeamState => {
  38. return { ...state, searchMemberQuery: action.payload };
  39. },
  40. teamGroupsLoaded: (state, action: PayloadAction<TeamGroup[]>): TeamState => {
  41. return { ...state, groups: action.payload };
  42. },
  43. },
  44. });
  45. export const { teamLoaded, teamGroupsLoaded, teamMembersLoaded, setSearchMemberQuery } = teamSlice.actions;
  46. export const teamReducer = teamSlice.reducer;
  47. export default {
  48. teams: teamsReducer,
  49. team: teamReducer,
  50. };