SlackIntegration.jsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import React, { useState, useEffect, useCallback } from 'react';
  2. import PropTypes from 'prop-types';
  3. import AppContainer from '../../../services/AppContainer';
  4. import { withUnstatedContainers } from '../../UnstatedUtils';
  5. import { toastError } from '../../../util/apiNotification';
  6. import AccessTokenSettings from './AccessTokenSettings';
  7. import OfficialBotSettings from './OfficialBotSettings';
  8. import CustomBotWithoutProxySettings from './CustomBotWithoutProxySettings';
  9. import CustomBotWithProxySettings from './CustomBotWithProxySettings';
  10. import ConfirmBotChangeModal from './ConfirmBotChangeModal';
  11. const SlackIntegration = (props) => {
  12. const { appContainer } = props;
  13. const [currentBotType, setCurrentBotType] = useState(null);
  14. const [selectedBotType, setSelectedBotType] = useState(null);
  15. const fetchData = useCallback(async() => {
  16. try {
  17. console.log(await appContainer.apiv3.get('slack-integration/'));
  18. }
  19. catch (err) {
  20. toastError(err);
  21. }
  22. }, [appContainer]);
  23. useEffect(() => {
  24. fetchData();
  25. }, [fetchData]);
  26. const resetBotType = async() => {
  27. try {
  28. await appContainer.apiv3.put('slack-integration/custom-bot-without-proxy', {
  29. slackSigningSecret: '',
  30. slackBotToken: '',
  31. botType: '',
  32. });
  33. }
  34. catch (err) {
  35. toastError(err);
  36. }
  37. };
  38. const handleBotTypeSelect = (clickedBotType) => {
  39. if (clickedBotType === currentBotType) {
  40. return;
  41. }
  42. if (currentBotType === null) {
  43. setCurrentBotType(clickedBotType);
  44. return;
  45. }
  46. setSelectedBotType(clickedBotType);
  47. };
  48. const handleCancelBotChange = () => {
  49. setSelectedBotType(null);
  50. };
  51. const changeCurrentBotSettings = () => {
  52. resetBotType();
  53. setCurrentBotType(selectedBotType);
  54. setSelectedBotType(null);
  55. };
  56. let settingsComponent = null;
  57. switch (currentBotType) {
  58. case 'official-bot':
  59. settingsComponent = <OfficialBotSettings />;
  60. break;
  61. case 'custom-bot-without-proxy':
  62. settingsComponent = <CustomBotWithoutProxySettings />;
  63. break;
  64. case 'custom-bot-with-proxy':
  65. settingsComponent = <CustomBotWithProxySettings />;
  66. break;
  67. }
  68. return (
  69. <>
  70. <div className="container">
  71. <ConfirmBotChangeModal
  72. isOpen={selectedBotType != null}
  73. onConfirmClick={changeCurrentBotSettings}
  74. onCancelClick={handleCancelBotChange}
  75. />
  76. </div>
  77. <div className="row">
  78. <div className="col-lg-12">
  79. <h2 className="admin-setting-header">Access Token</h2>
  80. <AccessTokenSettings />
  81. </div>
  82. </div>
  83. <div className="row my-5">
  84. <div className="card-deck mx-auto">
  85. <div
  86. className={`card mx-3 py-5 rounded ${currentBotType === 'official-bot' ? 'border-info' : ''}`}
  87. onClick={() => handleBotTypeSelect('official-bot')}
  88. style={{ cursor: 'pointer' }}
  89. >
  90. <div className="card-body">
  91. <h5 className="card-title">Official Bot</h5>
  92. <p className="card-text">This is a wider card with supporting text below as a natural lead-in to additional content.</p>
  93. </div>
  94. </div>
  95. <div
  96. className={`card mx-3 py-5 rounded ${currentBotType === 'custom-bot-without-proxy' ? 'border-info' : ''}`}
  97. onClick={() => handleBotTypeSelect('custom-bot-without-proxy')}
  98. style={{ cursor: 'pointer' }}
  99. >
  100. <div className="card-body">
  101. <h5 className="card-title">Custom Bot (Without Proxy)</h5>
  102. <p className="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. </p>
  103. </div>
  104. </div>
  105. <div
  106. className={`card mx-3 py-5 rounded ${currentBotType === 'custom-bot-with-proxy' ? 'border-info' : ''}`}
  107. onClick={() => handleBotTypeSelect('custom-bot-with-proxy')}
  108. style={{ cursor: 'pointer' }}
  109. >
  110. <div className="card-body">
  111. <h5 className="card-title">Custom Bot (With Proxy)</h5>
  112. <p className="card-text">This is a wider card with supporting text below as a natural lead-in to additional content.</p>
  113. </div>
  114. </div>
  115. </div>
  116. </div>
  117. {settingsComponent}
  118. </>
  119. );
  120. };
  121. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  122. SlackIntegration.propTypes = {
  123. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  124. };
  125. export default SlackIntegrationWrapper;