SettingsCreditLimit.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import { Button, Col, Form, Row, Spin } from '@douyinfe/semi-ui';
  3. import { useTranslation } from 'react-i18next';
  4. import {
  5. compareObjects,
  6. API,
  7. showError,
  8. showSuccess,
  9. showWarning,
  10. } from '../../../helpers';
  11. export default function SettingsCreditLimit(props) {
  12. const { t } = useTranslation();
  13. const [loading, setLoading] = useState(false);
  14. const [inputs, setInputs] = useState({
  15. QuotaForNewUser: '',
  16. PreConsumedQuota: '',
  17. QuotaForInviter: '',
  18. QuotaForInvitee: '',
  19. });
  20. const refForm = useRef();
  21. const [inputsRow, setInputsRow] = useState(inputs);
  22. function onSubmit() {
  23. const updateArray = compareObjects(inputs, inputsRow);
  24. if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
  25. const requestQueue = updateArray.map((item) => {
  26. let value = '';
  27. if (typeof inputs[item.key] === 'boolean') {
  28. value = String(inputs[item.key]);
  29. } else {
  30. value = inputs[item.key];
  31. }
  32. return API.put('/api/option/', {
  33. key: item.key,
  34. value,
  35. });
  36. });
  37. setLoading(true);
  38. Promise.all(requestQueue)
  39. .then((res) => {
  40. if (requestQueue.length === 1) {
  41. if (res.includes(undefined)) return;
  42. } else if (requestQueue.length > 1) {
  43. if (res.includes(undefined))
  44. return showError(t('部分保存失败,请重试'));
  45. }
  46. showSuccess(t('保存成功'));
  47. props.refresh();
  48. })
  49. .catch(() => {
  50. showError(t('保存失败,请重试'));
  51. })
  52. .finally(() => {
  53. setLoading(false);
  54. });
  55. }
  56. useEffect(() => {
  57. const currentInputs = {};
  58. for (let key in props.options) {
  59. if (Object.keys(inputs).includes(key)) {
  60. currentInputs[key] = props.options[key];
  61. }
  62. }
  63. setInputs(currentInputs);
  64. setInputsRow(structuredClone(currentInputs));
  65. refForm.current.setValues(currentInputs);
  66. }, [props.options]);
  67. return (
  68. <>
  69. <Spin spinning={loading}>
  70. <Form
  71. values={inputs}
  72. getFormApi={(formAPI) => (refForm.current = formAPI)}
  73. style={{ marginBottom: 15 }}
  74. >
  75. <Form.Section text={t('额度设置')}>
  76. <Row gutter={16}>
  77. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  78. <Form.InputNumber
  79. label={t('新用户初始额度')}
  80. field={'QuotaForNewUser'}
  81. step={1}
  82. min={0}
  83. suffix={'Token'}
  84. placeholder={''}
  85. onChange={(value) =>
  86. setInputs({
  87. ...inputs,
  88. QuotaForNewUser: String(value),
  89. })
  90. }
  91. />
  92. </Col>
  93. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  94. <Form.InputNumber
  95. label={t('请求预扣费额度')}
  96. field={'PreConsumedQuota'}
  97. step={1}
  98. min={0}
  99. suffix={'Token'}
  100. extraText={t('请求结束后多退少补')}
  101. placeholder={''}
  102. onChange={(value) =>
  103. setInputs({
  104. ...inputs,
  105. PreConsumedQuota: String(value),
  106. })
  107. }
  108. />
  109. </Col>
  110. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  111. <Form.InputNumber
  112. label={t('邀请新用户奖励额度')}
  113. field={'QuotaForInviter'}
  114. step={1}
  115. min={0}
  116. suffix={'Token'}
  117. extraText={''}
  118. placeholder={t('例如:2000')}
  119. onChange={(value) =>
  120. setInputs({
  121. ...inputs,
  122. QuotaForInviter: String(value),
  123. })
  124. }
  125. />
  126. </Col>
  127. </Row>
  128. <Row>
  129. <Col xs={24} sm={12} md={8} lg={8} xl={6}>
  130. <Form.InputNumber
  131. label={t('新用户使用邀请码奖励额度')}
  132. field={'QuotaForInvitee'}
  133. step={1}
  134. min={0}
  135. suffix={'Token'}
  136. extraText={''}
  137. placeholder={t('例如:1000')}
  138. onChange={(value) =>
  139. setInputs({
  140. ...inputs,
  141. QuotaForInvitee: String(value),
  142. })
  143. }
  144. />
  145. </Col>
  146. </Row>
  147. <Row>
  148. <Button size='default' onClick={onSubmit}>
  149. {t('保存额度设置')}
  150. </Button>
  151. </Row>
  152. </Form.Section>
  153. </Form>
  154. </Spin>
  155. </>
  156. );
  157. }