|
| 1 | +<script lang="ts"> |
| 2 | + import { enhance } from '$app/forms'; |
| 3 | + import { TrashBinOutline } from 'flowbite-svelte-icons'; |
| 4 | +
|
| 5 | + export let id: string; |
| 6 | +</script> |
| 7 | + |
| 8 | +<form method="POST" action="?/delete" use:enhance> |
| 9 | + <input type="hidden" name="id" hidden value={id} /> |
| 10 | + <!-- <button type="submit" on:click={(e) => !confirm('Are you sure?') && e.preventDefault()}> --> |
| 11 | + <button type="submit"> |
| 12 | + <TrashBinOutline class="text-red-500 dark:text-blue-400" /> |
| 13 | + </button> |
| 14 | +</form> |
| 15 | + |
| 16 | +<!-- <script lang="ts"> |
| 17 | + // Paired with this form action |
| 18 | + import { DeleteRuleStore } from '$houdini'; |
| 19 | + import { ToastLevel } from '$lib/components/toast'; |
| 20 | + import { ruleDeleteSchema as schema } from '$lib/models/schema'; |
| 21 | + import { Logger } from '$lib/utils'; |
| 22 | + import { fail } from '@sveltejs/kit'; |
| 23 | + import type { GraphQLError } from 'graphql'; |
| 24 | + import { redirect } from 'sveltekit-flash-message/server'; |
| 25 | + import { setError, setMessage, superValidate } from 'sveltekit-superforms/server'; |
| 26 | +
|
| 27 | + const log = new Logger('rule.action.server'); |
| 28 | +
|
| 29 | + const deleteRuleStore = new DeleteRuleStore(); |
| 30 | + export const actions = { |
| 31 | + delete: async (event) => { |
| 32 | + const { request, locals } = event; |
| 33 | + const session = await locals.auth(); |
| 34 | + if (session?.user == undefined) { |
| 35 | + throw redirect(307, '/auth/signin?callbackUrl=/dashboard/rules'); |
| 36 | + } |
| 37 | +
|
| 38 | + const form = await superValidate(request,zod(schema)); |
| 39 | + log.debug({ form }); |
| 40 | +
|
| 41 | + // superform validation |
| 42 | + if (!form.valid) return fail(400, { form }); |
| 43 | +
|
| 44 | + const variables = { id: form.data.id, deletedAt: new Date() }; |
| 45 | + log.debug('DELETE action variables:', variables); |
| 46 | + const { errors, data } = await deleteRuleStore.mutate(variables, { |
| 47 | + metadata: { logResult: true }, |
| 48 | + event |
| 49 | + }); |
| 50 | + if (errors) { |
| 51 | + errors.forEach((error) => { |
| 52 | + log.error('delete rule gql error', error); |
| 53 | + setError(form, '', (error as GraphQLError).message); |
| 54 | + }); |
| 55 | + return setMessage(form, 'Delete rule failed'); |
| 56 | + } |
| 57 | +
|
| 58 | + const result = data?.update_rules_by_pk; |
| 59 | + if (!result) return setMessage(form, 'Delete rule failed: responce empty', { status: 404 }); |
| 60 | + const message = { |
| 61 | + message: `Rule: ${result.displayName} deleted`, |
| 62 | + dismissible: true, |
| 63 | + duration: 10000, |
| 64 | + type: ToastLevel.Success |
| 65 | + } as const; |
| 66 | + throw redirect(302, '/dashboard/rules', message, event); |
| 67 | + } |
| 68 | + }; |
| 69 | +</script> --> |
0 commit comments