CustomBotWithProxySettings.jsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import React, { useState, useEffect } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import PropTypes from 'prop-types';
  4. import loggerFactory from '@alias/logger';
  5. import AppContainer from '../../../services/AppContainer';
  6. import { withUnstatedContainers } from '../../UnstatedUtils';
  7. import { toastSuccess, toastError } from '../../../util/apiNotification';
  8. import CustomBotWithProxyIntegrationCard from './CustomBotWithProxyIntegrationCard';
  9. import WithProxyAccordions from './WithProxyAccordions';
  10. import DeleteSlackBotSettingsModal from './DeleteSlackBotSettingsModal';
  11. const logger = loggerFactory('growi:SlackBotSettings');
  12. const CustomBotWithProxySettings = (props) => {
  13. const {
  14. appContainer, slackAppIntegrations, proxyServerUri, fetchSlackIntegrationData,
  15. } = props;
  16. const [isDeleteConfirmModalShown, setIsDeleteConfirmModalShown] = useState(false);
  17. const { t } = useTranslation();
  18. const [newProxyServerUri, setNewProxyServerUri] = useState();
  19. useEffect(() => {
  20. if (proxyServerUri != null) {
  21. setNewProxyServerUri(proxyServerUri);
  22. }
  23. }, [proxyServerUri]);
  24. const addSlackAppIntegrationHandler = async() => {
  25. try {
  26. await appContainer.apiv3.put('/slack-integration-settings/slack-app-integrations');
  27. if (fetchSlackIntegrationData == null) {
  28. return;
  29. }
  30. fetchSlackIntegrationData();
  31. }
  32. catch (err) {
  33. toastError(err);
  34. logger(err);
  35. }
  36. };
  37. const discardTokenHandler = async(tokenGtoP, tokenPtoG) => {
  38. try {
  39. // GW-6068 set new value after this
  40. await appContainer.apiv3.delete('/slack-integration-settings/slack-app-integration', { tokenGtoP, tokenPtoG });
  41. }
  42. catch (err) {
  43. toastError(err);
  44. logger(err);
  45. }
  46. };
  47. const generateTokenHandler = async() => {
  48. try {
  49. // GW-6068 set new value after this
  50. await appContainer.apiv3.put('/slack-integration-settings/slack-app-integrations');
  51. }
  52. catch (err) {
  53. toastError(err);
  54. logger(err);
  55. }
  56. };
  57. const deleteSlackAppIntegrationHandler = async() => {
  58. try {
  59. // TODO GW-5923 delete SlackAppIntegration
  60. // await appContainer.apiv3.put('/slack-integration-settings/custom-bot-with-proxy');
  61. toastSuccess('success');
  62. }
  63. catch (err) {
  64. toastError(err);
  65. }
  66. };
  67. const updateProxyUri = async() => {
  68. try {
  69. await appContainer.apiv3.put('/slack-integration-settings/proxy-uri', {
  70. proxyUri: newProxyServerUri,
  71. });
  72. toastSuccess(t('toaster.update_successed', { target: t('Proxy URL') }));
  73. }
  74. catch (err) {
  75. toastError(err);
  76. logger.error(err);
  77. }
  78. };
  79. return (
  80. <>
  81. <h2 className="admin-setting-header mb-2">{t('admin:slack_integration.custom_bot_with_proxy_integration')}</h2>
  82. {/* TODO delete tmp props */}
  83. <CustomBotWithProxyIntegrationCard
  84. growiApps={
  85. [
  86. { name: 'siteName1', active: true },
  87. { name: 'siteName2', active: false },
  88. { name: 'siteName3', active: false },
  89. ]
  90. }
  91. slackWorkSpaces={
  92. [
  93. { name: 'wsName1', active: true },
  94. { name: 'wsName2', active: false },
  95. ]
  96. }
  97. isSlackScopeSet
  98. />
  99. <div className="form-group row my-4">
  100. <label className="text-left text-md-right col-md-3 col-form-label mt-3">Proxy URL</label>
  101. <div className="col-md-6 mt-3">
  102. <input
  103. className="form-control"
  104. type="text"
  105. name="settingForm[proxyUrl]"
  106. defaultValue={newProxyServerUri}
  107. onChange={(e) => { setNewProxyServerUri(e.target.value) }}
  108. />
  109. </div>
  110. <div className="col-md-2 mt-3 text-center text-md-left">
  111. <button type="button" className="btn btn-primary" onClick={updateProxyUri}>{ t('Update') }</button>
  112. </div>
  113. </div>
  114. <h2 className="admin-setting-header">{t('admin:slack_integration.integration_procedure')}</h2>
  115. <div className="mx-3">
  116. {slackAppIntegrations.map((slackAppIntegration) => {
  117. const { tokenGtoP, tokenPtoG } = slackAppIntegration;
  118. return (
  119. <React.Fragment key={slackAppIntegration.id}>
  120. <div className="d-flex justify-content-end">
  121. <button
  122. className="my-3 btn btn-outline-danger"
  123. type="button"
  124. onClick={() => setIsDeleteConfirmModalShown(true)}
  125. >
  126. <i className="icon-trash mr-1" />
  127. {t('admin:slack_integration.delete')}
  128. </button>
  129. </div>
  130. <WithProxyAccordions
  131. botType="customBotWithProxy"
  132. discardTokenHandler={() => discardTokenHandler(tokenGtoP, tokenPtoG)}
  133. generateTokenHandler={generateTokenHandler}
  134. tokenGtoP={tokenGtoP}
  135. tokenPtoG={tokenPtoG}
  136. />
  137. </React.Fragment>
  138. );
  139. })}
  140. <div className="row justify-content-center my-5">
  141. <button
  142. type="button"
  143. className="btn btn-outline-primary"
  144. onClick={addSlackAppIntegrationHandler}
  145. >
  146. {`+ ${t('admin:slack_integration.accordion.add_slack_workspace')}`}
  147. </button>
  148. </div>
  149. </div>
  150. <DeleteSlackBotSettingsModal
  151. isResetAll={false}
  152. isOpen={isDeleteConfirmModalShown}
  153. onClose={() => setIsDeleteConfirmModalShown(false)}
  154. onClickDeleteButton={deleteSlackAppIntegrationHandler}
  155. />
  156. </>
  157. );
  158. };
  159. const CustomBotWithProxySettingsWrapper = withUnstatedContainers(CustomBotWithProxySettings, [AppContainer]);
  160. CustomBotWithProxySettings.defaultProps = {
  161. slackAppIntegrations: [],
  162. };
  163. CustomBotWithProxySettings.propTypes = {
  164. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  165. slackAppIntegrations: PropTypes.array,
  166. proxyServerUri: PropTypes.string,
  167. fetchSlackIntegrationData: PropTypes.func,
  168. };
  169. export default CustomBotWithProxySettingsWrapper;