EditRedemption.js 9.6 KB

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