reducer.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { getType } from 'typesafe-actions';
  2. import * as actions from 'redux/actions';
  3. import { ConnectState } from 'redux/interfaces/connect';
  4. import { Action } from 'redux/interfaces';
  5. import { ConnectorState, ConnectorTaskStatus } from 'generated-sources';
  6. export const initialState: ConnectState = {
  7. connects: [],
  8. connectors: [],
  9. currentConnector: {
  10. connector: null,
  11. tasks: [],
  12. config: null,
  13. },
  14. search: '',
  15. };
  16. const reducer = (state = initialState, action: Action): ConnectState => {
  17. switch (action.type) {
  18. case getType(actions.fetchConnectsAction.success):
  19. return {
  20. ...state,
  21. connects: action.payload.connects,
  22. };
  23. case getType(actions.fetchConnectorsAction.success):
  24. return {
  25. ...state,
  26. connectors: action.payload.connectors,
  27. };
  28. case getType(actions.fetchConnectorAction.success):
  29. case getType(actions.createConnectorAction.success):
  30. return {
  31. ...state,
  32. currentConnector: {
  33. ...state.currentConnector,
  34. connector: action.payload.connector,
  35. },
  36. };
  37. case getType(actions.deleteConnectorAction.success):
  38. return {
  39. ...state,
  40. connectors: state?.connectors.filter(
  41. ({ name }) => name !== action.payload.connectorName
  42. ),
  43. };
  44. case getType(actions.pauseConnectorAction.success):
  45. return {
  46. ...state,
  47. currentConnector: {
  48. ...state.currentConnector,
  49. connector: state.currentConnector.connector
  50. ? {
  51. ...state.currentConnector.connector,
  52. status: {
  53. ...state.currentConnector.connector?.status,
  54. state: ConnectorState.PAUSED,
  55. },
  56. }
  57. : null,
  58. tasks: state.currentConnector.tasks.map((task) => ({
  59. ...task,
  60. status: {
  61. ...task.status,
  62. state: ConnectorTaskStatus.PAUSED,
  63. },
  64. })),
  65. },
  66. };
  67. case getType(actions.resumeConnectorAction.success):
  68. return {
  69. ...state,
  70. currentConnector: {
  71. ...state.currentConnector,
  72. connector: state.currentConnector.connector
  73. ? {
  74. ...state.currentConnector.connector,
  75. status: {
  76. ...state.currentConnector.connector?.status,
  77. state: ConnectorState.RUNNING,
  78. },
  79. }
  80. : null,
  81. tasks: state.currentConnector.tasks.map((task) => ({
  82. ...task,
  83. status: {
  84. ...task.status,
  85. state: ConnectorTaskStatus.RUNNING,
  86. },
  87. })),
  88. },
  89. };
  90. case getType(actions.fetchConnectorTasksAction.success):
  91. return {
  92. ...state,
  93. currentConnector: {
  94. ...state.currentConnector,
  95. tasks: action.payload.tasks,
  96. },
  97. };
  98. case getType(actions.fetchConnectorConfigAction.success):
  99. return {
  100. ...state,
  101. currentConnector: {
  102. ...state.currentConnector,
  103. config: action.payload.config,
  104. },
  105. };
  106. case getType(actions.updateConnectorConfigAction.success):
  107. return {
  108. ...state,
  109. currentConnector: {
  110. ...state.currentConnector,
  111. connector: action.payload.connector,
  112. config: action.payload.connector.config,
  113. },
  114. };
  115. default:
  116. return state;
  117. }
  118. };
  119. export default reducer;