RelationEdit.tsx 4.1 KB

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