|
| 1 | +<script lang="ts"> |
| 2 | + import { defaults, superForm } from 'sveltekit-superforms'; |
| 3 | + import { zod } from 'sveltekit-superforms/adapters'; |
| 4 | + import { createEventDispatcher } from 'svelte'; |
| 5 | + import { writable } from 'svelte/store'; |
| 6 | + import { z } from 'zod'; |
| 7 | +
|
| 8 | + import * as Select from '$lib/components/ui/select/index.js'; |
| 9 | + import * as Sheet from '$lib/components/ui/sheet'; |
| 10 | + import { Textarea } from '$lib/components/ui/textarea'; |
| 11 | +
|
| 12 | + import * as Form from '$lib/components/form'; |
| 13 | +
|
| 14 | + import type { Acl, AclData } from '$lib/api'; |
| 15 | +
|
| 16 | + import { errorToast, successToast } from '$lib/utils/toast'; |
| 17 | + import { formatError } from '$lib/utils/error'; |
| 18 | + import Input from '$lib/components/ui/input/input.svelte'; |
| 19 | +
|
| 20 | + export let acl: Acl; |
| 21 | +
|
| 22 | + const dispatch = createEventDispatcher<{ submit: undefined }>(); |
| 23 | +
|
| 24 | + let mainSheet: InstanceType<typeof Sheet.Root>; |
| 25 | +
|
| 26 | + const schema: z.ZodType<Omit<AclData['hosts'][0], 'id'>> = z.object({ |
| 27 | + name: z.string(), |
| 28 | + cidr: z.string(), |
| 29 | + comments: z.array(z.string()).default([]) |
| 30 | + }); |
| 31 | +
|
| 32 | + const form = superForm(defaults(zod(schema)), { |
| 33 | + SPA: true, |
| 34 | + dataType: 'json', |
| 35 | + invalidateAll: true, |
| 36 | + validators: zod(schema), |
| 37 | + async onUpdate({ form }) { |
| 38 | + if (form.valid) { |
| 39 | + try { |
| 40 | + acl.hosts.push(form.data); |
| 41 | +
|
| 42 | + const { error } = await acl.save(); |
| 43 | + if (error) throw error; |
| 44 | +
|
| 45 | + successToast(`Created new host ${form.data.name}`); |
| 46 | + dispatch('submit'); |
| 47 | + mainSheet.close(); |
| 48 | + } catch (err) { |
| 49 | + console.error(err); |
| 50 | + errorToast(formatError(err)); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + }); |
| 55 | +
|
| 56 | + const { form: formData } = form; |
| 57 | +
|
| 58 | + const description = writable<string>(''); |
| 59 | +
|
| 60 | + description.subscribe((desc) => { |
| 61 | + formData.update((data) => ({ ...data, comments: desc.split(/\n{1,}/gm).map((i) => i.trim()) })); |
| 62 | + }); |
| 63 | +</script> |
| 64 | + |
| 65 | +<Sheet.Root bind:this={mainSheet}> |
| 66 | + <Sheet.Trigger asChild let:builder> |
| 67 | + <slot name="trigger" {builder} /> |
| 68 | + </Sheet.Trigger> |
| 69 | + |
| 70 | + <Sheet.Content side="left"> |
| 71 | + <Sheet.Header> |
| 72 | + <Sheet.Title>Create host</Sheet.Title> |
| 73 | + </Sheet.Header> |
| 74 | + |
| 75 | + <Form.Root {form} submitText="Create"> |
| 76 | + <Form.Field {form} name="name" let:constraints> |
| 77 | + <Form.Control let:attrs> |
| 78 | + <Form.Label class="required" for={attrs.id}>Name</Form.Label> |
| 79 | + <Input {...constraints} {...attrs} bind:value={$formData.name} /> |
| 80 | + <Form.FieldErrors /> |
| 81 | + </Form.Control> |
| 82 | + </Form.Field> |
| 83 | + |
| 84 | + <Form.Field {form} name="cidr" let:constraints> |
| 85 | + <Form.Control let:attrs> |
| 86 | + <Form.Label class="required" for={attrs.id}>CIDR</Form.Label> |
| 87 | + <Input {...constraints} {...attrs} bind:value={$formData.cidr} /> |
| 88 | + <Form.FieldErrors /> |
| 89 | + </Form.Control> |
| 90 | + </Form.Field> |
| 91 | + |
| 92 | + <Form.Field {form} name="comments" let:constraints> |
| 93 | + <Form.Control let:attrs> |
| 94 | + <Form.Label for={attrs.id}>Description</Form.Label> |
| 95 | + <Textarea {...constraints} {...attrs} bind:value={$description} /> |
| 96 | + </Form.Control> |
| 97 | + </Form.Field> |
| 98 | + </Form.Root> |
| 99 | + </Sheet.Content> |
| 100 | +</Sheet.Root> |
0 commit comments