CustomBotWithProxySettings.jsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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('');
  21. const [tokenGtoP, setTokenGtoP] = useState('');
  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. // const response = await appContainer.apiv3.delete('/slack-integration-settings/slack-app-integration',
  51. // { tokenGtoP, tokenPtoG });
  52. console.log('asdfasdf');
  53. };
  54. const generateTokenHandler = async() => {
  55. // const response = await appContainer.apiv3.put('/slack-integration-settings/access-tokens');
  56. console.log(response);
  57. setTokenGtoP('setAccessTokenForProxy');
  58. setTokenPtoG('tokenForProxy');
  59. };
  60. const deleteSlackSettingsHandler = async() => {
  61. try {
  62. // TODO imple delete PtoG and GtoP Token at GW 5861
  63. await appContainer.apiv3.put('/slack-integration-settings/custom-bot-with-proxy', {
  64. });
  65. deleteAccordionHandler();
  66. toastSuccess('success');
  67. }
  68. catch (err) {
  69. toastError(err);
  70. }
  71. };
  72. const updateProxyUri = async() => {
  73. try {
  74. await appContainer.apiv3.put('/slack-integration-settings/proxy-uri', {
  75. proxyUri,
  76. });
  77. toastSuccess(t('toaster.update_successed', { target: t('Proxy URL') }));
  78. }
  79. catch (err) {
  80. toastError(err);
  81. logger.error(err);
  82. }
  83. };
  84. return (
  85. <>
  86. <h2 className="admin-setting-header mb-2">{t('admin:slack_integration.custom_bot_with_proxy_integration')}</h2>
  87. {/* TODO delete tmp props */}
  88. <CustomBotWithProxyIntegrationCard
  89. growiApps={
  90. [
  91. { name: 'siteName1', active: true },
  92. { name: 'siteName2', active: false },
  93. { name: 'siteName3', active: false },
  94. ]
  95. }
  96. slackWorkSpaces={
  97. [
  98. { name: 'wsName1', active: true },
  99. { name: 'wsName2', active: false },
  100. ]
  101. }
  102. isSlackScopeSet
  103. />
  104. <div className="form-group row my-4">
  105. <label className="text-left text-md-right col-md-3 col-form-label mt-3">Proxy URL</label>
  106. <div className="col-md-6 mt-3">
  107. <input
  108. className="form-control"
  109. type="text"
  110. name="settingForm[proxyUrl]"
  111. defaultValue={proxyUri}
  112. onChange={(e) => { setProxyUri(e.target.value) }}
  113. />
  114. </div>
  115. <div className="col-md-2 mt-3 text-center text-md-left">
  116. <button type="button" className="btn btn-primary" onClick={updateProxyUri}>{ t('Update') }</button>
  117. </div>
  118. </div>
  119. <h2 className="admin-setting-header">{t('admin:slack_integration.integration_procedure')}</h2>
  120. <div className="mx-3">
  121. {/* // TODO: Multiple accordion logic */}
  122. {Array(...Array(accordionComponentsCount)).map(i => (
  123. <>
  124. <div
  125. className="d-flex justify-content-end"
  126. key={i}
  127. >
  128. <button
  129. className="my-3 btn btn-outline-danger"
  130. type="button"
  131. onClick={() => setIsDeleteConfirmModalShown(true)}
  132. >
  133. <i className="icon-trash mr-1" />
  134. {t('admin:slack_integration.delete')}
  135. </button>
  136. </div>
  137. <WithProxyAccordions
  138. botType="customBotWithProxy"
  139. discardTokenHandler={discardTokenHandler}
  140. generateTokenHandler={generateTokenHandler}
  141. // TODO: Multiple accordion logic
  142. tokenPtoG={tokenPtoG}
  143. tokenGtoP={tokenGtoP}
  144. />
  145. </>
  146. ))}
  147. {/* TODO: Disable button when integration is incomplete */}
  148. {/* TODO: i18n */}
  149. <div className="row justify-content-center my-5">
  150. <button
  151. type="button"
  152. className="btn btn-outline-primary"
  153. onClick={addAccordionHandler}
  154. >
  155. {`+ ${t('admin:slack_integration.accordion.add_slack_workspace')}`}
  156. </button>
  157. </div>
  158. </div>
  159. <DeleteSlackBotSettingsModal
  160. isResetAll={false}
  161. isOpen={isDeleteConfirmModalShown}
  162. onClose={() => setIsDeleteConfirmModalShown(false)}
  163. onClickDeleteButton={deleteSlackSettingsHandler}
  164. />
  165. </>
  166. );
  167. };
  168. const CustomBotWithProxySettingsWrapper = withUnstatedContainers(CustomBotWithProxySettings, [AppContainer]);
  169. CustomBotWithProxySettings.propTypes = {
  170. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  171. };
  172. export default CustomBotWithProxySettingsWrapper;