import React, { useEffect, useState } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import { API, downloadTextAsFile, isMobile, showError, showSuccess, } from '../../helpers'; import { renderQuotaWithPrompt } from '../../helpers/render'; import { AutoComplete, Button, Input, Modal, SideSheet, Space, Spin, Typography, } from '@douyinfe/semi-ui'; import Title from '@douyinfe/semi-ui/lib/es/typography/title'; import { Divider } from 'semantic-ui-react'; const EditRedemption = (props) => { const isEdit = props.editingRedemption.id !== undefined; const [loading, setLoading] = useState(isEdit); const params = useParams(); const navigate = useNavigate(); const originInputs = { name: '', quota: 100000, count: 1, }; const [inputs, setInputs] = useState(originInputs); const { name, quota, count } = inputs; const handleCancel = () => { props.handleClose(); }; const handleInputChange = (name, value) => { setInputs((inputs) => ({ ...inputs, [name]: value })); }; const loadRedemption = async () => { 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); }; useEffect(() => { if (isEdit) { loadRedemption().then(() => { // console.log(inputs); }); } else { setInputs(originInputs); } }, [props.editingRedemption.id]); const submit = async () => { if (!isEdit && inputs.name === '') return; setLoading(true); let localInputs = inputs; localInputs.count = parseInt(localInputs.count); localInputs.quota = parseInt(localInputs.quota); let res; if (isEdit) { res = await API.put(`/api/redemption/`, { ...localInputs, id: parseInt(props.editingRedemption.id), }); } else { res = await API.post(`/api/redemption/`, { ...localInputs, }); } const { success, message, data } = res.data; if (success) { if (isEdit) { showSuccess('兑换码更新成功!'); props.refresh(); props.handleClose(); } else { showSuccess('兑换码创建成功!'); setInputs(originInputs); props.refresh(); props.handleClose(); } } else { showError(message); } if (!isEdit && data) { let text = ''; for (let i = 0; i < data.length; i++) { text += data[i] + '\n'; } // downloadTextAsFile(text, `${inputs.name}.txt`); Modal.confirm({ title: '兑换码创建成功', content: (
兑换码创建成功,是否下载兑换码?
兑换码将以文本文件的形式下载,文件名为兑换码的名称。