RelationEdit.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import {
  2. ModalForm,
  3. ProForm,
  4. type ProFormInstance,
  5. ProFormSelect,
  6. ProFormText,
  7. } from "@ant-design/pro-components";
  8. import { Form, message } from "antd";
  9. import { useRef, useState } from "react";
  10. import { useIntl } from "react-intl";
  11. import type {
  12. IRelation,
  13. IRelationRequest,
  14. IRelationResponse,
  15. } from "../../../pages/admin/relation/list";
  16. import { get, post, put } from "../../../request";
  17. import GrammarSelect from "./GrammarSelect";
  18. export const _verb = [
  19. "n",
  20. "ti",
  21. "v",
  22. "v:ind",
  23. "ind",
  24. "sg",
  25. "pl",
  26. "nom",
  27. "acc",
  28. "gen",
  29. "dat",
  30. "inst",
  31. "voc",
  32. "abl",
  33. "loc",
  34. "base",
  35. "imp",
  36. "opt",
  37. "pres",
  38. "aor",
  39. "fut",
  40. "1p",
  41. "2p",
  42. "3p",
  43. "prp",
  44. "pp",
  45. "grd",
  46. "fpp",
  47. "vdn",
  48. "ger",
  49. "inf",
  50. "adj",
  51. "pron",
  52. "caus",
  53. "num",
  54. "adv",
  55. "conj",
  56. "pre",
  57. "suf",
  58. "ti:base",
  59. "n:base",
  60. "v:base",
  61. "vdn",
  62. ];
  63. interface IWidget {
  64. trigger?: JSX.Element;
  65. id?: string;
  66. onSuccess?: Function;
  67. }
  68. const RelationEditWidget = ({
  69. trigger = <>{"trigger"}</>,
  70. id,
  71. onSuccess,
  72. }: IWidget) => {
  73. const [title, setTitle] = useState<string | undefined>(id ? "" : "新建");
  74. const [form] = Form.useForm<IRelation>();
  75. const formRef = useRef<ProFormInstance | undefined>(undefined);
  76. const intl = useIntl();
  77. return (
  78. <ModalForm<IRelation>
  79. title={title}
  80. trigger={trigger}
  81. formRef={formRef}
  82. form={form}
  83. autoFocusFirstInput
  84. modalProps={{
  85. destroyOnClose: true,
  86. onCancel: () => console.log("onCancel"),
  87. }}
  88. submitTimeout={3000}
  89. onFinish={async (values) => {
  90. const data = values;
  91. data.from = { spell: values.fromSpell, case: values.fromCase };
  92. data.to = { spell: values.toSpell, case: values.toCase };
  93. let res: IRelationResponse;
  94. if (typeof id === "undefined") {
  95. res = await post<IRelationRequest, IRelationResponse>(
  96. `/v2/relation`,
  97. data
  98. );
  99. } else {
  100. res = await put<IRelationRequest, IRelationResponse>(
  101. `/v2/relation/${id}`,
  102. data
  103. );
  104. }
  105. console.log(res);
  106. if (res.ok) {
  107. message.success("提交成功");
  108. if (typeof onSuccess !== "undefined") {
  109. onSuccess();
  110. }
  111. } else {
  112. message.error(res.message);
  113. }
  114. return true;
  115. }}
  116. request={
  117. id
  118. ? async () => {
  119. const res = await get<IRelationResponse>(`/v2/relation/${id}`);
  120. console.log("relation get", res);
  121. if (res.ok) {
  122. setTitle(res.data.name + "dd");
  123. return {
  124. id: id,
  125. name: res.data.name,
  126. case: res.data.case,
  127. from: res.data.from,
  128. fromCase: res.data.from?.case,
  129. fromSpell: res.data.from?.spell,
  130. to: res.data.to,
  131. toCase: res.data.to?.case,
  132. toSpell: res.data.to?.spell,
  133. match: res.data.match ? res.data.match : undefined,
  134. category: res.data.category,
  135. };
  136. } else {
  137. return {
  138. id: undefined,
  139. name: "",
  140. };
  141. }
  142. }
  143. : undefined
  144. }
  145. >
  146. <ProForm.Group>
  147. <ProFormText
  148. width="md"
  149. name="name"
  150. label={intl.formatMessage({ id: "forms.fields.name.label" })}
  151. />
  152. </ProForm.Group>
  153. <ProForm.Group title="从">
  154. <GrammarSelect name="fromCase" />
  155. <ProFormText
  156. width="md"
  157. name="fromSpell"
  158. label={intl.formatMessage({ id: "buttons.spell" })}
  159. />
  160. </ProForm.Group>
  161. <ProForm.Group title="连接到">
  162. <GrammarSelect name="toCase" />
  163. <ProFormText
  164. width="md"
  165. name="toSpell"
  166. label={intl.formatMessage({ id: "buttons.spell" })}
  167. />
  168. </ProForm.Group>
  169. <ProForm.Group>
  170. <ProFormSelect
  171. options={["gender", "number", "case"].map((item) => {
  172. return {
  173. value: item,
  174. label: item,
  175. };
  176. })}
  177. fieldProps={{
  178. mode: "tags",
  179. }}
  180. width="md"
  181. name="match"
  182. allowClear={false}
  183. label={intl.formatMessage({ id: "forms.fields.match.label" })}
  184. />
  185. <ProFormText
  186. width="md"
  187. name="category"
  188. label={intl.formatMessage({ id: "forms.fields.category.label" })}
  189. />
  190. </ProForm.Group>
  191. </ModalForm>
  192. );
  193. };
  194. export default RelationEditWidget;