GroupRatioSettings.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import { Button, Col, Form, Row, Spin } from '@douyinfe/semi-ui';
  3. import {
  4. compareObjects,
  5. API,
  6. showError,
  7. showSuccess,
  8. showWarning,
  9. verifyJSON,
  10. } from '../../../helpers';
  11. import { useTranslation } from 'react-i18next';
  12. export default function GroupRatioSettings(props) {
  13. const { t } = useTranslation();
  14. const [loading, setLoading] = useState(false);
  15. const [inputs, setInputs] = useState({
  16. GroupRatio: '',
  17. UserUsableGroups: '',
  18. });
  19. const refForm = useRef();
  20. const [inputsRow, setInputsRow] = useState(inputs);
  21. async function onSubmit() {
  22. try {
  23. await refForm.current
  24. .validate()
  25. .then(() => {
  26. const updateArray = compareObjects(inputs, inputsRow);
  27. if (!updateArray.length)
  28. return showWarning(t('你似乎并没有修改什么'));
  29. const requestQueue = updateArray.map((item) => {
  30. const value =
  31. typeof inputs[item.key] === 'boolean'
  32. ? String(inputs[item.key])
  33. : inputs[item.key];
  34. return API.put('/api/option/', { key: item.key, value });
  35. });
  36. setLoading(true);
  37. Promise.all(requestQueue)
  38. .then((res) => {
  39. if (res.includes(undefined)) {
  40. return showError(
  41. requestQueue.length > 1
  42. ? t('部分保存失败,请重试')
  43. : t('保存失败'),
  44. );
  45. }
  46. for (let i = 0; i < res.length; i++) {
  47. if (!res[i].data.success) {
  48. return showError(res[i].data.message);
  49. }
  50. }
  51. showSuccess(t('保存成功'));
  52. props.refresh();
  53. })
  54. .catch((error) => {
  55. console.error('Unexpected error:', error);
  56. showError(t('保存失败,请重试'));
  57. })
  58. .finally(() => {
  59. setLoading(false);
  60. });
  61. })
  62. .catch(() => {
  63. showError(t('请检查输入'));
  64. });
  65. } catch (error) {
  66. showError(t('请检查输入'));
  67. console.error(error);
  68. }
  69. }
  70. useEffect(() => {
  71. const currentInputs = {};
  72. for (let key in props.options) {
  73. if (Object.keys(inputs).includes(key)) {
  74. currentInputs[key] = props.options[key];
  75. }
  76. }
  77. setInputs(currentInputs);
  78. setInputsRow(structuredClone(currentInputs));
  79. refForm.current.setValues(currentInputs);
  80. }, [props.options]);
  81. return (
  82. <Spin spinning={loading}>
  83. <Form
  84. values={inputs}
  85. getFormApi={(formAPI) => (refForm.current = formAPI)}
  86. style={{ marginBottom: 15 }}
  87. >
  88. <Form.Section text={t('分组设置')}>
  89. <Row gutter={16}>
  90. <Col xs={24} sm={16}>
  91. <Form.TextArea
  92. label={t('分组倍率')}
  93. placeholder={t('为一个 JSON 文本,键为分组名称,值为倍率')}
  94. field={'GroupRatio'}
  95. autosize={{ minRows: 6, maxRows: 12 }}
  96. trigger='blur'
  97. stopValidateWithError
  98. rules={[
  99. {
  100. validator: (rule, value) => verifyJSON(value),
  101. message: t('不是合法的 JSON 字符串'),
  102. },
  103. ]}
  104. onChange={(value) =>
  105. setInputs({ ...inputs, GroupRatio: value })
  106. }
  107. />
  108. </Col>
  109. </Row>
  110. <Row gutter={16}>
  111. <Col xs={24} sm={16}>
  112. <Form.TextArea
  113. label={t('用户可选分组')}
  114. placeholder={t('为一个 JSON 文本,键为分组名称,值为分组描述')}
  115. field={'UserUsableGroups'}
  116. autosize={{ minRows: 6, maxRows: 12 }}
  117. trigger='blur'
  118. stopValidateWithError
  119. rules={[
  120. {
  121. validator: (rule, value) => verifyJSON(value),
  122. message: t('不是合法的 JSON 字符串'),
  123. },
  124. ]}
  125. onChange={(value) =>
  126. setInputs({ ...inputs, UserUsableGroups: value })
  127. }
  128. />
  129. </Col>
  130. </Row>
  131. </Form.Section>
  132. </Form>
  133. <Button onClick={onSubmit}>{t('保存分组倍率设置')}</Button>
  134. </Spin>
  135. );
  136. }