Skip to content

Commit

Permalink
refactor(webhooks): migrate to new API library
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhthomas committed Nov 11, 2023
1 parent a4f3294 commit cd58518
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
22 changes: 14 additions & 8 deletions src/lib/components/webhooks/WebhookEditDrawer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
import { openModal } from 'svelte-modals';
import { superValidate } from 'sveltekit-superforms/client';
import WebhookEditDrawer from './WebhookEditDrawer.svelte';
import { WebhooksApi } from '$lib/api';
const webhooksService = useService(WebhooksApi);
type EditDrawerType = 'create' | 'update';
export const openWebhookEditDrawer = async (id?: string) => {
const webhook =
id != null
? await WebhooksService.getWebhook(id).then((w) => w.data.attributes)
? await webhooksService.getWebhook({ id }).then((w) => w.data.attributes)
: {
title: '',
trigger: WebhookTrigger.STORE_TRANSACTION,
response: WebhookResponse.TRANSACTIONS,
delivery: WebhookDelivery.JSON,
trigger: WebhookTrigger.StoreTransaction,
response: WebhookResponse.Transactions,
delivery: WebhookDelivery.Json,
url: '',
active: true
};
Expand All @@ -32,7 +35,6 @@
import Button from '../Button.svelte';
import { closeModal } from 'svelte-modals';
import {
WebhooksService,
WebhookTrigger,
WebhookResponse,
WebhookDelivery,
Expand All @@ -55,6 +57,7 @@
import ToggleField from '../form/ToggleField.svelte';
import { queryClient } from '$lib/client';
import MutationError from '../MutationError.svelte';
import { useService } from '$lib/services';
export let isOpen: boolean;
export let type: EditDrawerType;
Expand All @@ -80,8 +83,11 @@
const updateWebhookMutation = createMutation({
mutationFn: (options: MutationOptions) =>
options.type === 'create'
? WebhooksService.storeWebhook(options.webhook)
: WebhooksService.updateWebhook(options.id, options.webhook),
? webhooksService.storeWebhook({ webhookStore: options.webhook })
: webhooksService.updateWebhook({
id: options.id,
webhookUpdate: options.webhook
}),
onSuccess() {
queryClient.invalidateQueries({
queryKey: ['webhooks']
Expand All @@ -91,7 +97,7 @@
});
const deleteWebhookMutation = createMutation({
mutationFn: (id: string) => WebhooksService.deleteWebhook(id),
mutationFn: (id: string) => webhooksService.deleteWebhook({ id }),
onSuccess() {
queryClient.invalidateQueries({
queryKey: ['webhooks']
Expand Down
14 changes: 7 additions & 7 deletions src/lib/schemas/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ export const webhookSchema = z.object({
export type WebhookSchema = typeof webhookSchema;

export const triggerMap: { [key in WebhookTrigger]: string } = {
[WebhookTrigger.STORE_TRANSACTION]: 'After transaction creation',
[WebhookTrigger.UPDATE_TRANSACTION]: 'After transaction update',
[WebhookTrigger.DESTROY_TRANSACTION]: 'After transaction delete'
[WebhookTrigger.StoreTransaction]: 'After transaction creation',
[WebhookTrigger.UpdateTransaction]: 'After transaction update',
[WebhookTrigger.DestroyTransaction]: 'After transaction delete'
};

export const responseMap: { [key in WebhookResponse]: string } = {
[WebhookResponse.TRANSACTIONS]: 'Transaction details',
[WebhookResponse.ACCOUNTS]: 'Account details',
[WebhookResponse.NONE]: 'No details'
[WebhookResponse.Transactions]: 'Transaction details',
[WebhookResponse.Accounts]: 'Account details',
[WebhookResponse.None]: 'No details'
};

export const deliveryMap: { [key in WebhookDelivery]: string } = {
[WebhookDelivery.JSON]: 'JSON'
[WebhookDelivery.Json]: 'JSON'
};

export const triggerItems: SelectOptionType<WebhookTrigger>[] = Object.entries(
Expand Down
7 changes: 5 additions & 2 deletions src/routes/webhooks/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import Section from '$lib/components/layout/Section.svelte';
import { createQuery } from '@tanstack/svelte-query';
import WebhooksTable from '$lib/components/webhooks/WebhooksTable.svelte';
import { WebhooksService } from '$lib/api';
import { WebhooksApi } from '$lib/api';
import { useService } from '$lib/services';
const webhooksService = useService(WebhooksApi);
$: webhooksResult = createQuery({
queryKey: ['webhooks'],
queryFn: () => WebhooksService.listWebhook()
queryFn: () => webhooksService.listWebhook()
});
</script>

Expand Down

0 comments on commit cd58518

Please sign in to comment.