CustomBotWithoutProxySettings.jsx 4.1 KB

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