reducer.ts 1.3 KB

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