SettingsCheckin.jsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import React, { useEffect, useState, useRef } from 'react';
  16. import { Button, Col, Form, Row, Spin, Typography } from '@douyinfe/semi-ui';
  17. import {
  18. compareObjects,
  19. API,
  20. showError,
  21. showSuccess,
  22. showWarning,
  23. } from '../../../helpers';
  24. import { useTranslation } from 'react-i18next';
  25. export default function SettingsCheckin(props) {
  26. const { t } = useTranslation();
  27. const [loading, setLoading] = useState(false);
  28. const [inputs, setInputs] = useState({
  29. 'checkin_setting.enabled': false,
  30. 'checkin_setting.min_quota': 1000,
  31. 'checkin_setting.max_quota': 10000,
  32. });
  33. const refForm = useRef();
  34. const [inputsRow, setInputsRow] = useState(inputs);
  35. function handleFieldChange(fieldName) {
  36. return (value) => {
  37. setInputs((inputs) => ({ ...inputs, [fieldName]: value }));
  38. };
  39. }
  40. function onSubmit() {
  41. const updateArray = compareObjects(inputs, inputsRow);
  42. if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
  43. const requestQueue = updateArray.map((item) => {
  44. let value = '';
  45. if (typeof inputs[item.key] === 'boolean') {
  46. value = String(inputs[item.key]);
  47. } else {
  48. value = String(inputs[item.key]);
  49. }
  50. return API.put('/api/option/', {
  51. key: item.key,
  52. value,
  53. });
  54. });
  55. setLoading(true);
  56. Promise.all(requestQueue)
  57. .then((res) => {
  58. if (requestQueue.length === 1) {
  59. if (res.includes(undefined)) return;
  60. } else if (requestQueue.length > 1) {
  61. if (res.includes(undefined))
  62. return showError(t('部分保存失败,请重试'));
  63. }
  64. showSuccess(t('保存成功'));
  65. props.refresh();
  66. })
  67. .catch(() => {
  68. showError(t('保存失败,请重试'));
  69. })
  70. .finally(() => {
  71. setLoading(false);
  72. });
  73. }
  74. useEffect(() => {
  75. const currentInputs = {};
  76. for (let key in props.options) {
  77. if (Object.keys(inputs).includes(key)) {
  78. currentInputs[key] = props.options[key];
  79. }
  80. }
  81. setInputs(currentInputs);
  82. setInputsRow(structuredClone(currentInputs));
  83. refForm.current.setValues(currentInputs);
  84. }, [props.options]);
  85. return (
  86. <>
  87. <Spin spinning={loading}>
  88. <Form
  89. values={inputs}
  90. getFormApi={(formAPI) => (refForm.current = formAPI)}
  91. style={{ marginBottom: 15 }}
  92. >
  93. <Form.Section text={t('签到设置')}>
  94. <Typography.Text
  95. type='tertiary'
  96. style={{ marginBottom: 16, display: 'block' }}
  97. >
  98. {t('签到功能允许用户每日签到获取随机额度奖励')}
  99. </Typography.Text>
  100. <Row gutter={16}>
  101. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  102. <Form.Switch
  103. field={'checkin_setting.enabled'}
  104. label={t('启用签到功能')}
  105. size='default'
  106. checkedText='|'
  107. uncheckedText='〇'
  108. onChange={handleFieldChange('checkin_setting.enabled')}
  109. />
  110. </Col>
  111. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  112. <Form.InputNumber
  113. field={'checkin_setting.min_quota'}
  114. label={t('签到最小额度')}
  115. placeholder={t('签到奖励的最小额度')}
  116. onChange={handleFieldChange('checkin_setting.min_quota')}
  117. min={0}
  118. disabled={!inputs['checkin_setting.enabled']}
  119. />
  120. </Col>
  121. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  122. <Form.InputNumber
  123. field={'checkin_setting.max_quota'}
  124. label={t('签到最大额度')}
  125. placeholder={t('签到奖励的最大额度')}
  126. onChange={handleFieldChange('checkin_setting.max_quota')}
  127. min={0}
  128. disabled={!inputs['checkin_setting.enabled']}
  129. />
  130. </Col>
  131. </Row>
  132. <Row>
  133. <Button size='default' onClick={onSubmit}>
  134. {t('保存签到设置')}
  135. </Button>
  136. </Row>
  137. </Form.Section>
  138. </Form>
  139. </Spin>
  140. </>
  141. );
  142. }