SettingsCreditLimit.js 4.6 KB

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