| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- import {
- ModalForm,
- ProForm,
- type ProFormInstance,
- ProFormSelect,
- ProFormText,
- } from "@ant-design/pro-components";
- import { Form, message } from "antd";
- import { useRef, useState } from "react";
- import { useIntl } from "react-intl";
- import type {
- IRelation,
- IRelationRequest,
- IRelationResponse,
- } from "../../../pages/admin/relation/list";
- import { get, post, put } from "../../../request";
- import GrammarSelect from "./GrammarSelect";
- export const _verb = [
- "n",
- "ti",
- "v",
- "v:ind",
- "ind",
- "sg",
- "pl",
- "nom",
- "acc",
- "gen",
- "dat",
- "inst",
- "voc",
- "abl",
- "loc",
- "base",
- "imp",
- "opt",
- "pres",
- "aor",
- "fut",
- "1p",
- "2p",
- "3p",
- "prp",
- "pp",
- "grd",
- "fpp",
- "vdn",
- "ger",
- "inf",
- "adj",
- "pron",
- "caus",
- "num",
- "adv",
- "conj",
- "pre",
- "suf",
- "ti:base",
- "n:base",
- "v:base",
- "vdn",
- ];
- interface IWidget {
- trigger?: JSX.Element;
- id?: string;
- onSuccess?: Function;
- }
- const RelationEditWidget = ({
- trigger = <>{"trigger"}</>,
- id,
- onSuccess,
- }: IWidget) => {
- const [title, setTitle] = useState<string | undefined>(id ? "" : "新建");
- const [form] = Form.useForm<IRelation>();
- const formRef = useRef<ProFormInstance | undefined>(undefined);
- const intl = useIntl();
- return (
- <ModalForm<IRelation>
- title={title}
- trigger={trigger}
- formRef={formRef}
- form={form}
- autoFocusFirstInput
- modalProps={{
- destroyOnClose: true,
- onCancel: () => console.log("onCancel"),
- }}
- submitTimeout={3000}
- onFinish={async (values) => {
- const data = values;
- data.from = { spell: values.fromSpell, case: values.fromCase };
- data.to = { spell: values.toSpell, case: values.toCase };
- let res: IRelationResponse;
- if (typeof id === "undefined") {
- res = await post<IRelationRequest, IRelationResponse>(
- `/v2/relation`,
- data
- );
- } else {
- res = await put<IRelationRequest, IRelationResponse>(
- `/v2/relation/${id}`,
- data
- );
- }
- console.log(res);
- if (res.ok) {
- message.success("提交成功");
- if (typeof onSuccess !== "undefined") {
- onSuccess();
- }
- } else {
- message.error(res.message);
- }
- return true;
- }}
- request={
- id
- ? async () => {
- const res = await get<IRelationResponse>(`/v2/relation/${id}`);
- console.log("relation get", res);
- if (res.ok) {
- setTitle(res.data.name + "dd");
- return {
- id: id,
- name: res.data.name,
- case: res.data.case,
- from: res.data.from,
- fromCase: res.data.from?.case,
- fromSpell: res.data.from?.spell,
- to: res.data.to,
- toCase: res.data.to?.case,
- toSpell: res.data.to?.spell,
- match: res.data.match ? res.data.match : undefined,
- category: res.data.category,
- };
- } else {
- return {
- id: undefined,
- name: "",
- };
- }
- }
- : undefined
- }
- >
- <ProForm.Group>
- <ProFormText
- width="md"
- name="name"
- label={intl.formatMessage({ id: "forms.fields.name.label" })}
- />
- </ProForm.Group>
- <ProForm.Group title="从">
- <GrammarSelect name="fromCase" />
- <ProFormText
- width="md"
- name="fromSpell"
- label={intl.formatMessage({ id: "buttons.spell" })}
- />
- </ProForm.Group>
- <ProForm.Group title="连接到">
- <GrammarSelect name="toCase" />
- <ProFormText
- width="md"
- name="toSpell"
- label={intl.formatMessage({ id: "buttons.spell" })}
- />
- </ProForm.Group>
- <ProForm.Group>
- <ProFormSelect
- options={["gender", "number", "case"].map((item) => {
- return {
- value: item,
- label: item,
- };
- })}
- fieldProps={{
- mode: "tags",
- }}
- width="md"
- name="match"
- allowClear={false}
- label={intl.formatMessage({ id: "forms.fields.match.label" })}
- />
- <ProFormText
- width="md"
- name="category"
- label={intl.formatMessage({ id: "forms.fields.category.label" })}
- />
- </ProForm.Group>
- </ModalForm>
- );
- };
- export default RelationEditWidget;
|