CustomBotWithProxySettings.jsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 mt-3">
  79. <input
  80. className="form-control"
  81. type="text"
  82. onChange={(e) => { setProxyUri(e.target.value) }}
  83. />
  84. </div>
  85. <div className="col-md-2 mt-3 text-center text-md-left">
  86. <button type="button" className="btn btn-primary" onClick={() => updateProxyUri} disabled={false}>{ t('Update') }</button>
  87. </div>
  88. </div>
  89. <h2 className="admin-setting-header">{t('admin:slack_integration.cooperation_procedure')}</h2>
  90. <div className="mx-3">
  91. {/* // TODO: Multiple accordion logic */}
  92. {Array(...Array(accordionComponentsCount)).map(i => (
  93. <>
  94. <div className="d-flex justify-content-end">
  95. <button
  96. className="my-3 btn btn-outline-danger"
  97. type="button"
  98. onClick={() => setIsDeleteConfirmModalShown(true)}
  99. >
  100. <i className="icon-trash mr-1" />
  101. {t('admin:slack_integration.delete')}
  102. </button>
  103. </div>
  104. <WithProxyAccordions botType="customBotWithProxy" key={i} />
  105. </>
  106. ))}
  107. {/* TODO: Disable button when integration is incomplete */}
  108. {/* TODO: i18n */}
  109. <div className="row justify-content-center my-5">
  110. <button
  111. type="button"
  112. className="btn btn-outline-primary"
  113. onClick={addAccordionHandler}
  114. >
  115. {`+ ${t('admin:slack_integration.accordion.add_slack_workspace')}`}
  116. </button>
  117. </div>
  118. </div>
  119. <DeleteSlackBotSettingsModal
  120. isResetAll={false}
  121. isOpen={isDeleteConfirmModalShown}
  122. onClose={() => setIsDeleteConfirmModalShown(false)}
  123. onClickDeleteButton={deleteSlackSettingsHandler}
  124. />
  125. </>
  126. );
  127. };
  128. const CustomBotWithProxySettingsWrapper = withUnstatedContainers(CustomBotWithProxySettings, [AppContainer]);
  129. CustomBotWithProxySettings.propTypes = {
  130. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  131. };
  132. export default CustomBotWithProxySettingsWrapper;