InviteCreate.tsx 2.0 KB

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