import React, { useEffect, useState, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { API, downloadTextAsFile, isMobile, showError, showSuccess, renderQuota, renderQuotaWithPrompt, } from '../../helpers'; import { Button, Modal, SideSheet, Space, Spin, Typography, Card, Tag, Form, Avatar, Row, Col, } from '@douyinfe/semi-ui'; import { IconCreditCard, IconSave, IconClose, 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 formApiRef = useRef(null); const getInitValues = () => ({ name: '', quota: 100000, count: 1, expired_time: null, }); const handleCancel = () => { props.handleClose(); }; const loadRedemption = async () => { setLoading(true); let res = await API.get(`/api/redemption/${props.editingRedemption.id}`); const { success, message, data } = res.data; if (success) { if (data.expired_time === 0) { data.expired_time = null; } else { data.expired_time = new Date(data.expired_time * 1000); } formApiRef.current?.setValues({ ...getInitValues(), ...data }); } else { showError(message); } setLoading(false); }; useEffect(() => { if (formApiRef.current) { if (isEdit) { loadRedemption(); } else { formApiRef.current.setValues(getInitValues()); } } }, [props.editingRedemption.id]); const submit = async (values) => { let name = values.name; if (!isEdit && values.name === '') { name = renderQuota(values.quota); } setLoading(true); let localInputs = { ...values }; localInputs.count = parseInt(localInputs.count) || 0; localInputs.quota = parseInt(localInputs.quota) || 0; localInputs.name = name; if (!localInputs.expired_time) { localInputs.expired_time = 0; } else { localInputs.expired_time = Math.floor(localInputs.expired_time.getTime() / 1000); } 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('兑换码创建成功!')); props.refresh(); formApiRef.current?.setValues(getInitValues()); 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('兑换码将以文本文件的形式下载,文件名为兑换码的名称。')}