TermCreate.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { useIntl } from "react-intl";
  2. import { Button, Form, message } from "antd";
  3. import {
  4. ModalForm,
  5. ProForm,
  6. ProFormText,
  7. ProFormTextArea,
  8. } from "@ant-design/pro-components";
  9. import { PlusOutlined } from "@ant-design/icons";
  10. import { ITermResponse } from "../../api/Term";
  11. import { get } from "../../../request";
  12. import LangSelect from "../LangSelect";
  13. interface IFormData {
  14. word: string;
  15. tag: string;
  16. meaning: string;
  17. meaning2: string;
  18. note: string;
  19. channel: string;
  20. lang: string;
  21. }
  22. type IWidgetDictCreate = {
  23. studio?: string;
  24. isCreate?: boolean;
  25. wordId?: string;
  26. word?: string;
  27. channel?: string;
  28. };
  29. const Widget = ({
  30. studio,
  31. isCreate,
  32. wordId,
  33. word,
  34. channel,
  35. }: IWidgetDictCreate) => {
  36. const intl = useIntl();
  37. const [form] = Form.useForm<IFormData>();
  38. const waitTime = (time: number = 100) => {
  39. return new Promise((resolve) => {
  40. setTimeout(() => {
  41. resolve(true);
  42. }, time);
  43. });
  44. };
  45. const editTrigger = (
  46. <span>
  47. {intl.formatMessage({
  48. id: "buttons.edit",
  49. })}
  50. </span>
  51. );
  52. const createTrigger = (
  53. <Button type="primary" icon={<PlusOutlined />}>
  54. {intl.formatMessage({
  55. id: "buttons.create",
  56. })}
  57. </Button>
  58. );
  59. return (
  60. <>
  61. <ModalForm<IFormData>
  62. title={intl.formatMessage({
  63. id: isCreate ? "buttons.create" : "buttons.edit",
  64. })}
  65. trigger={isCreate ? createTrigger : editTrigger}
  66. form={form}
  67. autoFocusFirstInput
  68. modalProps={{
  69. destroyOnClose: true,
  70. onCancel: () => console.log("run"),
  71. }}
  72. submitTimeout={2000}
  73. onFinish={async (values) => {
  74. await waitTime(2000);
  75. console.log(values.word);
  76. message.success("提交成功");
  77. return true;
  78. }}
  79. request={async () => {
  80. let url: string;
  81. if (typeof isCreate !== "undefined" && isCreate === false) {
  82. // 如果是编辑,就从服务器拉取数据。
  83. url = "/v2/terms/" + (isCreate ? "" : wordId);
  84. } else if (typeof channel !== "undefined") {
  85. //在channel新建
  86. url = `/v2/terms?view=createByChannel&channel=${channel}&word=${word}`;
  87. } else if (typeof studio !== "undefined") {
  88. //在studio新建
  89. url = `/v2/terms?view=createByStudio&studio=${studio}&word=${word}`;
  90. } else {
  91. return {
  92. word: "",
  93. tag: "",
  94. meaning: "",
  95. meaning2: "",
  96. note: "",
  97. lang: "",
  98. channel: "",
  99. };
  100. }
  101. console.log(url);
  102. const res = await get<ITermResponse>(url);
  103. console.log(res);
  104. return {
  105. word: res.data.word,
  106. tag: res.data.tag,
  107. meaning: res.data.meaning,
  108. meaning2: res.data.other_meaning,
  109. note: res.data.note,
  110. lang: res.data.language,
  111. channel: res.data.channal,
  112. };
  113. }}
  114. >
  115. <ProForm.Group>
  116. <ProFormText
  117. width="md"
  118. name="word"
  119. required
  120. label={intl.formatMessage({
  121. id: "dict.fields.word.label",
  122. })}
  123. rules={[
  124. {
  125. required: true,
  126. message: intl.formatMessage({
  127. id: "channel.create.message.noname",
  128. }),
  129. },
  130. ]}
  131. />
  132. <ProFormText
  133. width="md"
  134. name="tag"
  135. tooltip={intl.formatMessage({
  136. id: "term.fields.description.tooltip",
  137. })}
  138. label={intl.formatMessage({
  139. id: "term.fields.description.label",
  140. })}
  141. />
  142. </ProForm.Group>
  143. <ProForm.Group>
  144. <ProFormText
  145. width="md"
  146. name="meaning"
  147. tooltip={intl.formatMessage({
  148. id: "term.fields.meaning.tooltip",
  149. })}
  150. label={intl.formatMessage({
  151. id: "term.fields.meaning.label",
  152. })}
  153. />
  154. <ProFormText
  155. width="md"
  156. name="meaning2"
  157. tooltip={intl.formatMessage({
  158. id: "term.fields.meaning2.tooltip",
  159. })}
  160. label={intl.formatMessage({
  161. id: "term.fields.meaning2.label",
  162. })}
  163. />
  164. </ProForm.Group>
  165. <ProForm.Group>
  166. <ProFormText
  167. width="md"
  168. name="channel"
  169. tooltip={intl.formatMessage({
  170. id: "term.fields.channel.tooltip",
  171. })}
  172. label={intl.formatMessage({
  173. id: "term.fields.channel.label",
  174. })}
  175. />
  176. <LangSelect />
  177. </ProForm.Group>
  178. <ProForm.Group>
  179. <ProFormTextArea
  180. name="note"
  181. width="xl"
  182. label={intl.formatMessage({
  183. id: "forms.fields.note.label",
  184. })}
  185. />
  186. </ProForm.Group>
  187. </ModalForm>
  188. </>
  189. );
  190. };
  191. export default Widget;