reducer.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { Action, TopicsState } from 'redux/interfaces';
  2. import { getType } from 'typesafe-actions';
  3. import * as actions from 'redux/actions';
  4. import * as _ from 'lodash';
  5. import { SortOrder, TopicColumnsToSort } from 'generated-sources';
  6. export const initialState: TopicsState = {
  7. byName: {},
  8. allNames: [],
  9. totalPages: 1,
  10. search: '',
  11. orderBy: TopicColumnsToSort.NAME,
  12. sortOrder: SortOrder.ASC,
  13. consumerGroups: [],
  14. };
  15. // eslint-disable-next-line @typescript-eslint/default-param-last
  16. const reducer = (state = initialState, action: Action): TopicsState => {
  17. switch (action.type) {
  18. case getType(actions.fetchTopicsListAction.success):
  19. case getType(actions.fetchTopicDetailsAction.success):
  20. case getType(actions.fetchTopicConfigAction.success):
  21. case getType(actions.createTopicAction.success):
  22. case getType(actions.fetchTopicConsumerGroupsAction.success):
  23. case getType(actions.updateTopicAction.success):
  24. return action.payload;
  25. case getType(actions.deleteTopicAction.success): {
  26. const newState: TopicsState = { ...state };
  27. delete newState.byName[action.payload];
  28. newState.allNames = newState.allNames.filter(
  29. (name) => name !== action.payload
  30. );
  31. return newState;
  32. }
  33. case getType(actions.recreateTopicAction.success):
  34. return {
  35. ...state,
  36. byName: {
  37. ...state.byName,
  38. [action.payload.name]: { ...action.payload },
  39. },
  40. };
  41. case getType(actions.setTopicsSearchAction): {
  42. return {
  43. ...state,
  44. search: action.payload,
  45. };
  46. }
  47. case getType(actions.setTopicsOrderByAction): {
  48. return {
  49. ...state,
  50. orderBy: action.payload,
  51. sortOrder:
  52. state.orderBy === action.payload && state.sortOrder === SortOrder.ASC
  53. ? SortOrder.DESC
  54. : SortOrder.ASC,
  55. };
  56. }
  57. case getType(actions.fetchTopicMessageSchemaAction.success): {
  58. const { topicName, schema } = action.payload;
  59. const newState = _.cloneDeep(state);
  60. newState.byName[topicName] = {
  61. ...newState.byName[topicName],
  62. messageSchema: schema,
  63. };
  64. return newState;
  65. }
  66. default:
  67. return state;
  68. }
  69. };
  70. export default reducer;