CustomBotWithoutProxySettings.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React, { useState, useEffect } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import PropTypes from 'prop-types';
  4. import AppContainer from '../../../services/AppContainer';
  5. import AdminAppContainer from '../../../services/AdminAppContainer';
  6. import { withUnstatedContainers } from '../../UnstatedUtils';
  7. import CustomBotWithoutProxySettingsAccordion, { botInstallationStep } from './CustomBotWithoutProxySettingsAccordion';
  8. import CustomBotWithoutProxyIntegrationCard from './CustomBotWithoutProxyIntegrationCard';
  9. import DeleteSlackBotSettingsModal from './DeleteSlackBotSettingsModal';
  10. const CustomBotWithoutProxySettings = (props) => {
  11. const { appContainer, onResetSettings, onSetIsSendTestMessage } = props;
  12. const { t } = useTranslation();
  13. const [siteName, setSiteName] = useState('');
  14. const [isDeleteConfirmModalShown, setIsDeleteConfirmModalShown] = useState(false);
  15. const [isIntegrationSuccess, setIsIntegrationSuccess] = useState(false);
  16. const [connectionMessage, setConnectionMessage] = useState(null);
  17. const [connectionErrorCode, setConnectionErrorCode] = useState(null);
  18. const [testChannel, setTestChannel] = useState('');
  19. const resetSettings = async() => {
  20. if (onResetSettings == null) {
  21. return;
  22. }
  23. onResetSettings();
  24. };
  25. const testConnection = async() => {
  26. setConnectionErrorCode(null);
  27. setConnectionMessage(null);
  28. try {
  29. await appContainer.apiv3.post('/slack-integration-settings/without-proxy/test', { channel: testChannel });
  30. setConnectionMessage('Send the message to slack work space.');
  31. onSetIsSendTestMessage(true);
  32. setIsIntegrationSuccess(true);
  33. }
  34. catch (err) {
  35. setConnectionErrorCode(err[0].code);
  36. setConnectionMessage(err[0].message);
  37. onSetIsSendTestMessage(false);
  38. setIsIntegrationSuccess(false);
  39. }
  40. };
  41. useEffect(() => {
  42. const siteName = appContainer.config.crowi.title;
  43. setSiteName(siteName);
  44. }, [appContainer]);
  45. return (
  46. <>
  47. <h2 className="admin-setting-header">{t('admin:slack_integration.custom_bot_without_proxy_integration')}</h2>
  48. <CustomBotWithoutProxyIntegrationCard
  49. siteName={siteName}
  50. slackWSNameInWithoutProxy={props.slackWSNameInWithoutProxy}
  51. isIntegrationSuccess={isIntegrationSuccess}
  52. />
  53. <h2 className="admin-setting-header">{t('admin:slack_integration.integration_procedure')}</h2>
  54. {(props.slackSigningSecret || props.slackBotToken) && (
  55. <button
  56. className="mx-3 pull-right btn text-danger border-danger"
  57. type="button"
  58. onClick={() => setIsDeleteConfirmModalShown(true)}
  59. >{t('admin:slack_integration.reset')}
  60. </button>
  61. )}
  62. <div className="my-5 mx-3">
  63. <CustomBotWithoutProxySettingsAccordion
  64. {...props}
  65. activeStep={botInstallationStep.CREATE_BOT}
  66. connectionMessage={connectionMessage}
  67. connectionErrorCode={connectionErrorCode}
  68. testChannel={testChannel}
  69. onTestConnection={testConnection}
  70. onSetTestChannel={setTestChannel}
  71. />
  72. </div>
  73. <DeleteSlackBotSettingsModal
  74. isResetAll={false}
  75. isOpen={isDeleteConfirmModalShown}
  76. onClose={() => setIsDeleteConfirmModalShown(false)}
  77. onClickDeleteButton={resetSettings}
  78. />
  79. </>
  80. );
  81. };
  82. const CustomBotWithoutProxySettingsWrapper = withUnstatedContainers(CustomBotWithoutProxySettings, [AppContainer, AdminAppContainer]);
  83. CustomBotWithoutProxySettings.propTypes = {
  84. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  85. adminAppContainer: PropTypes.instanceOf(AdminAppContainer).isRequired,
  86. slackSigningSecret: PropTypes.string,
  87. slackSigningSecretEnv: PropTypes.string,
  88. slackBotToken: PropTypes.string,
  89. slackBotTokenEnv: PropTypes.string,
  90. isRgisterSlackCredentials: PropTypes.bool,
  91. isIntegrationSuccess: PropTypes.bool,
  92. slackWSNameInWithoutProxy: PropTypes.string,
  93. onResetSettings: PropTypes.func,
  94. onSetIsSendTestMessage: PropTypes.func,
  95. };
  96. export default CustomBotWithoutProxySettingsWrapper;