import React, { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { API, downloadTextAsFile, isMobile, showError, showSuccess, renderQuota, renderQuotaWithPrompt } from '../../helpers'; import { AutoComplete, Button, Input, Modal, SideSheet, Space, Spin, Typography, Card, Tag, } from '@douyinfe/semi-ui'; import { IconCreditCard, IconSave, IconClose, IconPlusCircle, IconGift, } from '@douyinfe/semi-icons'; const { Text, Title } = Typography; const EditRedemption = (props) => { const { t } = useTranslation(); const isEdit = props.editingRedemption.id !== undefined; const [loading, setLoading] = useState(isEdit); 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 () => { let name = inputs.name; if (!isEdit && inputs.name === '') { // set default name name = renderQuota(quota); } setLoading(true); let localInputs = inputs; localInputs.count = parseInt(localInputs.count); localInputs.quota = parseInt(localInputs.quota); localInputs.name = name; 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(t('兑换码更新成功!')); props.refresh(); props.handleClose(); } else { showSuccess(t('兑换码创建成功!')); 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'; } Modal.confirm({ title: t('兑换码创建成功'), content: (

{t('兑换码创建成功,是否下载兑换码?')}

{t('兑换码将以文本文件的形式下载,文件名为兑换码的名称。')}

), onOk: () => { downloadTextAsFile(text, `${inputs.name}.txt`); }, }); } setLoading(false); }; return ( <> {isEdit ? {t('更新')} : {t('新建')} } {isEdit ? t('更新兑换码信息') : t('创建新的兑换码')} } headerStyle={{ borderBottom: '1px solid var(--semi-color-border)', padding: '24px' }} bodyStyle={{ backgroundColor: 'var(--semi-color-bg-0)', padding: '0' }} visible={props.visiable} width={isMobile() ? '100%' : 600} footer={
} closeIcon={null} onCancel={() => handleCancel()} >
{t('基本信息')}
{t('设置兑换码的基本信息')}
{t('名称')} handleInputChange('name', value)} value={name} autoComplete="new-password" size="large" className="!rounded-lg" showClear required={!isEdit} />
{t('额度设置')}
{t('设置兑换码的额度和数量')}
{t('额度')} {renderQuotaWithPrompt(quota)}
handleInputChange('quota', value)} value={quota} autoComplete="new-password" type="number" size="large" className="w-full !rounded-lg" prefix={} data={[ { value: 500000, label: '1$' }, { value: 5000000, label: '10$' }, { value: 25000000, label: '50$' }, { value: 50000000, label: '100$' }, { value: 250000000, label: '500$' }, { value: 500000000, label: '1000$' }, ]} />
{!isEdit && (
{t('生成数量')} handleInputChange('count', value)} value={count} autoComplete="new-password" type="number" size="large" className="!rounded-lg" prefix={} />
)}
); }; export default EditRedemption;