WebhookEdit.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import {
  2. ProForm,
  3. type ProFormInstance,
  4. ProFormSelect,
  5. ProFormText,
  6. } from "@ant-design/pro-components";
  7. import { message, Space, Typography } from "antd";
  8. import { useRef, useState } from "react";
  9. import { useIntl } from "react-intl";
  10. import { Link } from "react-router";
  11. import { get, post, put } from "../../request";
  12. import type {
  13. IWebhookEvent,
  14. IWebhookRequest,
  15. IWebhookResponse,
  16. } from "../../api/webhook";
  17. import WebhookTpl from "./WebhookTpl";
  18. import type { TResType } from "../../api/discussion";
  19. const { Title } = Typography;
  20. interface IWidget {
  21. studioName?: string;
  22. channelId?: string;
  23. id?: string;
  24. res_type?: TResType;
  25. res_id?: string;
  26. onSuccess?: () => void;
  27. }
  28. const WebhookEditWidget = ({
  29. studioName,
  30. channelId,
  31. id,
  32. res_type = "channel",
  33. res_id = "",
  34. onSuccess,
  35. }: IWidget) => {
  36. const formRef = useRef<ProFormInstance | undefined>(undefined);
  37. const [event, setEvent] = useState<IWebhookEvent[]>([]);
  38. const intl = useIntl();
  39. return (
  40. <Space orientation="vertical">
  41. <Title level={4}>
  42. <Link
  43. to={`/studio/${studioName}/channel/${channelId}/setting/webhooks`}
  44. >
  45. List
  46. </Link>{" "}
  47. / {id ? "Manage webhook" : "New"}
  48. </Title>
  49. <ProForm<IWebhookRequest>
  50. formRef={formRef}
  51. autoFocusFirstInput
  52. onFinish={async (values) => {
  53. console.log("submit", values);
  54. const data: IWebhookRequest = values;
  55. data.res_id = res_id;
  56. data.res_type = res_type;
  57. data.event2 = event;
  58. let res: IWebhookResponse;
  59. if (typeof id === "undefined") {
  60. res = await post<IWebhookRequest, IWebhookResponse>(
  61. `/v2/webhook`,
  62. data
  63. );
  64. } else {
  65. res = await put<IWebhookRequest, IWebhookResponse>(
  66. `/v2/webhook/${id}`,
  67. data
  68. );
  69. }
  70. console.log(res);
  71. if (res.ok) {
  72. message.success("提交成功");
  73. if (typeof onSuccess !== "undefined") {
  74. onSuccess();
  75. }
  76. } else {
  77. message.error(res.message);
  78. }
  79. return true;
  80. }}
  81. request={
  82. id
  83. ? async () => {
  84. const url = `/v2/webhook/${id}`;
  85. const res: IWebhookResponse = await get<IWebhookResponse>(url);
  86. console.log("get", res);
  87. if (res.ok) {
  88. return res.data;
  89. } else {
  90. return {
  91. res_type: res_type,
  92. res_id: res_id,
  93. url: "",
  94. receiver: "wechat",
  95. event: [],
  96. status: "normal",
  97. };
  98. }
  99. }
  100. : undefined
  101. }
  102. >
  103. <ProForm.Group>
  104. <ProFormText
  105. width="md"
  106. required
  107. name="url"
  108. label={intl.formatMessage({ id: "forms.fields.url.label" })}
  109. />
  110. </ProForm.Group>
  111. <ProForm.Group>
  112. <ProFormSelect
  113. options={[
  114. { value: "wechat", label: "wechat" },
  115. { value: "dingtalk", label: "dingtalk" },
  116. ]}
  117. width="md"
  118. required
  119. name={"receiver"}
  120. allowClear={false}
  121. label={intl.formatMessage({ id: "forms.fields.receiver.label" })}
  122. />
  123. </ProForm.Group>
  124. <ProForm.Group>
  125. <ProFormSelect
  126. placeholder={"全部事件"}
  127. options={["pr", "discussion", "content"].map((item) => {
  128. return {
  129. value: item,
  130. label: item,
  131. };
  132. })}
  133. fieldProps={{
  134. mode: "tags",
  135. }}
  136. width="md"
  137. name="event"
  138. allowClear={false}
  139. label={intl.formatMessage({ id: "forms.fields.event.label" })}
  140. />
  141. </ProForm.Group>
  142. <ProForm.Group>
  143. <ProFormSelect
  144. placeholder={"active"}
  145. options={["active", "disable"].map((item) => {
  146. return {
  147. value: item,
  148. label: item,
  149. };
  150. })}
  151. width="md"
  152. name="status"
  153. allowClear={false}
  154. label={intl.formatMessage({ id: "forms.fields.status.label" })}
  155. />
  156. </ProForm.Group>
  157. <ProForm.Group>
  158. <WebhookTpl onChange={(value: IWebhookEvent[]) => setEvent(value)} />
  159. </ProForm.Group>
  160. </ProForm>
  161. </Space>
  162. );
  163. };
  164. export default WebhookEditWidget;