|
| 1 | +import { useCallback, useEffect, useState } from "react"; |
| 2 | +import { Button, Flex, Sheet, Text } from "@raystack/apsara"; |
| 3 | +import { useNavigate, useParams } from "react-router-dom"; |
| 4 | +import { SheetHeader } from "~/components/sheet/header"; |
| 5 | +import * as z from "zod"; |
| 6 | +import { FormProvider, useForm } from "react-hook-form"; |
| 7 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 8 | +import { Form, FormSubmit } from "@radix-ui/react-form"; |
| 9 | +import { CustomFieldName } from "~/components/CustomField"; |
| 10 | +import events from "~/utils/webhook_events"; |
| 11 | +import { SheetFooter } from "~/components/sheet/footer"; |
| 12 | +import { useFrontier } from "@raystack/frontier/react"; |
| 13 | +import { V1Beta1Webhook, V1Beta1WebhookRequestBody } from "@raystack/frontier"; |
| 14 | +import { toast } from "sonner"; |
| 15 | + |
| 16 | +const UpdateWebhookSchema = z.object({ |
| 17 | + url: z.string().trim().url(), |
| 18 | + description: z |
| 19 | + .string() |
| 20 | + .trim() |
| 21 | + .min(3, { message: "Must be 10 or more characters long" }), |
| 22 | + state: z.boolean().default(false), |
| 23 | + subscribed_events: z.array(z.string()).default([]), |
| 24 | +}); |
| 25 | + |
| 26 | +export type UpdateWebhook = z.infer<typeof UpdateWebhookSchema>; |
| 27 | + |
| 28 | +export default function UpdateWebhooks() { |
| 29 | + const navigate = useNavigate(); |
| 30 | + const { client } = useFrontier(); |
| 31 | + |
| 32 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 33 | + |
| 34 | + const [isWebhookLoading, setIsWebhookLoading] = useState(false); |
| 35 | + const [webhook, setWebhook] = useState<V1Beta1Webhook>(); |
| 36 | + |
| 37 | + const { webhookId = "" } = useParams(); |
| 38 | + |
| 39 | + const onClose = useCallback(() => { |
| 40 | + navigate("/webhooks"); |
| 41 | + }, [navigate]); |
| 42 | + |
| 43 | + const methods = useForm<UpdateWebhook>({ |
| 44 | + resolver: zodResolver(UpdateWebhookSchema), |
| 45 | + defaultValues: {}, |
| 46 | + }); |
| 47 | + |
| 48 | + const onSubmit = async (data: UpdateWebhook) => { |
| 49 | + console.log("called"); |
| 50 | + try { |
| 51 | + setIsSubmitting(true); |
| 52 | + const body: V1Beta1WebhookRequestBody = { |
| 53 | + ...data, |
| 54 | + state: data.state ? "enabled" : "disabled", |
| 55 | + }; |
| 56 | + const resp = await client?.adminServiceUpdateWebhook(webhookId, { |
| 57 | + body, |
| 58 | + }); |
| 59 | + |
| 60 | + if (resp?.data?.webhook) { |
| 61 | + toast.success("Webhook updated"); |
| 62 | + } |
| 63 | + } catch (err) { |
| 64 | + toast.error("Something went wrong"); |
| 65 | + } finally { |
| 66 | + setIsSubmitting(false); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + useEffect(() => { |
| 71 | + async function getWebhookDetails(id: string) { |
| 72 | + try { |
| 73 | + setIsWebhookLoading(true); |
| 74 | + const resp = await client?.adminServiceListWebhooks(); |
| 75 | + const webhooks = resp?.data?.webhooks || []; |
| 76 | + const webhookData = webhooks?.find((wb) => wb?.id === id); |
| 77 | + setWebhook(webhookData); |
| 78 | + methods?.reset({ |
| 79 | + ...webhookData, |
| 80 | + state: webhookData?.state === "enabled", |
| 81 | + }); |
| 82 | + } catch (err) { |
| 83 | + console.error(err); |
| 84 | + } finally { |
| 85 | + setIsWebhookLoading(false); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (webhookId) { |
| 90 | + getWebhookDetails(webhookId); |
| 91 | + } |
| 92 | + }, [client, methods, webhookId]); |
| 93 | + |
| 94 | + return ( |
| 95 | + <Sheet open={true}> |
| 96 | + <Sheet.Content |
| 97 | + side="right" |
| 98 | + // @ts-ignore |
| 99 | + style={{ |
| 100 | + width: "30vw", |
| 101 | + padding: 0, |
| 102 | + borderRadius: "var(--pd-8)", |
| 103 | + boxShadow: "var(--shadow-sm)", |
| 104 | + }} |
| 105 | + close={false} |
| 106 | + > |
| 107 | + <FormProvider {...methods}> |
| 108 | + <Form onSubmit={methods.handleSubmit(onSubmit)}> |
| 109 | + <SheetHeader |
| 110 | + title="Update Webhook" |
| 111 | + onClick={onClose} |
| 112 | + data-test-id="admin-ui-update-webhook-close-btn" |
| 113 | + /> |
| 114 | + <Flex direction="column" gap="large" style={styles.main}> |
| 115 | + <CustomFieldName |
| 116 | + name="url" |
| 117 | + defaultValue={webhook?.url} |
| 118 | + register={methods.register} |
| 119 | + control={methods.control} |
| 120 | + variant="textarea" |
| 121 | + style={{ width: "100%" }} |
| 122 | + isLoading={isWebhookLoading} |
| 123 | + /> |
| 124 | + <CustomFieldName |
| 125 | + name="description" |
| 126 | + defaultValue={webhook?.description} |
| 127 | + register={methods.register} |
| 128 | + control={methods.control} |
| 129 | + variant="textarea" |
| 130 | + style={{ width: "100%" }} |
| 131 | + isLoading={isWebhookLoading} |
| 132 | + /> |
| 133 | + <CustomFieldName |
| 134 | + name="subscribed_events" |
| 135 | + defaultValue={webhook?.subscribed_events} |
| 136 | + register={methods.register} |
| 137 | + control={methods.control} |
| 138 | + variant="multiselect" |
| 139 | + options={events.map((e) => ({ label: e, value: e }))} |
| 140 | + isLoading={isWebhookLoading} |
| 141 | + /> |
| 142 | + <CustomFieldName |
| 143 | + name="state" |
| 144 | + defaultChecked={webhook?.state === "enabled"} |
| 145 | + register={methods.register} |
| 146 | + control={methods.control} |
| 147 | + variant="switch" |
| 148 | + isLoading={isWebhookLoading} |
| 149 | + /> |
| 150 | + </Flex> |
| 151 | + <SheetFooter> |
| 152 | + <FormSubmit asChild> |
| 153 | + <Button |
| 154 | + variant="primary" |
| 155 | + style={{ height: "inherit" }} |
| 156 | + disabled={isSubmitting || isWebhookLoading} |
| 157 | + data-test-id="admin-ui-submit-btn" |
| 158 | + > |
| 159 | + <Text |
| 160 | + size={4} |
| 161 | + style={{ color: "var(--foreground-inverted)" }} |
| 162 | + > |
| 163 | + {isSubmitting ? "Updating..." : "Update Webhook"} |
| 164 | + </Text> |
| 165 | + </Button> |
| 166 | + </FormSubmit> |
| 167 | + </SheetFooter> |
| 168 | + </Form> |
| 169 | + </FormProvider> |
| 170 | + </Sheet.Content> |
| 171 | + </Sheet> |
| 172 | + ); |
| 173 | +} |
| 174 | + |
| 175 | +const styles = { |
| 176 | + main: { |
| 177 | + padding: "32px", |
| 178 | + margin: 0, |
| 179 | + height: "calc(100vh - 125px)", |
| 180 | + overflow: "auto", |
| 181 | + }, |
| 182 | + formfield: { |
| 183 | + width: "80%", |
| 184 | + marginBottom: "40px", |
| 185 | + }, |
| 186 | + select: { |
| 187 | + height: "32px", |
| 188 | + borderRadius: "8px", |
| 189 | + padding: "8px", |
| 190 | + border: "none", |
| 191 | + backgroundColor: "transparent", |
| 192 | + "&:active,&:focus": { |
| 193 | + border: "none", |
| 194 | + outline: "none", |
| 195 | + boxShadow: "none", |
| 196 | + }, |
| 197 | + }, |
| 198 | +}; |
0 commit comments