reducer.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { Action, ConsumerGroup } from 'redux/interfaces';
  2. import { ActionType } from 'redux/actionType';
  3. import { ConsumerGroupsState } from '../../interfaces/consumerGroup';
  4. export const initialState: ConsumerGroupsState = {
  5. byID: {},
  6. allIDs: []
  7. };
  8. const updateConsumerGroupsList = (state: ConsumerGroupsState, payload: ConsumerGroup[]): ConsumerGroupsState => {
  9. const initialMemo: ConsumerGroupsState = {
  10. ...state,
  11. allIDs: []
  12. };
  13. return payload.reduce(
  14. (memo: ConsumerGroupsState, consumerGroup) => {
  15. const {consumerGroupId} = consumerGroup;
  16. memo.byID[consumerGroupId] = {
  17. ...memo.byID[consumerGroupId],
  18. ...consumerGroup,
  19. };
  20. memo.allIDs.push(consumerGroupId);
  21. return memo;
  22. },
  23. initialMemo,
  24. );
  25. };
  26. const reducer = (state = initialState, action: Action): ConsumerGroupsState => {
  27. switch (action.type) {
  28. case ActionType.GET_CONSUMER_GROUPS__SUCCESS:
  29. return updateConsumerGroupsList(state, action.payload);
  30. case ActionType.GET_CONSUMER_GROUP_DETAILS__SUCCESS:
  31. return {
  32. ...state,
  33. byID: {
  34. ...state.byID,
  35. [action.payload.consumerGroupID]: {
  36. ...state.byID[action.payload.consumerGroupID],
  37. ...action.payload.details,
  38. }
  39. }
  40. };
  41. default:
  42. return state;
  43. }
  44. };
  45. export default reducer;