EditRedemption.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Form, Header, Segment } from 'semantic-ui-react';
  3. import { useParams, useNavigate } from 'react-router-dom';
  4. import { API, downloadTextAsFile, showError, showSuccess } from '../../helpers';
  5. import { renderQuota, renderQuotaWithPrompt } from '../../helpers/render';
  6. const EditRedemption = () => {
  7. const params = useParams();
  8. const navigate = useNavigate();
  9. const redemptionId = params.id;
  10. const isEdit = redemptionId !== undefined;
  11. const [loading, setLoading] = useState(isEdit);
  12. const originInputs = {
  13. name: '',
  14. quota: 100000,
  15. count: 1
  16. };
  17. const [inputs, setInputs] = useState(originInputs);
  18. const { name, quota, count } = inputs;
  19. const handleCancel = () => {
  20. navigate('/redemption');
  21. };
  22. const handleInputChange = (e, { name, value }) => {
  23. setInputs((inputs) => ({ ...inputs, [name]: value }));
  24. };
  25. const loadRedemption = async () => {
  26. let res = await API.get(`/api/redemption/${redemptionId}`);
  27. const { success, message, data } = res.data;
  28. if (success) {
  29. setInputs(data);
  30. } else {
  31. showError(message);
  32. }
  33. setLoading(false);
  34. };
  35. useEffect(() => {
  36. if (isEdit) {
  37. loadRedemption().then();
  38. }
  39. }, []);
  40. const submit = async () => {
  41. if (!isEdit && inputs.name === '') return;
  42. let localInputs = inputs;
  43. localInputs.count = parseInt(localInputs.count);
  44. localInputs.quota = parseInt(localInputs.quota);
  45. let res;
  46. if (isEdit) {
  47. res = await API.put(`/api/redemption/`, { ...localInputs, id: parseInt(redemptionId) });
  48. } else {
  49. res = await API.post(`/api/redemption/`, {
  50. ...localInputs
  51. });
  52. }
  53. const { success, message, data } = res.data;
  54. if (success) {
  55. if (isEdit) {
  56. showSuccess('兑换码更新成功!');
  57. } else {
  58. showSuccess('兑换码创建成功!');
  59. setInputs(originInputs);
  60. }
  61. } else {
  62. showError(message);
  63. }
  64. if (!isEdit && data) {
  65. let text = "";
  66. for (let i = 0; i < data.length; i++) {
  67. text += data[i] + "\n";
  68. }
  69. downloadTextAsFile(text, `${inputs.name}.txt`);
  70. }
  71. };
  72. return (
  73. <>
  74. <Segment loading={loading}>
  75. <Header as='h3'>{isEdit ? '更新兑换码信息' : '创建新的兑换码'}</Header>
  76. <Form autoComplete='new-password'>
  77. <Form.Field>
  78. <Form.Input
  79. label='名称'
  80. name='name'
  81. placeholder={'请输入名称'}
  82. onChange={handleInputChange}
  83. value={name}
  84. autoComplete='new-password'
  85. required={!isEdit}
  86. />
  87. </Form.Field>
  88. <Form.Field>
  89. <Form.Input
  90. label={`额度${renderQuotaWithPrompt(quota)}`}
  91. name='quota'
  92. placeholder={'请输入单个兑换码中包含的额度'}
  93. onChange={handleInputChange}
  94. value={quota}
  95. autoComplete='new-password'
  96. type='number'
  97. />
  98. </Form.Field>
  99. {
  100. !isEdit && <>
  101. <Form.Field>
  102. <Form.Input
  103. label='生成数量'
  104. name='count'
  105. placeholder={'请输入生成数量'}
  106. onChange={handleInputChange}
  107. value={count}
  108. autoComplete='new-password'
  109. type='number'
  110. />
  111. </Form.Field>
  112. </>
  113. }
  114. <Button positive onClick={submit}>提交</Button>
  115. <Button onClick={handleCancel}>取消</Button>
  116. </Form>
  117. </Segment>
  118. </>
  119. );
  120. };
  121. export default EditRedemption;