diff --git a/packages/web/lib/networking/mutations/deleteWebhookMutation.ts b/packages/web/lib/networking/mutations/deleteWebhookMutation.ts new file mode 100644 index 000000000..0533c6b77 --- /dev/null +++ b/packages/web/lib/networking/mutations/deleteWebhookMutation.ts @@ -0,0 +1,39 @@ +import { gql } from 'graphql-request' +import { gqlFetcher } from '../networkHelpers' +import { Webhook } from '../queries/useGetWebhooksQuery' + +interface DeleteWebhookResult { + deleteWebhook: DeleteWebhook + errorCodes?: unknown[] +} + +type DeleteWebhook = { + webhook: Webhook +} + +export async function deleteWebhookMutation( + id: string +): Promise { + const mutation = gql` + mutation { + deleteWebhook(id: "${id}") { + ... on DeleteWebhookSuccess { + webhook { + id + } + } + ... on DeleteWebhookError { + errorCodes + } + } + } + ` + + try { + const data = (await gqlFetcher(mutation)) as DeleteWebhookResult + return data.errorCodes ? undefined : data.deleteWebhook.webhook.id + } catch (error) { + console.log('deleteWebhookMutation error', error) + return undefined + } +} diff --git a/packages/web/lib/networking/queries/useGetWebhooksQuery.tsx b/packages/web/lib/networking/queries/useGetWebhooksQuery.tsx new file mode 100644 index 000000000..dedd16ceb --- /dev/null +++ b/packages/web/lib/networking/queries/useGetWebhooksQuery.tsx @@ -0,0 +1,85 @@ +import { gql } from 'graphql-request' +import useSWR from 'swr' +import { publicGqlFetcher } from '../networkHelpers' + +export type WebhookEvent = + | 'PAGE_CREATED' + | 'PAGE_UPDATED' + | 'PAGE_DELETED' + | 'HIGHLIGHT_CREATED' + | 'HIGHLIGHT_UPDATED' + | 'HIGHLIGHT_DELETED' + +export interface Webhook { + id: string + url: string + eventTypes: WebhookEvent[] + contentType: string + method: string + enabled: boolean + createdAt: Date + updatedAt: Date +} + +interface WebhooksQueryResponse { + isValidating: boolean + webhooks: Webhook[] + revalidate: () => void +} + +interface WebhooksQueryResponseData { + webhooks: WebhooksData +} + +interface WebhooksData { + webhooks: unknown +} + +export function useGetWebhooksQuery(): WebhooksQueryResponse { + const query = gql` + query GetWebhooks { + webhooks { + ... on WebhooksSuccess { + webhooks { + id + url + eventTypes + contentType + method + enabled + createdAt + updatedAt + } + } + ... on WebhooksError { + errorCodes + } + } + } + ` + + const { data, mutate, error, isValidating } = useSWR(query, publicGqlFetcher) + console.log('webhooks data', data) + + try { + if (data) { + const result = data as WebhooksQueryResponseData + const webhooks = result.webhooks.webhooks as Webhook[] + return { + isValidating, + webhooks, + revalidate: () => { + mutate() + }, + } + } + } catch (error) { + console.log('error', error) + } + return { + isValidating: false, + webhooks: [], + // eslint-disable-next-line @typescript-eslint/no-empty-function + revalidate: () => {}, + } +}