CustomBotWithProxySettings.jsx 5.5 KB

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