CustomBotWithProxySettings.jsx 4.7 KB

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