ArticleCreate.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { useIntl } from "react-intl";
  2. import {
  3. ProForm,
  4. ProFormInstance,
  5. ProFormText,
  6. } from "@ant-design/pro-components";
  7. import { message } from "antd";
  8. import { post } from "../../request";
  9. import { IArticleCreateRequest, IArticleResponse } from "../api/Article";
  10. import LangSelect from "../general/LangSelect";
  11. import { useRef } from "react";
  12. interface IFormData {
  13. title: string;
  14. lang: string;
  15. studio: string;
  16. anthologyId?: string;
  17. }
  18. interface IWidget {
  19. studio?: string;
  20. anthologyId?: string;
  21. onSuccess?: Function;
  22. }
  23. const ArticleCreateWidget = ({ studio, anthologyId, onSuccess }: IWidget) => {
  24. const intl = useIntl();
  25. const formRef = useRef<ProFormInstance>();
  26. return (
  27. <ProForm<IFormData>
  28. formRef={formRef}
  29. onFinish={async (values: IFormData) => {
  30. console.log(values);
  31. if (typeof studio === "undefined") {
  32. return;
  33. }
  34. values.studio = studio;
  35. values.anthologyId = anthologyId;
  36. const res = await post<IArticleCreateRequest, IArticleResponse>(
  37. `/v2/article`,
  38. values
  39. );
  40. console.log(res);
  41. if (res.ok) {
  42. message.success(intl.formatMessage({ id: "flashes.success" }));
  43. if (typeof onSuccess !== "undefined") {
  44. onSuccess();
  45. formRef.current?.resetFields(["title"]);
  46. }
  47. } else {
  48. message.error(res.message);
  49. }
  50. }}
  51. >
  52. <ProForm.Group>
  53. <ProFormText
  54. width="md"
  55. name="title"
  56. required
  57. label={intl.formatMessage({ id: "channel.name" })}
  58. rules={[
  59. {
  60. required: true,
  61. message: intl.formatMessage({
  62. id: "channel.create.message.noname",
  63. }),
  64. },
  65. ]}
  66. />
  67. </ProForm.Group>
  68. <ProForm.Group>
  69. <LangSelect />
  70. </ProForm.Group>
  71. </ProForm>
  72. );
  73. };
  74. export default ArticleCreateWidget;