CustomBotWithProxySettings.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import React, { useState } 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. import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow';
  12. const logger = loggerFactory('growi:SlackBotSettings');
  13. const CustomBotWithProxySettings = (props) => {
  14. // eslint-disable-next-line no-unused-vars
  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 [accordionComponentsCount, setAccordionComponentsCount] = useState(0);
  21. const addAccordionHandler = () => {
  22. setAccordionComponentsCount(
  23. prevState => prevState + 1,
  24. );
  25. };
  26. // TODO: Delete accordion logic
  27. const deleteAccordionHandler = () => {
  28. setAccordionComponentsCount(
  29. prevState => prevState - 1,
  30. );
  31. };
  32. const deleteSlackSettingsHandler = async() => {
  33. try {
  34. // TODO imple delete PtoG and GtoP Token at GW 5861
  35. await appContainer.apiv3.put('/slack-integration-settings/custom-bot-with-proxy', {
  36. });
  37. deleteAccordionHandler();
  38. toastSuccess('success');
  39. }
  40. catch (err) {
  41. toastError(err);
  42. }
  43. };
  44. const updateProxyUri = async() => {
  45. try {
  46. await appContainer.apiv3.put('/slack-integration-settings/proxy-uri', {
  47. proxyUri,
  48. });
  49. toastSuccess(t('toaster.update_successed', { target: t('Proxy URL') }));
  50. }
  51. catch (err) {
  52. toastError(err);
  53. logger.error(err);
  54. }
  55. };
  56. return (
  57. <>
  58. <h2 className="admin-setting-header mb-2">{t('admin:slack_integration.custom_bot_with_proxy_integration')}</h2>
  59. {/* TODO delete tmp props */}
  60. <CustomBotWithProxyIntegrationCard
  61. growiApps={
  62. [
  63. { name: 'siteName1', active: true },
  64. { name: 'siteName2', active: false },
  65. { name: 'siteName3', active: false },
  66. ]
  67. }
  68. slackWorkSpaces={
  69. [
  70. { name: 'wsName1', active: true },
  71. { name: 'wsName2', active: false },
  72. ]
  73. }
  74. isSlackScopeSet
  75. />
  76. <div className="form-group row my-4">
  77. <label className="text-left text-md-right col-md-3 col-form-label mt-3">Proxy URL</label>
  78. <div className="col-md-6 mr-3 mt-3">
  79. <input
  80. className="form-control"
  81. type="text"
  82. onChange={(e) => { setProxyUri(e.target.value) }}
  83. />
  84. </div>
  85. <AdminUpdateButtonRow
  86. disabled={false}
  87. onClick={() => updateProxyUri()}
  88. />
  89. </div>
  90. <h2 className="admin-setting-header">{t('admin:slack_integration.cooperation_procedure')}</h2>
  91. <div className="mx-3">
  92. {/* // TODO: Multiple accordion logic */}
  93. {Array(...Array(accordionComponentsCount)).map(i => (
  94. <>
  95. <div className="d-flex justify-content-end">
  96. <button
  97. className="my-3 btn btn-outline-danger"
  98. type="button"
  99. onClick={() => setIsDeleteConfirmModalShown(true)}
  100. >
  101. <i className="icon-trash mr-1" />
  102. {t('admin:slack_integration.delete')}
  103. </button>
  104. </div>
  105. <WithProxyAccordions botType="customBotWithProxy" key={i} />
  106. </>
  107. ))}
  108. {/* TODO: Disable button when integration is incomplete */}
  109. {/* TODO: i18n */}
  110. <div className="row justify-content-center my-5">
  111. <button
  112. type="button"
  113. className="btn btn-outline-primary"
  114. onClick={addAccordionHandler}
  115. >
  116. {`+ ${t('admin:slack_integration.accordion.add_slack_workspace')}`}
  117. </button>
  118. </div>
  119. </div>
  120. <DeleteSlackBotSettingsModal
  121. isResetAll={false}
  122. isOpen={isDeleteConfirmModalShown}
  123. onClose={() => setIsDeleteConfirmModalShown(false)}
  124. onClickDeleteButton={deleteSlackSettingsHandler}
  125. />
  126. </>
  127. );
  128. };
  129. const CustomBotWithProxySettingsWrapper = withUnstatedContainers(CustomBotWithProxySettings, [AppContainer]);
  130. CustomBotWithProxySettings.propTypes = {
  131. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  132. };
  133. export default CustomBotWithProxySettingsWrapper;