reducer.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { v4 } from 'uuid';
  2. import { Topic, TopicMessage } from 'generated-sources';
  3. import { Action, TopicsState } from 'redux/interfaces';
  4. import { getType } from 'typesafe-actions';
  5. import * as actions from 'redux/actions';
  6. export const initialState: TopicsState = {
  7. byName: {},
  8. allNames: [],
  9. messages: [],
  10. };
  11. const updateTopicList = (state: TopicsState, payload: Topic[]): TopicsState => {
  12. const initialMemo: TopicsState = {
  13. ...state,
  14. allNames: [],
  15. };
  16. return payload.reduce(
  17. (memo: TopicsState, topic) => ({
  18. ...memo,
  19. byName: {
  20. ...memo.byName,
  21. [topic.name]: {
  22. ...memo.byName[topic.name],
  23. ...topic,
  24. id: v4(),
  25. },
  26. },
  27. allNames: [...memo.allNames, topic.name],
  28. }),
  29. initialMemo
  30. );
  31. };
  32. const addToTopicList = (state: TopicsState, payload: Topic): TopicsState => {
  33. const newState: TopicsState = {
  34. ...state,
  35. };
  36. newState.allNames.push(payload.name);
  37. newState.byName[payload.name] = { ...payload, id: v4() };
  38. return newState;
  39. };
  40. const transformTopicMessages = (
  41. state: TopicsState,
  42. messages: TopicMessage[]
  43. ): TopicsState => ({
  44. ...state,
  45. messages: messages.map((mes) => {
  46. const { content } = mes;
  47. let parsedContent = content;
  48. if (content) {
  49. try {
  50. parsedContent =
  51. typeof content !== 'object' ? JSON.parse(content) : content;
  52. } catch (_) {
  53. // do nothing
  54. }
  55. }
  56. return {
  57. ...mes,
  58. content: parsedContent,
  59. };
  60. }),
  61. });
  62. const reducer = (state = initialState, action: Action): TopicsState => {
  63. switch (action.type) {
  64. case getType(actions.fetchTopicsListAction.success):
  65. return updateTopicList(state, action.payload);
  66. case getType(actions.fetchTopicDetailsAction.success):
  67. return {
  68. ...state,
  69. byName: {
  70. ...state.byName,
  71. [action.payload.topicName]: {
  72. ...state.byName[action.payload.topicName],
  73. ...action.payload.details,
  74. },
  75. },
  76. };
  77. case getType(actions.fetchTopicMessagesAction.success):
  78. return transformTopicMessages(state, action.payload);
  79. case getType(actions.fetchTopicConfigAction.success):
  80. return {
  81. ...state,
  82. byName: {
  83. ...state.byName,
  84. [action.payload.topicName]: {
  85. ...state.byName[action.payload.topicName],
  86. config: action.payload.config.map((inputConfig) => ({
  87. ...inputConfig,
  88. id: v4(),
  89. })),
  90. },
  91. },
  92. };
  93. case getType(actions.createTopicAction.success):
  94. return addToTopicList(state, action.payload);
  95. default:
  96. return state;
  97. }
  98. };
  99. export default reducer;