reducers.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { createSlice } from '@reduxjs/toolkit';
  2. import { ApiKeysState } from 'app/types';
  3. export const initialApiKeysState: ApiKeysState = {
  4. hasFetched: false,
  5. includeExpired: false,
  6. keys: [],
  7. keysIncludingExpired: [],
  8. searchQuery: '',
  9. };
  10. const apiKeysSlice = createSlice({
  11. name: 'apiKeys',
  12. initialState: initialApiKeysState,
  13. reducers: {
  14. apiKeysLoaded: (state, action): ApiKeysState => {
  15. const { keys, keysIncludingExpired } = action.payload;
  16. const includeExpired =
  17. action.payload.keys.length === 0 && action.payload.keysIncludingExpired.length > 0
  18. ? true
  19. : state.includeExpired;
  20. return { ...state, hasFetched: true, keys, keysIncludingExpired, includeExpired };
  21. },
  22. setSearchQuery: (state, action): ApiKeysState => {
  23. return { ...state, searchQuery: action.payload };
  24. },
  25. includeExpiredToggled: (state): ApiKeysState => {
  26. return { ...state, includeExpired: !state.includeExpired };
  27. },
  28. isFetching: (state): ApiKeysState => {
  29. return { ...state, hasFetched: false };
  30. },
  31. },
  32. });
  33. export const { apiKeysLoaded, includeExpiredToggled, isFetching, setSearchQuery } = apiKeysSlice.actions;
  34. export const apiKeysReducer = apiKeysSlice.reducer;
  35. export default {
  36. apiKeys: apiKeysReducer,
  37. };