|
3 | 3 | import Button from "$lib/components/ui/button/button.svelte" |
4 | 4 | import * as Dialog from "$lib/components/ui/dialog" |
5 | 5 | import { closeModal, DUPLICATE_BASE_MODAL, isModalOpen } from "$lib/store/modal.store" |
| 6 | + import * as Form from "$lib/components/ui/form" |
| 7 | + import { Input } from "$lib/components/ui/input" |
6 | 8 | import { trpc } from "$lib/trpc/client" |
7 | 9 | import { createMutation } from "@tanstack/svelte-query" |
8 | 10 | import type { Base, IBaseDTO } from "@undb/base" |
| 11 | + import { duplicateBaseCommand } from "@undb/commands" |
9 | 12 | import { LoaderCircleIcon } from "lucide-svelte" |
10 | 13 | import { toast } from "svelte-sonner" |
| 14 | + import { defaults, superForm } from "sveltekit-superforms" |
| 15 | + import { zodClient } from "sveltekit-superforms/adapters" |
| 16 | + import { Checkbox } from "$lib/components/ui/checkbox" |
11 | 17 |
|
12 | 18 | export let base: Omit<IBaseDTO, "spaceId"> |
13 | 19 |
|
| 20 | + const form = superForm( |
| 21 | + defaults( |
| 22 | + { |
| 23 | + id: base.id, |
| 24 | + name: "", |
| 25 | + includeData: true, |
| 26 | + }, |
| 27 | + zodClient(duplicateBaseCommand), |
| 28 | + ), |
| 29 | + { |
| 30 | + SPA: true, |
| 31 | + dataType: "json", |
| 32 | + validators: zodClient(duplicateBaseCommand), |
| 33 | + resetForm: false, |
| 34 | + invalidateAll: false, |
| 35 | + onUpdate(event) { |
| 36 | + if (!event.form.valid) { |
| 37 | + return |
| 38 | + } |
| 39 | +
|
| 40 | + $duplicateBaseMutation.mutate(event.form.data) |
| 41 | + }, |
| 42 | + }, |
| 43 | + ) |
| 44 | +
|
14 | 45 | const duplicateBaseMutation = createMutation({ |
15 | 46 | mutationFn: trpc.base.duplicate.mutate, |
16 | 47 | onSuccess: async (data) => { |
|
22 | 53 | toast.error(error.message) |
23 | 54 | }, |
24 | 55 | }) |
| 56 | +
|
| 57 | + const { form: formData, enhance } = form |
25 | 58 | </script> |
26 | 59 |
|
27 | 60 | <Dialog.Root |
|
38 | 71 | <Dialog.Description>Create a new base include all tables in base.</Dialog.Description> |
39 | 72 | </Dialog.Header> |
40 | 73 |
|
41 | | - <div class="item-center flex justify-end gap-2"> |
42 | | - <Button |
43 | | - on:click={() => { |
44 | | - closeModal(DUPLICATE_BASE_MODAL) |
45 | | - }} |
46 | | - variant="outline" |
47 | | - > |
48 | | - Cancel |
49 | | - </Button> |
50 | | - <Button |
51 | | - disabled={$duplicateBaseMutation.isPending} |
52 | | - on:click={() => { |
53 | | - $duplicateBaseMutation.mutate({ id: base.id }) |
54 | | - }} |
55 | | - > |
56 | | - {#if $duplicateBaseMutation.isPending} |
57 | | - <LoaderCircleIcon class="mr-2 h-5 w-5 animate-spin" /> |
58 | | - {/if} |
59 | | - Duplicate |
60 | | - </Button> |
61 | | - </div> |
| 74 | + <form method="POST" use:enhance> |
| 75 | + <Form.Field {form} name="name"> |
| 76 | + <Form.Control let:attrs> |
| 77 | + <Form.Label>Name</Form.Label> |
| 78 | + <Input {...attrs} bind:value={$formData.name} /> |
| 79 | + </Form.Control> |
| 80 | + <Form.Description>This is base display name.</Form.Description> |
| 81 | + <Form.FieldErrors /> |
| 82 | + </Form.Field> |
| 83 | + <Form.Field {form} name="includeData" class="flex flex-row items-start space-x-3 space-y-0 rounded-md border p-4"> |
| 84 | + <Form.Control let:attrs> |
| 85 | + <Checkbox {...attrs} bind:checked={$formData.includeData} /> |
| 86 | + <div class="space-y-1 leading-none"> |
| 87 | + <Form.Label>Include data</Form.Label> |
| 88 | + <Form.Description>Include data in the new base.</Form.Description> |
| 89 | + </div> |
| 90 | + <input name={attrs.name} value={$formData.includeData} hidden /> |
| 91 | + </Form.Control> |
| 92 | + </Form.Field> |
| 93 | + |
| 94 | + <div class="mt-4 flex items-center justify-end gap-2"> |
| 95 | + <Button |
| 96 | + on:click={() => { |
| 97 | + closeModal(DUPLICATE_BASE_MODAL) |
| 98 | + }} |
| 99 | + variant="outline" |
| 100 | + > |
| 101 | + Cancel |
| 102 | + </Button> |
| 103 | + <Form.Button |
| 104 | + disabled={$duplicateBaseMutation.isPending} |
| 105 | + on:click={() => { |
| 106 | + $duplicateBaseMutation.mutate({ id: base.id }) |
| 107 | + }} |
| 108 | + > |
| 109 | + {#if $duplicateBaseMutation.isPending} |
| 110 | + <LoaderCircleIcon class="mr-2 h-5 w-5 animate-spin" /> |
| 111 | + {/if} |
| 112 | + Duplicate |
| 113 | + </Form.Button> |
| 114 | + </div> |
| 115 | + </form> |
62 | 116 | </Dialog.Content> |
63 | 117 | </Dialog.Root> |
0 commit comments