WordModal.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import React, { useState, useEffect } from 'react';
  2. import type { ModalProps } from 'antd';
  3. import { Modal, Form, Input, Button, message } from 'antd';
  4. import type { DictResult } from '../apis/dicts';
  5. import type { WordResult } from '../apis/words';
  6. import { useCreateWord, useUpdateWord } from '../apis/words';
  7. interface WordModalProps extends ModalProps {
  8. dict: DictResult;
  9. initialWord?: WordResult;
  10. refresh: () => void;
  11. onCancel: () => void;
  12. }
  13. const WordModal: React.FC<WordModalProps> = ({
  14. dict,
  15. initialWord,
  16. refresh,
  17. ...props
  18. }) => {
  19. const [loading, setLoading] = useState(false);
  20. const [form] = Form.useForm();
  21. useEffect(() => {
  22. if (props.open) {
  23. if (initialWord) {
  24. form.setFieldsValue(initialWord);
  25. } else {
  26. form.resetFields();
  27. }
  28. }
  29. }, [props.open, form, initialWord]);
  30. const createWord = useCreateWord();
  31. const updateWord = useUpdateWord();
  32. return (
  33. <Modal
  34. style={{ top: 64 }}
  35. title={initialWord ? '更新单词' : '创建新单词'}
  36. footer={null}
  37. destroyOnClose
  38. {...props}
  39. >
  40. <Form
  41. name="word"
  42. labelCol={{ span: 6 }}
  43. wrapperCol={{ span: 18 }}
  44. form={form}
  45. onFinish={(values: {
  46. value: string;
  47. meaning: string;
  48. extra?: string;
  49. }) =>
  50. void (async () => {
  51. try {
  52. setLoading(true);
  53. if (initialWord) {
  54. await updateWord({
  55. id: initialWord.id,
  56. ...values,
  57. star: initialWord.star,
  58. });
  59. void message.success('更新成功');
  60. props.onCancel();
  61. } else {
  62. await createWord({ ...values, dictID: dict.id });
  63. void message.success('创建成功');
  64. form.resetFields();
  65. }
  66. refresh();
  67. } catch (err) {
  68. if (err instanceof Error) {
  69. void message.error(err.message);
  70. }
  71. } finally {
  72. setLoading(false);
  73. }
  74. })()
  75. }
  76. >
  77. <Form.Item
  78. name="value"
  79. label={dict.valueTitle}
  80. rules={[{ required: true, message: `请输入${dict.valueTitle}` }]}
  81. validateTrigger="onBlur"
  82. >
  83. <Input placeholder={`请输入${dict.valueTitle}`} />
  84. </Form.Item>
  85. <Form.Item
  86. name="meaning"
  87. label={dict.meaningTitle}
  88. rules={[{ required: true, message: `请输入${dict.meaningTitle}` }]}
  89. validateTrigger="onBlur"
  90. >
  91. <Input placeholder={`请输入${dict.meaningTitle}`} />
  92. </Form.Item>
  93. {dict.extraTitle && (
  94. <Form.Item name="extra" label={dict.extraTitle}>
  95. <Input placeholder={`请输入${dict.extraTitle}`} />
  96. </Form.Item>
  97. )}
  98. <Form.Item label=" " colon={false}>
  99. <Button htmlType="submit" type="primary" loading={loading}>
  100. {initialWord ? '更新' : '创建'}
  101. </Button>
  102. </Form.Item>
  103. </Form>
  104. </Modal>
  105. );
  106. };
  107. export default WordModal;