SlackIntegration.jsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import React, { useState, useEffect, useCallback } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { useTranslation } from 'react-i18next';
  4. import AppContainer from '../../../services/AppContainer';
  5. import { withUnstatedContainers } from '../../UnstatedUtils';
  6. import { toastSuccess, toastError } from '../../../util/apiNotification';
  7. import AccessTokenSettings from './AccessTokenSettings';
  8. import OfficialBotSettings from './OfficialBotSettings';
  9. import CustomBotWithoutProxySettings from './CustomBotWithoutProxySettings';
  10. import CustomBotWithProxySettings from './CustomBotWithProxySettings';
  11. import ConfirmBotChangeModal from './ConfirmBotChangeModal';
  12. import BotTypeCard from './BotTypeCard';
  13. const SlackIntegration = (props) => {
  14. const { appContainer } = props;
  15. const { t } = useTranslation();
  16. const [currentBotType, setCurrentBotType] = useState(null);
  17. const [selectedBotType, setSelectedBotType] = useState(null);
  18. const [accessToken, setAccessToken] = useState('');
  19. const fetchData = useCallback(async() => {
  20. try {
  21. const response = await appContainer.apiv3.get('slack-integration/');
  22. const { currentBotType, accessToken } = response.data.slackBotSettingParams;
  23. setCurrentBotType(currentBotType);
  24. setAccessToken(accessToken);
  25. }
  26. catch (err) {
  27. toastError(err);
  28. }
  29. }, [appContainer.apiv3]);
  30. useEffect(() => {
  31. fetchData();
  32. }, [fetchData]);
  33. const handleBotTypeSelect = (clickedBotType) => {
  34. if (clickedBotType === currentBotType) {
  35. return;
  36. }
  37. if (currentBotType === null) {
  38. setCurrentBotType(clickedBotType);
  39. return;
  40. }
  41. setSelectedBotType(clickedBotType);
  42. };
  43. const cancelBotChangeHandler = () => {
  44. setSelectedBotType(null);
  45. };
  46. const changeCurrentBotSettingsHandler = async() => {
  47. try {
  48. const res = await appContainer.apiv3.put('slack-integration/custom-bot-without-proxy', {
  49. slackSigningSecret: '',
  50. slackBotToken: '',
  51. currentBotType: selectedBotType,
  52. });
  53. setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType);
  54. setSelectedBotType(null);
  55. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  56. }
  57. catch (err) {
  58. toastError(err);
  59. }
  60. };
  61. const generateTokenHandler = async() => {
  62. try {
  63. await appContainer.apiv3.put('slack-integration/access-token');
  64. fetchData();
  65. }
  66. catch (err) {
  67. toastError(err);
  68. }
  69. };
  70. const discardTokenHandler = async() => {
  71. try {
  72. await appContainer.apiv3.delete('slack-integration/access-token');
  73. fetchData();
  74. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  75. }
  76. catch (err) {
  77. toastError(err);
  78. }
  79. };
  80. let settingsComponent = null;
  81. switch (currentBotType) {
  82. case 'official-bot':
  83. settingsComponent = <OfficialBotSettings />;
  84. break;
  85. case 'custom-bot-without-proxy':
  86. settingsComponent = (
  87. <CustomBotWithoutProxySettings />
  88. );
  89. break;
  90. case 'custom-bot-with-proxy':
  91. settingsComponent = <CustomBotWithProxySettings />;
  92. break;
  93. }
  94. // const botTypes = {
  95. // officialBot: {
  96. // botType: 'official-bot',
  97. // botTypeCategory: 'official_bot',
  98. // setUp: 'easy',
  99. // multiWSIntegration: 'possible',
  100. // securityControl: 'impossible',
  101. // },
  102. // customBotWithoutProxy: {
  103. // botType: 'custom-bot-without-proxy',
  104. // botTypeCategory: 'custom_bot',
  105. // supplementaryBotName: 'without_proxy',
  106. // setUp: 'normal',
  107. // multiWSIntegration: 'impossible',
  108. // securityControl: 'possible',
  109. // },
  110. // customBotWithProxy: {
  111. // botType: 'custom-bot-with-proxy',
  112. // botTypeCategory: 'custom_bot',
  113. // supplementaryBotName: 'with_proxy',
  114. // setUp: 'hard',
  115. // multiWSIntegration: 'possible',
  116. // securityControl: 'possible',
  117. // },
  118. // };
  119. const botTypes = ['officialBot', 'customBotWithoutProxy', 'customBotWithProxy'];
  120. return (
  121. <>
  122. <ConfirmBotChangeModal
  123. isOpen={selectedBotType != null}
  124. onConfirmClick={changeCurrentBotSettingsHandler}
  125. onCancelClick={cancelBotChangeHandler}
  126. />
  127. <AccessTokenSettings
  128. accessToken={accessToken}
  129. onClickDiscardButton={discardTokenHandler}
  130. onClickGenerateToken={generateTokenHandler}
  131. />
  132. <div className="selecting-bot-type my-5">
  133. <h2 className="admin-setting-header mb-4">
  134. {t('admin:slack_integration.selecting_bot_types.slack_bot')}
  135. <span className="ml-2 btn-link">
  136. <span className="mr-1">{t('admin:slack_integration.selecting_bot_types.detailed_explanation')}</span>
  137. {/* TODO: add an appropriate link by GW-5614 */}
  138. <i className="fa fa-external-link" aria-hidden="true"></i>
  139. </span>
  140. </h2>
  141. {t('admin:slack_integration.selecting_bot_types.selecting_bot_type')}
  142. <div className="row my-4">
  143. <div className="card-deck mx-auto">
  144. {/* {Object.entries(botTypes).map(([key, value]) => { */}
  145. {botTypes.map((botType) => {
  146. return (
  147. <BotTypeCard
  148. key={botType}
  149. botType={botType}
  150. isActive={currentBotType === botType}
  151. // value={value}
  152. handleBotTypeSelect={handleBotTypeSelect}
  153. />
  154. );
  155. })}
  156. </div>
  157. </div>
  158. </div>
  159. {settingsComponent}
  160. </>
  161. );
  162. };
  163. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  164. SlackIntegration.propTypes = {
  165. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  166. };
  167. export default SlackIntegrationWrapper;