Actions.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import React from 'react';
  2. import { useHistory, useParams } from 'react-router-dom';
  3. import { ConnectorState } from 'generated-sources';
  4. import { ClusterName, ConnectName, ConnectorName } from 'redux/interfaces';
  5. import {
  6. clusterConnectConnectorEditPath,
  7. clusterConnectorsPath,
  8. } from 'lib/paths';
  9. import ConfirmationModal from 'components/common/ConfirmationModal/ConfirmationModal';
  10. import styled from 'styled-components';
  11. import { Button } from 'components/common/Button/Button';
  12. interface RouterParams {
  13. clusterName: ClusterName;
  14. connectName: ConnectName;
  15. connectorName: ConnectorName;
  16. }
  17. const ConnectorActionsWrapperStyled = styled.div`
  18. display: flex;
  19. gap: 8px;
  20. `;
  21. export interface ActionsProps {
  22. deleteConnector(
  23. clusterName: ClusterName,
  24. connectName: ConnectName,
  25. connectorName: ConnectorName
  26. ): Promise<void>;
  27. isConnectorDeleting: boolean;
  28. connectorStatus?: ConnectorState;
  29. restartConnector(
  30. clusterName: ClusterName,
  31. connectName: ConnectName,
  32. connectorName: ConnectorName
  33. ): void;
  34. pauseConnector(
  35. clusterName: ClusterName,
  36. connectName: ConnectName,
  37. connectorName: ConnectorName
  38. ): void;
  39. resumeConnector(
  40. clusterName: ClusterName,
  41. connectName: ConnectName,
  42. connectorName: ConnectorName
  43. ): void;
  44. isConnectorActionRunning: boolean;
  45. }
  46. const Actions: React.FC<ActionsProps> = ({
  47. deleteConnector,
  48. isConnectorDeleting,
  49. connectorStatus,
  50. restartConnector,
  51. pauseConnector,
  52. resumeConnector,
  53. isConnectorActionRunning,
  54. }) => {
  55. const { clusterName, connectName, connectorName } = useParams<RouterParams>();
  56. const history = useHistory();
  57. const [
  58. isDeleteConnectorConfirmationVisible,
  59. setIsDeleteConnectorConfirmationVisible,
  60. ] = React.useState(false);
  61. const deleteConnectorHandler = React.useCallback(async () => {
  62. try {
  63. await deleteConnector(clusterName, connectName, connectorName);
  64. history.push(clusterConnectorsPath(clusterName));
  65. } catch {
  66. // do not redirect
  67. }
  68. }, [deleteConnector, clusterName, connectName, connectorName]);
  69. const restartConnectorHandler = React.useCallback(() => {
  70. restartConnector(clusterName, connectName, connectorName);
  71. }, [restartConnector, clusterName, connectName, connectorName]);
  72. const pauseConnectorHandler = React.useCallback(() => {
  73. pauseConnector(clusterName, connectName, connectorName);
  74. }, [pauseConnector, clusterName, connectName, connectorName]);
  75. const resumeConnectorHandler = React.useCallback(() => {
  76. resumeConnector(clusterName, connectName, connectorName);
  77. }, [resumeConnector, clusterName, connectName, connectorName]);
  78. return (
  79. <ConnectorActionsWrapperStyled>
  80. {connectorStatus === ConnectorState.RUNNING && (
  81. <Button
  82. buttonSize="M"
  83. buttonType="primary"
  84. type="button"
  85. onClick={pauseConnectorHandler}
  86. disabled={isConnectorActionRunning}
  87. >
  88. <span>
  89. <i className="fas fa-pause" />
  90. </span>
  91. <span>Pause</span>
  92. </Button>
  93. )}
  94. {connectorStatus === ConnectorState.PAUSED && (
  95. <Button
  96. buttonSize="M"
  97. buttonType="primary"
  98. type="button"
  99. onClick={resumeConnectorHandler}
  100. disabled={isConnectorActionRunning}
  101. >
  102. <span>
  103. <i className="fas fa-play" />
  104. </span>
  105. <span>Resume</span>
  106. </Button>
  107. )}
  108. <Button
  109. buttonSize="M"
  110. buttonType="primary"
  111. type="button"
  112. onClick={restartConnectorHandler}
  113. disabled={isConnectorActionRunning}
  114. >
  115. <span>
  116. <i className="fas fa-sync-alt" />
  117. </span>
  118. <span>Restart all tasks</span>
  119. </Button>
  120. <Button
  121. buttonSize="M"
  122. buttonType="primary"
  123. type="button"
  124. isLink
  125. disabled={isConnectorActionRunning}
  126. to={clusterConnectConnectorEditPath(
  127. clusterName,
  128. connectName,
  129. connectorName
  130. )}
  131. >
  132. <span>
  133. <i className="fas fa-pencil-alt" />
  134. </span>
  135. <span>Edit config</span>
  136. </Button>
  137. <Button
  138. buttonSize="M"
  139. buttonType="secondary"
  140. type="button"
  141. onClick={() => setIsDeleteConnectorConfirmationVisible(true)}
  142. disabled={isConnectorActionRunning}
  143. >
  144. <span>
  145. <i className="far fa-trash-alt" />
  146. </span>
  147. <span>Delete</span>
  148. </Button>
  149. <ConfirmationModal
  150. isOpen={isDeleteConnectorConfirmationVisible}
  151. onCancel={() => setIsDeleteConnectorConfirmationVisible(false)}
  152. onConfirm={deleteConnectorHandler}
  153. isConfirming={isConnectorDeleting}
  154. >
  155. Are you sure you want to remove <b>{connectorName}</b> connector?
  156. </ConfirmationModal>
  157. </ConnectorActionsWrapperStyled>
  158. );
  159. };
  160. export default Actions;