NissayaEndingEdit.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { ModalForm, ProForm, ProFormText } from "@ant-design/pro-components";
  2. import { Form, message } from "antd";
  3. import { useState } from "react";
  4. import { useIntl } from "react-intl";
  5. import type {
  6. INissayaEnding,
  7. INissayaEndingRequest,
  8. INissayaEndingResponse,
  9. } from "../../../pages/admin/nissaya-ending/list";
  10. import { get, post, put } from "../../../request";
  11. import LangSelect from "../../general/LangSelect";
  12. import GrammarSelect from "./GrammarSelect";
  13. interface IWidget {
  14. trigger?: JSX.Element;
  15. id?: string;
  16. onSuccess?: Function;
  17. }
  18. const NissayaEndingWidget = ({
  19. trigger = <>{"trigger"}</>,
  20. id,
  21. onSuccess,
  22. }: IWidget) => {
  23. const [title, setTitle] = useState<string | undefined>(id ? "" : "新建");
  24. const [form] = Form.useForm<INissayaEnding>();
  25. const intl = useIntl();
  26. return (
  27. <ModalForm<INissayaEnding>
  28. title={title}
  29. trigger={trigger}
  30. form={form}
  31. autoFocusFirstInput
  32. modalProps={{
  33. destroyOnClose: true,
  34. onCancel: () => console.log("run"),
  35. }}
  36. submitTimeout={2000}
  37. onFinish={async (values) => {
  38. const data = values;
  39. data.from = {
  40. spell: values.fromSpell,
  41. case: values.fromCase ? values.fromCase : undefined,
  42. };
  43. let res: INissayaEndingResponse;
  44. if (typeof id === "undefined") {
  45. res = await post<INissayaEndingRequest, INissayaEndingResponse>(
  46. `/v2/nissaya-ending`,
  47. data
  48. );
  49. } else {
  50. res = await put<INissayaEndingRequest, INissayaEndingResponse>(
  51. `/v2/nissaya-ending/${id}`,
  52. data
  53. );
  54. }
  55. console.log(res);
  56. if (res.ok) {
  57. message.success("提交成功");
  58. if (typeof onSuccess !== "undefined") {
  59. onSuccess();
  60. }
  61. } else {
  62. message.error(res.message);
  63. }
  64. return true;
  65. }}
  66. request={
  67. id
  68. ? async () => {
  69. const res = await get<INissayaEndingResponse>(
  70. `/v2/nissaya-ending/${id}`
  71. );
  72. console.log("nissaya-ending get", res);
  73. if (res.ok) {
  74. setTitle(res.data.ending);
  75. return {
  76. id: id,
  77. ending: res.data.ending,
  78. relation: res.data.relation,
  79. from: res.data.from,
  80. fromCase: res.data.from?.case,
  81. fromSpell: res.data.from?.spell,
  82. lang: res.data.lang,
  83. };
  84. } else {
  85. return {
  86. id: undefined,
  87. ending: "",
  88. relation: "",
  89. lang: "",
  90. };
  91. }
  92. }
  93. : undefined
  94. }
  95. >
  96. <ProForm.Group>
  97. <ProFormText
  98. width="md"
  99. name="ending"
  100. label={intl.formatMessage({ id: "forms.fields.ending.label" })}
  101. tooltip="最长为 24 位"
  102. />
  103. <LangSelect width="md" />
  104. </ProForm.Group>
  105. <ProForm.Group>
  106. <GrammarSelect name="fromCase" />
  107. <ProFormText
  108. width="md"
  109. name="fromSpell"
  110. label={intl.formatMessage({ id: "buttons.spell" })}
  111. />
  112. </ProForm.Group>
  113. <ProForm.Group>
  114. <ProFormText
  115. width="md"
  116. name="relation"
  117. label={intl.formatMessage({ id: "forms.fields.relation.label" })}
  118. />
  119. </ProForm.Group>
  120. </ModalForm>
  121. );
  122. };
  123. export default NissayaEndingWidget;