|
@@ -1,17 +1,17 @@
|
|
|
-import React, { useState } from 'react';
|
|
|
|
|
-import { Drawer, Form, Input, Button, message } from 'antd';
|
|
|
|
|
-import type { DrawerProps } from 'antd';
|
|
|
|
|
|
|
+import React, { useState, useEffect } from 'react';
|
|
|
|
|
+import { Modal, Form, Input, Button, message } from 'antd';
|
|
|
|
|
+import type { ModalProps } from 'antd';
|
|
|
import type { WordResult } from '../apis/words';
|
|
import type { WordResult } from '../apis/words';
|
|
|
import { useCreateWord, useUpdateWord } from '../apis/words';
|
|
import { useCreateWord, useUpdateWord } from '../apis/words';
|
|
|
|
|
|
|
|
-interface WordDrawerProps extends DrawerProps {
|
|
|
|
|
|
|
+interface WordModalProps extends ModalProps {
|
|
|
dictID: number;
|
|
dictID: number;
|
|
|
initialWord?: WordResult;
|
|
initialWord?: WordResult;
|
|
|
refresh: () => void;
|
|
refresh: () => void;
|
|
|
- onClose: () => void;
|
|
|
|
|
|
|
+ onCancel: () => void;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-const WordDrawer: React.FC<WordDrawerProps> = ({
|
|
|
|
|
|
|
+const WordModal: React.FC<WordModalProps> = ({
|
|
|
dictID,
|
|
dictID,
|
|
|
initialWord,
|
|
initialWord,
|
|
|
refresh,
|
|
refresh,
|
|
@@ -19,12 +19,21 @@ const WordDrawer: React.FC<WordDrawerProps> = ({
|
|
|
}) => {
|
|
}) => {
|
|
|
const [loading, setLoading] = useState(false);
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
|
|
|
|
|
+ const [form] = Form.useForm();
|
|
|
|
|
+
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ if (props.open) {
|
|
|
|
|
+ form.setFieldsValue(initialWord);
|
|
|
|
|
+ }
|
|
|
|
|
+ }, [props.open, form, initialWord]);
|
|
|
|
|
+
|
|
|
const createWord = useCreateWord();
|
|
const createWord = useCreateWord();
|
|
|
const updateWord = useUpdateWord();
|
|
const updateWord = useUpdateWord();
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
- <Drawer
|
|
|
|
|
|
|
+ <Modal
|
|
|
title={initialWord ? '更新单词' : '创建新单词'}
|
|
title={initialWord ? '更新单词' : '创建新单词'}
|
|
|
|
|
+ footer={null}
|
|
|
destroyOnClose
|
|
destroyOnClose
|
|
|
{...props}
|
|
{...props}
|
|
|
>
|
|
>
|
|
@@ -32,7 +41,8 @@ const WordDrawer: React.FC<WordDrawerProps> = ({
|
|
|
name="word"
|
|
name="word"
|
|
|
labelCol={{ span: 6 }}
|
|
labelCol={{ span: 6 }}
|
|
|
wrapperCol={{ span: 18 }}
|
|
wrapperCol={{ span: 18 }}
|
|
|
- initialValues={initialWord}
|
|
|
|
|
|
|
+ form={form}
|
|
|
|
|
+ preserve={false}
|
|
|
onFinish={(values: { value: string; meaning: string; extra: string }) =>
|
|
onFinish={(values: { value: string; meaning: string; extra: string }) =>
|
|
|
void (async () => {
|
|
void (async () => {
|
|
|
try {
|
|
try {
|
|
@@ -40,12 +50,13 @@ const WordDrawer: React.FC<WordDrawerProps> = ({
|
|
|
if (initialWord) {
|
|
if (initialWord) {
|
|
|
await updateWord({ ...initialWord, ...values });
|
|
await updateWord({ ...initialWord, ...values });
|
|
|
void message.success('更新成功');
|
|
void message.success('更新成功');
|
|
|
|
|
+ props.onCancel();
|
|
|
} else {
|
|
} else {
|
|
|
await createWord({ ...values, dictID });
|
|
await createWord({ ...values, dictID });
|
|
|
void message.success('创建成功');
|
|
void message.success('创建成功');
|
|
|
|
|
+ form.resetFields();
|
|
|
}
|
|
}
|
|
|
refresh();
|
|
refresh();
|
|
|
- props.onClose();
|
|
|
|
|
} catch (err) {
|
|
} catch (err) {
|
|
|
if (err instanceof Error) {
|
|
if (err instanceof Error) {
|
|
|
void message.error(err.message);
|
|
void message.error(err.message);
|
|
@@ -81,8 +92,8 @@ const WordDrawer: React.FC<WordDrawerProps> = ({
|
|
|
</Button>
|
|
</Button>
|
|
|
</Form.Item>
|
|
</Form.Item>
|
|
|
</Form>
|
|
</Form>
|
|
|
- </Drawer>
|
|
|
|
|
|
|
+ </Modal>
|
|
|
);
|
|
);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-export default WordDrawer;
|
|
|
|
|
|
|
+export default WordModal;
|