InviteCreate.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 { useRef } from "react";
  10. import LangSelect from "../general/LangSelect";
  11. import { dashboardBasePath } from "../../utils";
  12. import { IInviteRequest, IInviteResponse } from "../api/Auth";
  13. interface IFormData {
  14. email: string;
  15. lang: string;
  16. }
  17. interface IWidget {
  18. studio?: string;
  19. onCreate?: Function;
  20. }
  21. const InviteCreateWidget = ({ studio, onCreate }: IWidget) => {
  22. const intl = useIntl();
  23. const formRef = useRef<ProFormInstance>();
  24. return (
  25. <ProForm<IFormData>
  26. formRef={formRef}
  27. onFinish={async (values: IFormData) => {
  28. if (typeof studio === "undefined") {
  29. return;
  30. }
  31. const url = `/v2/invite`;
  32. const data: IInviteRequest = {
  33. email: values.email,
  34. lang: values.lang,
  35. studio: studio,
  36. dashboard: dashboardBasePath(),
  37. };
  38. console.info("api request", values);
  39. const res = await post<IInviteRequest, IInviteResponse>(url, data);
  40. console.debug("api response", res);
  41. if (res.ok) {
  42. message.success(intl.formatMessage({ id: "flashes.success" }));
  43. if (typeof onCreate !== "undefined") {
  44. onCreate();
  45. formRef.current?.resetFields();
  46. }
  47. } else {
  48. message.error(res.message);
  49. }
  50. }}
  51. >
  52. <ProForm.Group>
  53. <ProFormText
  54. width="md"
  55. name="email"
  56. required
  57. label={intl.formatMessage({ id: "forms.fields.email.label" })}
  58. rules={[
  59. {
  60. required: true,
  61. type: "email",
  62. },
  63. ]}
  64. />
  65. </ProForm.Group>
  66. <ProForm.Group>
  67. <LangSelect />
  68. </ProForm.Group>
  69. </ProForm>
  70. );
  71. };
  72. export default InviteCreateWidget;