import React, { useEffect, useState } from 'react'; import { Button, Form, Header, Segment } from 'semantic-ui-react'; import { useParams } from 'react-router-dom'; import { API, isAdmin, showError, showSuccess, quotatamp2string } from '../../helpers'; const EditRedemption = () => { const params = useParams(); const redemptionId = params.id; const isEdit = redemptionId !== undefined; const [loading, setLoading] = useState(isEdit); const originInputs = { name: '', quota: 100, count: 1, }; const [inputs, setInputs] = useState(originInputs); const { name, quota, count } = inputs; const handleInputChange = (e, { name, value }) => { setInputs((inputs) => ({ ...inputs, [name]: value })); }; const loadRedemption = async () => { let res = await API.get(`/api/redemption/${redemptionId}`); const { success, message, data } = res.data; if (success) { setInputs(data); } else { showError(message); } setLoading(false); }; useEffect(() => { if (isEdit) { loadRedemption().then(); } }, []); const submit = async () => { if (!isEdit && inputs.name === '') return; 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(redemptionId) }); } else { res = await API.post(`/api/redemption/`, { ...localInputs, }); } const { success, message } = res.data; if (success) { if (isEdit) { showSuccess('兑换码更新成功!'); } else { showSuccess('兑换码创建成功!'); setInputs(originInputs); } } else { showError(message); } }; return ( <>
{isEdit ? '更新兑换码信息' : '创建新的兑换码'}
{ !isEdit && <> }
); }; export default EditRedemption;