CustomBotWithoutProxySecretTokenSection.jsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import React, { useState, useEffect } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import PropTypes from 'prop-types';
  4. import { apiv3Put } from '~/client/util/apiv3-client';
  5. import { toastSuccess, toastError } from '~/client/util/toastr';
  6. import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow';
  7. const CustomBotWithoutProxySecretTokenSection = (props) => {
  8. const {
  9. slackSigningSecret, slackBotToken, slackSigningSecretEnv, slackBotTokenEnv, onUpdatedSecretToken,
  10. } = props;
  11. const { t } = useTranslation();
  12. const [inputSigningSecret, setInputSigningSecret] = useState(slackSigningSecret || '');
  13. const [inputBotToken, setInputBotToken] = useState(slackBotToken || '');
  14. // update states when props are updated
  15. useEffect(() => {
  16. setInputSigningSecret(slackSigningSecret || '');
  17. }, [slackSigningSecret]);
  18. useEffect(() => {
  19. setInputBotToken(slackBotToken || '');
  20. }, [slackBotToken]);
  21. const updatedSecretToken = async() => {
  22. try {
  23. await apiv3Put('/slack-integration-settings/without-proxy/update-settings', {
  24. slackSigningSecret: inputSigningSecret,
  25. slackBotToken: inputBotToken,
  26. });
  27. if (onUpdatedSecretToken != null) {
  28. onUpdatedSecretToken(inputSigningSecret, inputBotToken);
  29. }
  30. toastSuccess(t('toaster.update_successed', { target: t('admin:slack_integration.custom_bot_without_proxy_settings'), ns: 'commons' }));
  31. }
  32. catch (err) {
  33. toastError(err);
  34. }
  35. };
  36. return (
  37. <div className="w-75 mx-auto">
  38. <h3>Signing Secret</h3>
  39. <div className="row">
  40. <div className="col-sm">
  41. <p>Database</p>
  42. <input
  43. className="form-control"
  44. type="text"
  45. value={inputSigningSecret}
  46. onChange={e => setInputSigningSecret(e.target.value)}
  47. />
  48. </div>
  49. <div className="col-sm">
  50. <p>Environment variables</p>
  51. <input
  52. className="form-control"
  53. type="text"
  54. defaultValue={slackSigningSecretEnv}
  55. readOnly
  56. />
  57. <p className="form-text text-muted">
  58. {/* eslint-disable-next-line max-len, react/no-danger */}
  59. <small dangerouslySetInnerHTML={{ __html: t('admin:slack_integration.use_env_var_if_empty', { variable: 'SLACKBOT_WITHOUT_PROXY_SIGNING_SECRET' }) }} />
  60. </p>
  61. </div>
  62. </div>
  63. <h3>Bot User OAuth Token</h3>
  64. <div className="row">
  65. <div className="col-sm">
  66. <p>Database</p>
  67. <input
  68. className="form-control"
  69. type="text"
  70. value={inputBotToken}
  71. onChange={e => setInputBotToken(e.target.value)}
  72. />
  73. </div>
  74. <div className="col-sm">
  75. <p>Environment variables</p>
  76. <input
  77. className="form-control"
  78. type="text"
  79. defaultValue={slackBotTokenEnv}
  80. readOnly
  81. />
  82. <p className="form-text text-muted">
  83. {/* eslint-disable-next-line react/no-danger */}
  84. <small dangerouslySetInnerHTML={{ __html: t('admin:slack_integration.use_env_var_if_empty', { variable: 'SLACKBOT_WITHOUT_PROXY_BOT_TOKEN' }) }} />
  85. </p>
  86. </div>
  87. </div>
  88. <AdminUpdateButtonRow onClick={updatedSecretToken} disabled={false} />
  89. </div>
  90. );
  91. };
  92. CustomBotWithoutProxySecretTokenSection.propTypes = {
  93. onUpdatedSecretToken: PropTypes.func,
  94. slackSigningSecret: PropTypes.string,
  95. slackSigningSecretEnv: PropTypes.string,
  96. slackBotToken: PropTypes.string,
  97. slackBotTokenEnv: PropTypes.string,
  98. };
  99. export default CustomBotWithoutProxySecretTokenSection;