CustomBotWithProxySettings.jsx 5.8 KB

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