WebhookEdit.tsx 4.6 KB

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