CourseCreate.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 { ICourseCreateRequest, ICourseResponse } from "../api/Course";
  10. import LangSelect from "../general/LangSelect";
  11. import { useRef } from "react";
  12. interface IFormData {
  13. title: string;
  14. lang: string;
  15. studio: string;
  16. }
  17. interface IWidgetCourseCreate {
  18. studio?: string;
  19. onCreate?: Function;
  20. }
  21. const Widget = ({ studio = "", onCreate }: IWidgetCourseCreate) => {
  22. const intl = useIntl();
  23. const formRef = useRef<ProFormInstance>();
  24. return (
  25. <ProForm<IFormData>
  26. formRef={formRef}
  27. onFinish={async (values: IFormData) => {
  28. console.log(values);
  29. values.studio = studio;
  30. const res = await post<ICourseCreateRequest, ICourseResponse>(
  31. `/v2/course`,
  32. values
  33. );
  34. console.log(res);
  35. if (res.ok) {
  36. message.success(intl.formatMessage({ id: "flashes.success" }));
  37. formRef.current?.resetFields(["title"]);
  38. if (typeof onCreate !== "undefined") {
  39. onCreate();
  40. }
  41. } else {
  42. message.error(res.message);
  43. }
  44. }}
  45. >
  46. <ProForm.Group>
  47. <ProFormText
  48. width="md"
  49. name="title"
  50. required
  51. label={intl.formatMessage({ id: "channel.name" })}
  52. rules={[
  53. {
  54. required: true,
  55. message: intl.formatMessage({
  56. id: "channel.create.message.noname",
  57. }),
  58. },
  59. ]}
  60. />
  61. </ProForm.Group>
  62. <ProForm.Group>
  63. <LangSelect />
  64. </ProForm.Group>
  65. </ProForm>
  66. );
  67. };
  68. export default Widget;