reducer.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { SchemaSubject } from 'generated-sources';
  2. import ActionType from 'redux/actionType';
  3. import { Action, SchemasState } from 'redux/interfaces';
  4. export const initialState: SchemasState = {
  5. byName: {},
  6. allNames: [],
  7. };
  8. const updateSchemaList = (
  9. state: SchemasState,
  10. payload: SchemaSubject[]
  11. ): SchemasState => {
  12. const initialMemo: SchemasState = {
  13. ...state,
  14. allNames: [],
  15. };
  16. return payload.reduce((memo: SchemasState, schema) => {
  17. if (!schema.subject) return memo;
  18. return {
  19. ...memo,
  20. byName: {
  21. ...memo.byName,
  22. [schema.subject]: {
  23. ...memo.byName[schema.subject],
  24. ...schema,
  25. },
  26. },
  27. allNames: [...memo.allNames, schema.subject],
  28. };
  29. }, initialMemo);
  30. };
  31. const reducer = (state = initialState, action: Action): SchemasState => {
  32. switch (action.type) {
  33. case ActionType.GET_CLUSTER_SCHEMAS__SUCCESS:
  34. return updateSchemaList(state, action.payload);
  35. default:
  36. return state;
  37. }
  38. };
  39. export default reducer;