EditRedemption.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import React, { useEffect, useState } from 'react';
  2. import { useNavigate, useParams } from 'react-router-dom';
  3. import {
  4. API,
  5. downloadTextAsFile,
  6. isMobile,
  7. showError,
  8. showSuccess,
  9. } from '../../helpers';
  10. import { getQuotaPerUnit, renderQuota, renderQuotaWithPrompt } from '../../helpers/render';
  11. import {
  12. AutoComplete,
  13. Button,
  14. Input,
  15. Modal,
  16. SideSheet,
  17. Space,
  18. Spin,
  19. Typography,
  20. } from '@douyinfe/semi-ui';
  21. import Title from '@douyinfe/semi-ui/lib/es/typography/title';
  22. import { Divider } from 'semantic-ui-react';
  23. const EditRedemption = (props) => {
  24. const isEdit = props.editingRedemption.id !== undefined;
  25. const [loading, setLoading] = useState(isEdit);
  26. const params = useParams();
  27. const navigate = useNavigate();
  28. const originInputs = {
  29. name: '',
  30. quota: 100000,
  31. count: 1,
  32. };
  33. const [inputs, setInputs] = useState(originInputs);
  34. const { name, quota, count } = inputs;
  35. const handleCancel = () => {
  36. props.handleClose();
  37. };
  38. const handleInputChange = (name, value) => {
  39. setInputs((inputs) => ({ ...inputs, [name]: value }));
  40. };
  41. const loadRedemption = async () => {
  42. setLoading(true);
  43. let res = await API.get(`/api/redemption/${props.editingRedemption.id}`);
  44. const { success, message, data } = res.data;
  45. if (success) {
  46. setInputs(data);
  47. } else {
  48. showError(message);
  49. }
  50. setLoading(false);
  51. };
  52. useEffect(() => {
  53. if (isEdit) {
  54. loadRedemption().then(() => {
  55. // console.log(inputs);
  56. });
  57. } else {
  58. setInputs(originInputs);
  59. }
  60. }, [props.editingRedemption.id]);
  61. const submit = async () => {
  62. let name = inputs.name;
  63. if (!isEdit && inputs.name === '') {
  64. // set default name
  65. name = '兑换码-' + renderQuota(quota);
  66. }
  67. setLoading(true);
  68. let localInputs = inputs;
  69. localInputs.count = parseInt(localInputs.count);
  70. localInputs.quota = parseInt(localInputs.quota);
  71. localInputs.name = name;
  72. let res;
  73. if (isEdit) {
  74. res = await API.put(`/api/redemption/`, {
  75. ...localInputs,
  76. id: parseInt(props.editingRedemption.id),
  77. });
  78. } else {
  79. res = await API.post(`/api/redemption/`, {
  80. ...localInputs,
  81. });
  82. }
  83. const { success, message, data } = res.data;
  84. if (success) {
  85. if (isEdit) {
  86. showSuccess('兑换码更新成功!');
  87. props.refresh();
  88. props.handleClose();
  89. } else {
  90. showSuccess('兑换码创建成功!');
  91. setInputs(originInputs);
  92. props.refresh();
  93. props.handleClose();
  94. }
  95. } else {
  96. showError(message);
  97. }
  98. if (!isEdit && data) {
  99. let text = '';
  100. for (let i = 0; i < data.length; i++) {
  101. text += data[i] + '\n';
  102. }
  103. // downloadTextAsFile(text, `${inputs.name}.txt`);
  104. Modal.confirm({
  105. title: '兑换码创建成功',
  106. content: (
  107. <div>
  108. <p>兑换码创建成功,是否下载兑换码?</p>
  109. <p>兑换码将以文本文件的形式下载,文件名为兑换码的名称。</p>
  110. </div>
  111. ),
  112. onOk: () => {
  113. downloadTextAsFile(text, `${inputs.name}.txt`);
  114. },
  115. });
  116. }
  117. setLoading(false);
  118. };
  119. return (
  120. <>
  121. <SideSheet
  122. placement={isEdit ? 'right' : 'left'}
  123. title={
  124. <Title level={3}>
  125. {isEdit ? '更新兑换码信息' : '创建新的兑换码'}
  126. </Title>
  127. }
  128. headerStyle={{ borderBottom: '1px solid var(--semi-color-border)' }}
  129. bodyStyle={{ borderBottom: '1px solid var(--semi-color-border)' }}
  130. visible={props.visiable}
  131. footer={
  132. <div style={{ display: 'flex', justifyContent: 'flex-end' }}>
  133. <Space>
  134. <Button theme='solid' size={'large'} onClick={submit}>
  135. 提交
  136. </Button>
  137. <Button
  138. theme='solid'
  139. size={'large'}
  140. type={'tertiary'}
  141. onClick={handleCancel}
  142. >
  143. 取消
  144. </Button>
  145. </Space>
  146. </div>
  147. }
  148. closeIcon={null}
  149. onCancel={() => handleCancel()}
  150. width={isMobile() ? '100%' : 600}
  151. >
  152. <Spin spinning={loading}>
  153. <Input
  154. style={{ marginTop: 20 }}
  155. label='名称'
  156. name='name'
  157. placeholder={'请输入名称'}
  158. onChange={(value) => handleInputChange('name', value)}
  159. value={name}
  160. autoComplete='new-password'
  161. required={!isEdit}
  162. />
  163. <Divider />
  164. <div style={{ marginTop: 20 }}>
  165. <Typography.Text>{`额度${renderQuotaWithPrompt(quota)}`}</Typography.Text>
  166. </div>
  167. <AutoComplete
  168. style={{ marginTop: 8 }}
  169. name='quota'
  170. placeholder={'请输入额度'}
  171. onChange={(value) => handleInputChange('quota', value)}
  172. value={quota}
  173. autoComplete='new-password'
  174. type='number'
  175. position={'bottom'}
  176. data={[
  177. { value: 500000, label: '1$' },
  178. { value: 5000000, label: '10$' },
  179. { value: 25000000, label: '50$' },
  180. { value: 50000000, label: '100$' },
  181. { value: 250000000, label: '500$' },
  182. { value: 500000000, label: '1000$' },
  183. ]}
  184. />
  185. {!isEdit && (
  186. <>
  187. <Divider />
  188. <Typography.Text>生成数量</Typography.Text>
  189. <Input
  190. style={{ marginTop: 8 }}
  191. label='生成数量'
  192. name='count'
  193. placeholder={'请输入生成数量'}
  194. onChange={(value) => handleInputChange('count', value)}
  195. value={count}
  196. autoComplete='new-password'
  197. type='number'
  198. />
  199. </>
  200. )}
  201. </Spin>
  202. </SideSheet>
  203. </>
  204. );
  205. };
  206. export default EditRedemption;