CustomBotWithoutProxySettings.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 } = 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. 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. setIsIntegrationSuccess(true);
  32. }
  33. catch (err) {
  34. setConnectionErrorCode(err[0].code);
  35. setConnectionMessage(err[0].message);
  36. setIsIntegrationSuccess(false);
  37. }
  38. };
  39. const inputTestChannelHandler = (channel) => {
  40. setTestChannel(channel);
  41. };
  42. useEffect(() => {
  43. const siteName = appContainer.config.crowi.title;
  44. setSiteName(siteName);
  45. }, [appContainer]);
  46. return (
  47. <>
  48. <h2 className="admin-setting-header">{t('admin:slack_integration.custom_bot_without_proxy_integration')}</h2>
  49. <CustomBotWithoutProxyIntegrationCard
  50. siteName={siteName}
  51. slackWSNameInWithoutProxy={props.slackWSNameInWithoutProxy}
  52. isIntegrationSuccess={isIntegrationSuccess}
  53. />
  54. <h2 className="admin-setting-header">{t('admin:slack_integration.integration_procedure')}</h2>
  55. {(props.slackSigningSecret || props.slackBotToken) && (
  56. <button
  57. className="mx-3 pull-right btn text-danger border-danger"
  58. type="button"
  59. onClick={() => setIsDeleteConfirmModalShown(true)}
  60. >{t('admin:slack_integration.reset')}
  61. </button>
  62. )}
  63. <div className="my-5 mx-3">
  64. <CustomBotWithoutProxySettingsAccordion
  65. {...props}
  66. activeStep={botInstallationStep.CREATE_BOT}
  67. connectionMessage={connectionMessage}
  68. connectionErrorCode={connectionErrorCode}
  69. isIntegrationSuccess={isIntegrationSuccess}
  70. testChannel={testChannel}
  71. onTestFormSubmitted={testConnection}
  72. inputTestChannelHandler={inputTestChannelHandler}
  73. />
  74. </div>
  75. <DeleteSlackBotSettingsModal
  76. isResetAll={false}
  77. isOpen={isDeleteConfirmModalShown}
  78. onClose={() => setIsDeleteConfirmModalShown(false)}
  79. onClickDeleteButton={resetSettings}
  80. />
  81. </>
  82. );
  83. };
  84. const CustomBotWithoutProxySettingsWrapper = withUnstatedContainers(CustomBotWithoutProxySettings, [AppContainer, AdminAppContainer]);
  85. CustomBotWithoutProxySettings.propTypes = {
  86. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  87. adminAppContainer: PropTypes.instanceOf(AdminAppContainer).isRequired,
  88. slackSigningSecret: PropTypes.string,
  89. slackSigningSecretEnv: PropTypes.string,
  90. slackBotToken: PropTypes.string,
  91. slackBotTokenEnv: PropTypes.string,
  92. isRgisterSlackCredentials: PropTypes.bool,
  93. isIntegrationSuccess: PropTypes.bool,
  94. slackWSNameInWithoutProxy: PropTypes.string,
  95. onResetSettings: PropTypes.func,
  96. };
  97. export default CustomBotWithoutProxySettingsWrapper;