reducers.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { createSlice, PayloadAction } from '@reduxjs/toolkit';
  2. import config from 'app/core/config';
  3. import { OrgUser, UsersState } from 'app/types';
  4. export const initialState: UsersState = {
  5. users: [] as OrgUser[],
  6. searchQuery: '',
  7. searchPage: 1,
  8. canInvite: !config.externalUserMngLinkName,
  9. externalUserMngInfo: config.externalUserMngInfo,
  10. externalUserMngLinkName: config.externalUserMngLinkName,
  11. externalUserMngLinkUrl: config.externalUserMngLinkUrl,
  12. hasFetched: false,
  13. };
  14. const usersSlice = createSlice({
  15. name: 'users',
  16. initialState,
  17. reducers: {
  18. usersLoaded: (state, action: PayloadAction<OrgUser[]>): UsersState => {
  19. return { ...state, hasFetched: true, users: action.payload };
  20. },
  21. setUsersSearchQuery: (state, action: PayloadAction<string>): UsersState => {
  22. // reset searchPage otherwise search results won't appear
  23. return { ...state, searchQuery: action.payload, searchPage: initialState.searchPage };
  24. },
  25. setUsersSearchPage: (state, action: PayloadAction<number>): UsersState => {
  26. return { ...state, searchPage: action.payload };
  27. },
  28. },
  29. });
  30. export const { setUsersSearchQuery, setUsersSearchPage, usersLoaded } = usersSlice.actions;
  31. export const usersReducer = usersSlice.reducer;
  32. export default {
  33. users: usersReducer,
  34. };