new-api/web/src/pages/Redemption/EditRedemption.js

216 lines
5.9 KiB
JavaScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
2024-03-15 16:05:33 +08:00
import { useNavigate, useParams } from 'react-router-dom';
2024-03-23 21:24:39 +08:00
import {
API,
downloadTextAsFile,
isMobile,
showError,
showSuccess,
} from '../../helpers';
import { getQuotaPerUnit, renderQuota, renderQuotaWithPrompt } from '../../helpers/render';
2024-03-23 21:24:39 +08:00
import {
AutoComplete,
Button,
Input,
Modal,
SideSheet,
Space,
Spin,
Typography,
} from '@douyinfe/semi-ui';
2024-03-15 16:05:33 +08:00
import Title from '@douyinfe/semi-ui/lib/es/typography/title';
import { Divider } from 'semantic-ui-react';
2023-11-10 00:10:41 +08:00
const EditRedemption = (props) => {
const isEdit = props.editingRedemption.id !== undefined;
const [loading, setLoading] = useState(isEdit);
2023-11-10 00:10:41 +08:00
const params = useParams();
2024-03-15 16:05:33 +08:00
const navigate = useNavigate();
const originInputs = {
name: '',
quota: 100000,
2024-03-23 21:24:39 +08:00
count: 1,
};
const [inputs, setInputs] = useState(originInputs);
const { name, quota, count } = inputs;
const handleCancel = () => {
2023-11-10 00:10:41 +08:00
props.handleClose();
2024-03-15 16:05:33 +08:00
};
2023-11-10 00:10:41 +08:00
const handleInputChange = (name, value) => {
setInputs((inputs) => ({ ...inputs, [name]: value }));
};
const loadRedemption = async () => {
2023-11-10 00:10:41 +08:00
setLoading(true);
let res = await API.get(`/api/redemption/${props.editingRedemption.id}`);
const { success, message, data } = res.data;
if (success) {
setInputs(data);
} else {
showError(message);
}
setLoading(false);
};
2023-11-10 00:10:41 +08:00
useEffect(() => {
if (isEdit) {
2024-03-23 21:24:39 +08:00
loadRedemption().then(() => {
// console.log(inputs);
});
2023-11-10 00:10:41 +08:00
} else {
setInputs(originInputs);
}
2023-11-10 00:10:41 +08:00
}, [props.editingRedemption.id]);
const submit = async () => {
let name = inputs.name;
if (!isEdit && inputs.name === '') {
// set default name
name = '兑换码-' + renderQuota(quota);
}
2023-11-10 00:10:41 +08:00
setLoading(true);
let localInputs = inputs;
localInputs.count = parseInt(localInputs.count);
localInputs.quota = parseInt(localInputs.quota);
localInputs.name = name;
let res;
if (isEdit) {
2024-03-23 21:24:39 +08:00
res = await API.put(`/api/redemption/`, {
...localInputs,
id: parseInt(props.editingRedemption.id),
});
} else {
res = await API.post(`/api/redemption/`, {
2024-03-23 21:24:39 +08:00
...localInputs,
});
}
const { success, message, data } = res.data;
if (success) {
if (isEdit) {
showSuccess('兑换码更新成功!');
2023-11-10 00:10:41 +08:00
props.refresh();
props.handleClose();
} else {
showSuccess('兑换码创建成功!');
setInputs(originInputs);
2023-11-10 00:10:41 +08:00
props.refresh();
props.handleClose();
}
} else {
showError(message);
}
if (!isEdit && data) {
2024-03-15 16:05:33 +08:00
let text = '';
for (let i = 0; i < data.length; i++) {
2024-03-15 16:05:33 +08:00
text += data[i] + '\n';
}
2023-11-10 00:10:41 +08:00
// downloadTextAsFile(text, `${inputs.name}.txt`);
Modal.confirm({
title: '兑换码创建成功',
content: (
2024-03-15 16:05:33 +08:00
<div>
<p>兑换码创建成功是否下载兑换码</p>
<p>兑换码将以文本文件的形式下载文件名为兑换码的名称</p>
</div>
2023-11-10 00:10:41 +08:00
),
onOk: () => {
2024-03-15 16:05:33 +08:00
downloadTextAsFile(text, `${inputs.name}.txt`);
2024-03-23 21:24:39 +08:00
},
2023-11-10 00:10:41 +08:00
});
}
2023-11-10 00:10:41 +08:00
setLoading(false);
};
return (
<>
2023-11-10 00:10:41 +08:00
<SideSheet
2024-03-15 16:05:33 +08:00
placement={isEdit ? 'right' : 'left'}
2024-03-23 21:24:39 +08:00
title={
<Title level={3}>
{isEdit ? '更新兑换码信息' : '创建新的兑换码'}
</Title>
}
2024-03-15 16:05:33 +08:00
headerStyle={{ borderBottom: '1px solid var(--semi-color-border)' }}
bodyStyle={{ borderBottom: '1px solid var(--semi-color-border)' }}
visible={props.visiable}
footer={
<div style={{ display: 'flex', justifyContent: 'flex-end' }}>
<Space>
2024-03-23 21:24:39 +08:00
<Button theme='solid' size={'large'} onClick={submit}>
提交
</Button>
<Button
theme='solid'
size={'large'}
type={'tertiary'}
onClick={handleCancel}
>
取消
</Button>
2024-03-15 16:05:33 +08:00
</Space>
</div>
}
closeIcon={null}
onCancel={() => handleCancel()}
width={isMobile() ? '100%' : 600}
2023-11-10 00:10:41 +08:00
>
<Spin spinning={loading}>
<Input
2024-03-15 16:05:33 +08:00
style={{ marginTop: 20 }}
2024-03-23 21:24:39 +08:00
label='名称'
name='name'
2024-03-15 16:05:33 +08:00
placeholder={'请输入名称'}
2024-03-23 21:24:39 +08:00
onChange={(value) => handleInputChange('name', value)}
2024-03-15 16:05:33 +08:00
value={name}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
2024-03-15 16:05:33 +08:00
required={!isEdit}
2023-11-10 00:10:41 +08:00
/>
2024-03-15 16:05:33 +08:00
<Divider />
2023-11-10 00:10:41 +08:00
<div style={{ marginTop: 20 }}>
<Typography.Text>{`额度${renderQuotaWithPrompt(quota)}`}</Typography.Text>
</div>
<AutoComplete
2024-03-15 16:05:33 +08:00
style={{ marginTop: 8 }}
2024-03-23 21:24:39 +08:00
name='quota'
2024-03-15 16:05:33 +08:00
placeholder={'请输入额度'}
onChange={(value) => handleInputChange('quota', value)}
value={quota}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
type='number'
2024-03-15 16:05:33 +08:00
position={'bottom'}
data={[
{ value: 500000, label: '1$' },
{ value: 5000000, label: '10$' },
{ value: 25000000, label: '50$' },
{ value: 50000000, label: '100$' },
{ value: 250000000, label: '500$' },
2024-03-23 21:24:39 +08:00
{ value: 500000000, label: '1000$' },
2024-03-15 16:05:33 +08:00
]}
2023-11-10 00:10:41 +08:00
/>
2024-03-23 21:24:39 +08:00
{!isEdit && (
<>
2024-03-15 16:05:33 +08:00
<Divider />
<Typography.Text>生成数量</Typography.Text>
<Input
style={{ marginTop: 8 }}
2024-03-23 21:24:39 +08:00
label='生成数量'
name='count'
2024-03-15 16:05:33 +08:00
placeholder={'请输入生成数量'}
2024-03-23 21:24:39 +08:00
onChange={(value) => handleInputChange('count', value)}
2024-03-15 16:05:33 +08:00
value={count}
2024-03-23 21:24:39 +08:00
autoComplete='new-password'
type='number'
2024-03-15 16:05:33 +08:00
/>
</>
2024-03-23 21:24:39 +08:00
)}
2023-11-10 00:10:41 +08:00
</Spin>
</SideSheet>
</>
);
};
export default EditRedemption;