CustomBotWithoutProxySettings.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 CustomBotWithoutProxyConnectionStatus from './CustomBotWithoutProxyConnectionStatus';
  9. import DeleteSlackBotSettingsModal from './DeleteSlackBotSettingsModal';
  10. const CustomBotWithoutProxySettings = (props) => {
  11. const { appContainer, onResetSettings, connectionStatuses } = 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('');
  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. try {
  27. await appContainer.apiv3.post('/slack-integration-settings/without-proxy/test', { channel: testChannel });
  28. setConnectionMessage('Send the message to slack work space.');
  29. setIsIntegrationSuccess(true);
  30. }
  31. catch (err) {
  32. setConnectionErrorCode(err[0].code);
  33. setConnectionMessage(err[0].message);
  34. setIsIntegrationSuccess(false);
  35. }
  36. };
  37. const inputTestChannelHandler = (channel) => {
  38. setTestChannel(channel);
  39. };
  40. useEffect(() => {
  41. const siteName = appContainer.config.crowi.title;
  42. setSiteName(siteName);
  43. }, [appContainer]);
  44. return (
  45. <>
  46. <h2 className="admin-setting-header">{t('admin:slack_integration.custom_bot_without_proxy_integration')}</h2>
  47. <CustomBotWithoutProxyConnectionStatus
  48. siteName={siteName}
  49. connectionStatuses={connectionStatuses}
  50. />
  51. <h2 className="admin-setting-header">{t('admin:slack_integration.integration_procedure')}</h2>
  52. {(props.slackSigningSecret || props.slackBotToken) && (
  53. <button
  54. className="mx-3 pull-right btn text-danger border-danger"
  55. type="button"
  56. onClick={() => setIsDeleteConfirmModalShown(true)}
  57. >{t('admin:slack_integration.reset')}
  58. </button>
  59. )}
  60. <div className="my-5 mx-3">
  61. {/* {isConnectedFailed && (<>Settings #1 <span className="text-danger">{t('admin:slack_integration.integration_failed')}</span></>)} */}
  62. <CustomBotWithoutProxySettingsAccordion
  63. {...props}
  64. activeStep={botInstallationStep.CREATE_BOT}
  65. connectionMessage={connectionMessage}
  66. connectionErrorCode={connectionErrorCode}
  67. isIntegrationSuccess={isIntegrationSuccess}
  68. testChannel={testChannel}
  69. onTestFormSubmitted={testConnection}
  70. inputTestChannelHandler={inputTestChannelHandler}
  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. connectionStatuses: PropTypes.object.isRequired,
  95. };
  96. export default CustomBotWithoutProxySettingsWrapper;