import React, {useEffect, useRef, useState} from 'react'; // import {Button, Form, Header, Message, Segment} from 'semantic-ui-react'; import {useParams, useNavigate} from 'react-router-dom'; import {API, isMobile, showError, showSuccess, timestamp2string} from '../../helpers'; import {renderQuota, renderQuotaWithPrompt} from '../../helpers/render'; import {Layout, SideSheet, Button, Space, Spin, Banner, Input, DatePicker, AutoComplete, Typography} from "@douyinfe/semi-ui"; import Title from "@douyinfe/semi-ui/lib/es/typography/title"; import {Divider} from "semantic-ui-react"; const EditToken = (props) => { const isEdit = props.editingToken.id !== undefined; const [loading, setLoading] = useState(isEdit); const originInputs = { name: '', remain_quota: isEdit ? 0 : 500000, expired_time: -1, unlimited_quota: false }; const [inputs, setInputs] = useState(originInputs); const {name, remain_quota, expired_time, unlimited_quota} = inputs; // const [visible, setVisible] = useState(false); const navigate = useNavigate(); const handleInputChange = (name, value) => { setInputs((inputs) => ({...inputs, [name]: value})); }; const handleCancel = () => { props.handleClose(); } const setExpiredTime = (month, day, hour, minute) => { let now = new Date(); let timestamp = now.getTime() / 1000; let seconds = month * 30 * 24 * 60 * 60; seconds += day * 24 * 60 * 60; seconds += hour * 60 * 60; seconds += minute * 60; if (seconds !== 0) { timestamp += seconds; setInputs({...inputs, expired_time: timestamp2string(timestamp)}); } else { setInputs({...inputs, expired_time: -1}); } }; const setUnlimitedQuota = () => { setInputs({...inputs, unlimited_quota: !unlimited_quota}); }; const loadToken = async () => { setLoading(true); let res = await API.get(`/api/token/${props.editingToken.id}`); const {success, message, data} = res.data; if (success) { if (data.expired_time !== -1) { data.expired_time = timestamp2string(data.expired_time); } setInputs(data); } else { showError(message); } setLoading(false); }; useEffect(() => { if (isEdit) { loadToken().then( () => { // console.log(inputs); } ); } else { setInputs(originInputs); } }, [props.editingToken.id]); const submit = async () => { if (!isEdit && inputs.name === '') return; setLoading(true); let localInputs = inputs; localInputs.remain_quota = parseInt(localInputs.remain_quota); if (localInputs.expired_time !== -1) { let time = Date.parse(localInputs.expired_time); if (isNaN(time)) { showError('过期时间格式错误!'); return; } localInputs.expired_time = Math.ceil(time / 1000); } let res; if (isEdit) { res = await API.put(`/api/token/`, {...localInputs, id: parseInt(props.editingToken.id)}); } else { res = await API.post(`/api/token/`, localInputs); } const {success, message} = res.data; if (success) { if (isEdit) { showSuccess('令牌更新成功!'); props.refresh(); props.handleClose(); } else { showSuccess('令牌创建成功,请在列表页面点击复制获取令牌!'); setInputs(originInputs); props.refresh(); props.handleClose(); } } else { showError(message); } setLoading(false); }; return ( <> {isEdit ? '更新令牌信息' : '创建新的令牌'}} headerStyle={{borderBottom: '1px solid var(--semi-color-border)'}} bodyStyle={{borderBottom: '1px solid var(--semi-color-border)'}} visible={props.visiable} footer={
} closeIcon={null} onCancel={() => handleCancel()} width={isMobile() ? '100%' : 600} > handleInputChange('name', value)} value={name} autoComplete='new-password' required={!isEdit} /> handleInputChange('expired_time', value)} value={expired_time} autoComplete='new-password' type='dateTime' />
{`额度${renderQuotaWithPrompt(remain_quota)}`}
handleInputChange('remain_quota', value)} value={remain_quota} autoComplete='new-password' type='number' position={'top'} 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$'}, ]} disabled={unlimited_quota} />
); }; export default EditToken;