CustomBotWithProxySettings.jsx 6.2 KB

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