CustomBotWithoutProxySettings.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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')}
  47. {/* TODO: add an appropriate links by GW-5614 */}
  48. <i className="fa fa-external-link btn-link ml-2" aria-hidden="true"></i>
  49. </h2>
  50. <CustomBotWithoutProxyConnectionStatus
  51. siteName={siteName}
  52. connectionStatuses={connectionStatuses}
  53. />
  54. <h2 className="admin-setting-header">{t('admin:slack_integration.integration_procedure')}</h2>
  55. <div className={(props.slackSigningSecret != null || props.slackBotToken != null) ? 'px-3 mb-5' : 'px-3 my-5'}>
  56. {(props.slackSigningSecret != null || props.slackBotToken != null) && (
  57. <div className="d-flex justify-content-end my-4">
  58. <button
  59. className="btn text-danger border-danger"
  60. type="button"
  61. onClick={() => setIsDeleteConfirmModalShown(true)}
  62. >{t('admin:slack_integration.reset')}
  63. </button>
  64. </div>
  65. )}
  66. {/* {isConnectedFailed && (<>Settings #1 <span className="text-danger">{t('admin:slack_integration.integration_failed')}</span></>)} */}
  67. <CustomBotWithoutProxySettingsAccordion
  68. {...props}
  69. activeStep={botInstallationStep.CREATE_BOT}
  70. connectionMessage={connectionMessage}
  71. connectionErrorCode={connectionErrorCode}
  72. isIntegrationSuccess={isIntegrationSuccess}
  73. testChannel={testChannel}
  74. onTestFormSubmitted={testConnection}
  75. inputTestChannelHandler={inputTestChannelHandler}
  76. />
  77. </div>
  78. <DeleteSlackBotSettingsModal
  79. isResetAll={false}
  80. isOpen={isDeleteConfirmModalShown}
  81. onClose={() => setIsDeleteConfirmModalShown(false)}
  82. onClickDeleteButton={resetSettings}
  83. />
  84. </>
  85. );
  86. };
  87. const CustomBotWithoutProxySettingsWrapper = withUnstatedContainers(CustomBotWithoutProxySettings, [AppContainer, AdminAppContainer]);
  88. CustomBotWithoutProxySettings.propTypes = {
  89. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  90. adminAppContainer: PropTypes.instanceOf(AdminAppContainer).isRequired,
  91. slackSigningSecret: PropTypes.string,
  92. slackSigningSecretEnv: PropTypes.string,
  93. slackBotToken: PropTypes.string,
  94. slackBotTokenEnv: PropTypes.string,
  95. isRgisterSlackCredentials: PropTypes.bool,
  96. isIntegrationSuccess: PropTypes.bool,
  97. slackWSNameInWithoutProxy: PropTypes.string,
  98. onResetSettings: PropTypes.func,
  99. connectionStatuses: PropTypes.object.isRequired,
  100. };
  101. export default CustomBotWithoutProxySettingsWrapper;