CourseCreate.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { useIntl } from "react-intl";
  2. import {
  3. ProForm,
  4. type ProFormInstance,
  5. ProFormText,
  6. } from "@ant-design/pro-components";
  7. import { message } from "antd";
  8. import { post } from "../../request";
  9. import type { 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 CourseCreateWidget = ({ studio = "", onCreate }: IWidgetCourseCreate) => {
  22. const intl = useIntl();
  23. const formRef = useRef<ProFormInstance | undefined>(undefined);
  24. return (
  25. <ProForm<IFormData>
  26. formRef={formRef}
  27. onFinish={async (values: IFormData) => {
  28. console.log(values);
  29. values.studio = studio;
  30. const url = `/v2/course`;
  31. console.info("CourseCreateWidget api request", url, values);
  32. const res = await post<ICourseCreateRequest, ICourseResponse>(
  33. url,
  34. values
  35. );
  36. console.debug("CourseCreateWidget api response", res);
  37. if (res.ok) {
  38. message.success(intl.formatMessage({ id: "flashes.success" }));
  39. formRef.current?.resetFields(["title"]);
  40. if (typeof onCreate !== "undefined") {
  41. onCreate();
  42. }
  43. } else {
  44. message.error(res.message);
  45. }
  46. }}
  47. >
  48. <ProForm.Group>
  49. <ProFormText
  50. width="md"
  51. name="title"
  52. required
  53. label={intl.formatMessage({ id: "channel.name" })}
  54. rules={[
  55. {
  56. required: true,
  57. message: intl.formatMessage({
  58. id: "channel.create.message.noname",
  59. }),
  60. },
  61. ]}
  62. />
  63. </ProForm.Group>
  64. <ProForm.Group>
  65. <LangSelect />
  66. </ProForm.Group>
  67. </ProForm>
  68. );
  69. };
  70. export default CourseCreateWidget;