| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- import React, { useState, useEffect, useCallback } from 'react';
- import PropTypes from 'prop-types';
- import { useTranslation } from 'react-i18next';
- import AppContainer from '../../../services/AppContainer';
- import { withUnstatedContainers } from '../../UnstatedUtils';
- import { toastSuccess, toastError } from '../../../util/apiNotification';
- import AccessTokenSettings from './AccessTokenSettings';
- import OfficialBotSettings from './OfficialBotSettings';
- import CustomBotWithoutProxySettings from './CustomBotWithoutProxySettings';
- import CustomBotWithProxySettings from './CustomBotWithProxySettings';
- import ConfirmBotChangeModal from './ConfirmBotChangeModal';
- const SlackIntegration = (props) => {
- const { appContainer } = props;
- const { t } = useTranslation();
- const [currentBotType, setCurrentBotType] = useState(null);
- const [selectedBotType, setSelectedBotType] = useState(null);
- const fetchData = useCallback(async() => {
- try {
- const response = await appContainer.apiv3.get('slack-integration/');
- const { currentBotType } = response.data.slackBotSettingParams;
- setCurrentBotType(currentBotType);
- }
- catch (err) {
- toastError(err);
- }
- }, [appContainer.apiv3]);
- useEffect(() => {
- fetchData();
- }, [fetchData]);
- const resetBotType = async() => {
- try {
- await appContainer.apiv3.put('slack-integration/custom-bot-without-proxy', {
- slackSigningSecret: '',
- slackBotToken: '',
- botType: selectedBotType,
- });
- }
- catch (err) {
- toastError(err);
- }
- };
- const handleBotTypeSelect = (clickedBotType) => {
- if (clickedBotType === currentBotType) {
- return;
- }
- if (currentBotType === null) {
- setCurrentBotType(clickedBotType);
- return;
- }
- setSelectedBotType(clickedBotType);
- };
- const handleCancelBotChange = () => {
- setSelectedBotType(null);
- };
- const handleChangeCurrentBotSettings = async() => {
- try {
- const res = await appContainer.apiv3.put('slack-integration/custom-bot-without-proxy', {
- slackSigningSecret: '',
- slackBotToken: '',
- botType: selectedBotType,
- });
- setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType);
- setSelectedBotType(null);
- toastSuccess(t('admin:slack_integration.bot_reset_successful'));
- }
- catch (err) {
- toastError(err);
- }
- };
- let settingsComponent = null;
- switch (currentBotType) {
- case 'official-bot':
- settingsComponent = <OfficialBotSettings />;
- break;
- case 'custom-bot-without-proxy':
- settingsComponent = <CustomBotWithoutProxySettings />;
- break;
- case 'custom-bot-with-proxy':
- settingsComponent = <CustomBotWithProxySettings />;
- break;
- }
- return (
- <>
- <div className="container">
- <ConfirmBotChangeModal
- isOpen={selectedBotType != null}
- onConfirmClick={handleChangeCurrentBotSettings}
- onCancelClick={handleCancelBotChange}
- />
- </div>
- <div className="row">
- <div className="col-lg-12">
- <h2 className="admin-setting-header">Access Token</h2>
- <AccessTokenSettings />
- </div>
- </div>
- <div className="row my-5">
- <div className="card-deck mx-auto">
- <div
- className={`card admin-bot-card mx-3 py-5 rounded ${currentBotType === 'official-bot' ? 'border-info' : ''}`}
- onClick={() => handleBotTypeSelect('official-bot')}
- >
- <div className="card-body">
- <h5 className="card-title">Official Bot</h5>
- <p className="card-text">This is a wider card with supporting text below as a natural lead-in to additional content.</p>
- </div>
- </div>
- <div
- className={`card admin-bot-card mx-3 py-5 rounded ${currentBotType === 'custom-bot-without-proxy' ? 'border-info' : ''}`}
- onClick={() => handleBotTypeSelect('custom-bot-without-proxy')}
- >
- <div className="card-body">
- <h5 className="card-title">Custom Bot (Without Proxy)</h5>
- <p className="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. </p>
- </div>
- </div>
- <div
- className={`card admin-bot-card mx-3 py-5 rounded ${currentBotType === 'custom-bot-with-proxy' ? 'border-info' : ''}`}
- onClick={() => handleBotTypeSelect('custom-bot-with-proxy')}
- >
- <div className="card-body">
- <h5 className="card-title">Custom Bot (With Proxy)</h5>
- <p className="card-text">This is a wider card with supporting text below as a natural lead-in to additional content.</p>
- </div>
- </div>
- </div>
- </div>
- {settingsComponent}
- </>
- );
- };
- const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
- SlackIntegration.propTypes = {
- appContainer: PropTypes.instanceOf(AppContainer).isRequired,
- };
- export default SlackIntegrationWrapper;
|