import CopyButton from '@/components/copy-button'; import ModalFooter from '@/components/modal-footer'; import SealInput from '@/components/seal-form/seal-input'; import SealSelect from '@/components/seal-form/seal-select'; import { PageAction } from '@/config'; import { PageActionType } from '@/config/types'; import { useIntl } from '@umijs/max'; import { Button, Form, Modal, Select, Tag } from 'antd'; import dayjs from 'dayjs'; import { useState } from 'react'; import { createApisKey } from '../apis'; import { expirationOptions } from '../config'; import { FormData } from '../config/types'; type AddModalProps = { title: string; action: PageActionType; open: boolean; onOk: () => void; onCancel: () => void; }; const AddModal: React.FC = ({ title, action, open, onOk, onCancel }) => { const [form] = Form.useForm(); const intl = useIntl(); const [showKey, setShowKey] = useState(false); const [apikeyValue, setAPIKeyValue] = useState(''); const [loading, setLoading] = useState(false); if (action === PageAction.CREATE && open) { form.setFieldsValue({ expires_in: 1 }); } const getExpireValue = (val: number | null) => { const expires_in = val; if (expires_in === -1) { return 0; } const selected = expirationOptions.find( (item) => expires_in === item.value ); const d1 = dayjs().add( selected?.value as number, `${selected?.type}` as never ); const d2 = dayjs(); const res = d1.diff(d2, 'second'); return res; }; const handleOnOk = async (data: FormData) => { try { setLoading(true); const params = { ...data, expires_in: getExpireValue(data.expires_in) }; const res = await createApisKey({ data: params }); setAPIKeyValue(res.value); setShowKey(true); setLoading(false); } catch (error) { setLoading(false); } }; const handleSumit = () => { form.submit(); }; const handleDone = () => { onOk(); }; const handleAfterOpenChange = (isOpen: boolean) => { setShowKey(false); }; return ( ) : ( ) } >
{!showKey ? ( <> name="name" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'common.table.name' }) } ) } ]} > name="expires_in" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.select' }, { name: intl.formatMessage({ id: 'apikeys.form.expiretime' }) } ) } ]} > {expirationOptions.map((option) => { return ( {intl.formatMessage({ id: option.label })} ); })} name="description" rules={[{ required: false }]} > ) : ( {intl.formatMessage({ id: 'apikeys.table.save.tips' })} } > } > )}
); }; export default AddModal;