EditRedemption.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import {
  4. API,
  5. downloadTextAsFile,
  6. isMobile,
  7. showError,
  8. showSuccess,
  9. renderQuota,
  10. renderQuotaWithPrompt,
  11. } from '../../helpers';
  12. import {
  13. Button,
  14. Modal,
  15. SideSheet,
  16. Space,
  17. Spin,
  18. Typography,
  19. Card,
  20. Tag,
  21. Form,
  22. Avatar,
  23. Row,
  24. Col,
  25. } from '@douyinfe/semi-ui';
  26. import {
  27. IconCreditCard,
  28. IconSave,
  29. IconClose,
  30. IconGift,
  31. } from '@douyinfe/semi-icons';
  32. const { Text, Title } = Typography;
  33. const EditRedemption = (props) => {
  34. const { t } = useTranslation();
  35. const isEdit = props.editingRedemption.id !== undefined;
  36. const [loading, setLoading] = useState(isEdit);
  37. const formApiRef = useRef(null);
  38. const getInitValues = () => ({
  39. name: '',
  40. quota: 100000,
  41. count: 1,
  42. expired_time: null,
  43. });
  44. const handleCancel = () => {
  45. props.handleClose();
  46. };
  47. const loadRedemption = async () => {
  48. setLoading(true);
  49. let res = await API.get(`/api/redemption/${props.editingRedemption.id}`);
  50. const { success, message, data } = res.data;
  51. if (success) {
  52. if (data.expired_time === 0) {
  53. data.expired_time = null;
  54. } else {
  55. data.expired_time = new Date(data.expired_time * 1000);
  56. }
  57. formApiRef.current?.setValues({ ...getInitValues(), ...data });
  58. } else {
  59. showError(message);
  60. }
  61. setLoading(false);
  62. };
  63. useEffect(() => {
  64. if (formApiRef.current) {
  65. if (isEdit) {
  66. loadRedemption();
  67. } else {
  68. formApiRef.current.setValues(getInitValues());
  69. }
  70. }
  71. }, [props.editingRedemption.id]);
  72. const submit = async (values) => {
  73. let name = values.name;
  74. if (!isEdit && (!name || name === '')) {
  75. name = renderQuota(values.quota);
  76. }
  77. setLoading(true);
  78. let localInputs = { ...values };
  79. localInputs.count = parseInt(localInputs.count) || 0;
  80. localInputs.quota = parseInt(localInputs.quota) || 0;
  81. localInputs.name = name;
  82. if (!localInputs.expired_time) {
  83. localInputs.expired_time = 0;
  84. } else {
  85. localInputs.expired_time = Math.floor(localInputs.expired_time.getTime() / 1000);
  86. }
  87. let res;
  88. if (isEdit) {
  89. res = await API.put(`/api/redemption/`, {
  90. ...localInputs,
  91. id: parseInt(props.editingRedemption.id),
  92. });
  93. } else {
  94. res = await API.post(`/api/redemption/`, {
  95. ...localInputs,
  96. });
  97. }
  98. const { success, message, data } = res.data;
  99. if (success) {
  100. if (isEdit) {
  101. showSuccess(t('兑换码更新成功!'));
  102. props.refresh();
  103. props.handleClose();
  104. } else {
  105. showSuccess(t('兑换码创建成功!'));
  106. props.refresh();
  107. formApiRef.current?.setValues(getInitValues());
  108. props.handleClose();
  109. }
  110. } else {
  111. showError(message);
  112. }
  113. if (!isEdit && data) {
  114. let text = '';
  115. for (let i = 0; i < data.length; i++) {
  116. text += data[i] + '\n';
  117. }
  118. Modal.confirm({
  119. title: t('兑换码创建成功'),
  120. content: (
  121. <div>
  122. <p>{t('兑换码创建成功,是否下载兑换码?')}</p>
  123. <p>{t('兑换码将以文本文件的形式下载,文件名为兑换码的名称。')}</p>
  124. </div>
  125. ),
  126. onOk: () => {
  127. downloadTextAsFile(text, `${localInputs.name}.txt`);
  128. },
  129. });
  130. }
  131. setLoading(false);
  132. };
  133. return (
  134. <>
  135. <SideSheet
  136. placement={isEdit ? 'right' : 'left'}
  137. title={
  138. <Space>
  139. {isEdit ?
  140. <Tag color="blue" shape="circle">{t('更新')}</Tag> :
  141. <Tag color="green" shape="circle">{t('新建')}</Tag>
  142. }
  143. <Title heading={4} className="m-0">
  144. {isEdit ? t('更新兑换码信息') : t('创建新的兑换码')}
  145. </Title>
  146. </Space>
  147. }
  148. bodyStyle={{ padding: '0' }}
  149. visible={props.visiable}
  150. width={isMobile() ? '100%' : 600}
  151. footer={
  152. <div className="flex justify-end bg-white">
  153. <Space>
  154. <Button
  155. theme="solid"
  156. onClick={() => formApiRef.current?.submitForm()}
  157. icon={<IconSave />}
  158. loading={loading}
  159. >
  160. {t('提交')}
  161. </Button>
  162. <Button
  163. theme="light"
  164. type="primary"
  165. onClick={handleCancel}
  166. icon={<IconClose />}
  167. >
  168. {t('取消')}
  169. </Button>
  170. </Space>
  171. </div>
  172. }
  173. closeIcon={null}
  174. onCancel={() => handleCancel()}
  175. >
  176. <Spin spinning={loading}>
  177. <Form
  178. initValues={getInitValues()}
  179. getFormApi={(api) => formApiRef.current = api}
  180. onSubmit={submit}
  181. >
  182. {({ values }) => (
  183. <div className="p-2">
  184. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  185. {/* Header: Basic Info */}
  186. <div className="flex items-center mb-2">
  187. <Avatar size="small" color="blue" className="mr-2 shadow-md">
  188. <IconGift size={16} />
  189. </Avatar>
  190. <div>
  191. <Text className="text-lg font-medium">{t('基本信息')}</Text>
  192. <div className="text-xs text-gray-600">{t('设置兑换码的基本信息')}</div>
  193. </div>
  194. </div>
  195. <Row gutter={12}>
  196. <Col span={24}>
  197. <Form.Input
  198. field='name'
  199. label={t('名称')}
  200. placeholder={t('请输入名称')}
  201. style={{ width: '100%' }}
  202. rules={!isEdit ? [] : [{ required: true, message: t('请输入名称') }]}
  203. showClear
  204. />
  205. </Col>
  206. <Col span={24}>
  207. <Form.DatePicker
  208. field='expired_time'
  209. label={t('过期时间')}
  210. type='dateTime'
  211. placeholder={t('选择过期时间(可选,留空为永久)')}
  212. style={{ width: '100%' }}
  213. showClear
  214. />
  215. </Col>
  216. </Row>
  217. </Card>
  218. <Card className="!rounded-2xl shadow-sm border-0">
  219. {/* Header: Quota Settings */}
  220. <div className="flex items-center mb-2">
  221. <Avatar size="small" color="green" className="mr-2 shadow-md">
  222. <IconCreditCard size={16} />
  223. </Avatar>
  224. <div>
  225. <Text className="text-lg font-medium">{t('额度设置')}</Text>
  226. <div className="text-xs text-gray-600">{t('设置兑换码的额度和数量')}</div>
  227. </div>
  228. </div>
  229. <Row gutter={12}>
  230. <Col span={12}>
  231. <Form.AutoComplete
  232. field='quota'
  233. label={t('额度')}
  234. placeholder={t('请输入额度')}
  235. style={{ width: '100%' }}
  236. type='number'
  237. rules={[
  238. { required: true, message: t('请输入额度') },
  239. {
  240. validator: (rule, v) => {
  241. const num = parseInt(v, 10);
  242. return num > 0
  243. ? Promise.resolve()
  244. : Promise.reject(t('额度必须大于0'));
  245. },
  246. },
  247. ]}
  248. extraText={renderQuotaWithPrompt(Number(values.quota) || 0)}
  249. data={[
  250. { value: 500000, label: '1$' },
  251. { value: 5000000, label: '10$' },
  252. { value: 25000000, label: '50$' },
  253. { value: 50000000, label: '100$' },
  254. { value: 250000000, label: '500$' },
  255. { value: 500000000, label: '1000$' },
  256. ]}
  257. showClear
  258. />
  259. </Col>
  260. {!isEdit && (
  261. <Col span={12}>
  262. <Form.InputNumber
  263. field='count'
  264. label={t('生成数量')}
  265. min={1}
  266. rules={[
  267. { required: true, message: t('请输入生成数量') },
  268. {
  269. validator: (rule, v) => {
  270. const num = parseInt(v, 10);
  271. return num > 0
  272. ? Promise.resolve()
  273. : Promise.reject(t('生成数量必须大于0'));
  274. },
  275. },
  276. ]}
  277. style={{ width: '100%' }}
  278. showClear
  279. />
  280. </Col>
  281. )}
  282. </Row>
  283. </Card>
  284. </div>
  285. )}
  286. </Form>
  287. </Spin>
  288. </SideSheet>
  289. </>
  290. );
  291. };
  292. export default EditRedemption;