CustomBotWithProxySettings.jsx 5.7 KB

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