|
1 | 1 | <script lang="ts"> |
2 | | - import type { TaskData, UserData } from '$lib/backendSchemas'; |
| 2 | + import type { GroupData, TaskData, UserData } from '$lib/backendSchemas'; |
3 | 3 | import * as AlertDialog from '$components/ui/alert-dialog/index.js'; |
4 | 4 | import { buttonVariants } from '$components/ui/button/index.js'; |
5 | 5 | import * as m from '$lib/paraglide/messages.js'; |
6 | 6 | import { fileProxy, superForm, type Infer, type SuperValidated } from 'sveltekit-superforms'; |
7 | | - import { editTaskSchema, type EditTaskSchema } from './formSchemas'; |
| 7 | + import { |
| 8 | + assignTaskToGroupsSchema, |
| 9 | + editTaskSchema, |
| 10 | + type AssingTaskToGroupsSchema, |
| 11 | + type EditTaskSchema |
| 12 | + } from './formSchemas'; |
8 | 13 | import * as Form from '$components/ui/form'; |
9 | 14 | import { zodClient } from 'sveltekit-superforms/adapters'; |
10 | 15 | import Input from '../ui/input/input.svelte'; |
11 | 16 | import Separator from '../ui/separator/separator.svelte'; |
12 | 17 | import Label from '../ui/label/label.svelte'; |
| 18 | + import * as Select from '$lib/components/ui/select/index.js'; |
13 | 19 |
|
14 | 20 | let { |
15 | | - localUser, |
16 | 21 | taskData, |
17 | | - editTaskForm |
| 22 | + userGroups, |
| 23 | + editTaskForm, |
| 24 | + assingTaskToGroupsForm |
18 | 25 | }: { |
19 | | - localUser: UserData; |
20 | 26 | taskData: Omit<TaskData, 'description_url'>; |
| 27 | + userGroups: GroupData[]; |
21 | 28 | editTaskForm: SuperValidated<Infer<EditTaskSchema>>; |
| 29 | + assingTaskToGroupsForm: SuperValidated<Infer<AssingTaskToGroupsSchema>>; |
22 | 30 | } = $props(); |
23 | 31 |
|
24 | | - const form = superForm(editTaskForm, { |
| 32 | + const editForm = superForm(editTaskForm, { |
25 | 33 | validators: zodClient(editTaskSchema), |
26 | 34 | resetForm: false, |
27 | 35 | onResult: ({ result: { type } }) => { |
|
32 | 40 | } |
33 | 41 | }); |
34 | 42 |
|
35 | | - const { form: formData, message, enhance } = form; |
| 43 | + const assignToGroupsForm = superForm(assingTaskToGroupsForm, { |
| 44 | + validators: zodClient(assignTaskToGroupsSchema), |
| 45 | + resetForm: false, |
| 46 | + onResult: ({ result: { type } }) => { |
| 47 | + if (type === 'success') { |
| 48 | + open = false; |
| 49 | + location.reload(); |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | +
|
| 54 | + const { form: editTaskFormData, message: editTaskMessage, enhance: editTaskEnhance } = editForm; |
| 55 | + const { |
| 56 | + form: assignToGroupsFormData, |
| 57 | + message: assignToGroupsMessage, |
| 58 | + enhance: assignToGroupsEnhance |
| 59 | + } = assignToGroupsForm; |
| 60 | +
|
| 61 | + $editTaskFormData.id = taskData.id; |
| 62 | + $editTaskFormData.title = taskData.title; |
| 63 | + $editTaskFormData.archive = null; |
36 | 64 |
|
37 | | - $formData.id = taskData.id; |
38 | | - $formData.title = taskData.title; |
39 | | - $formData.archive = null; |
| 65 | + $assignToGroupsFormData.taskId = taskData.id; |
40 | 66 |
|
41 | | - const file = fileProxy(form, 'archive'); |
| 67 | + const file = fileProxy(editForm, 'archive'); |
42 | 68 |
|
43 | 69 | let open = $state(false); |
44 | 70 | </script> |
|
51 | 77 | <AlertDialog.Header> |
52 | 78 | <AlertDialog.Title>{m.edit_task_title()}</AlertDialog.Title> |
53 | 79 | <AlertDialog.Description> |
54 | | - <form enctype="multipart/form-data" action="?/editTask" method="POST" use:enhance> |
55 | | - <Form.Field {form} name="id" hidden> |
| 80 | + <form enctype="multipart/form-data" action="?/editTask" method="POST" use:editTaskEnhance> |
| 81 | + <Form.Field form={editForm} name="id" hidden> |
56 | 82 | <Form.Control> |
57 | 83 | {#snippet children({ props })} |
58 | | - <Input type="number" {...props} bind:value={$formData.id} /> |
| 84 | + <Input type="number" {...props} bind:value={$editTaskFormData.id} /> |
59 | 85 | {/snippet} |
60 | 86 | </Form.Control> |
61 | 87 | <Form.FieldErrors /> |
62 | 88 | </Form.Field> |
63 | | - <Form.Field {form} name="title"> |
| 89 | + <Form.Field form={editForm} name="title"> |
64 | 90 | <Form.Control> |
65 | 91 | {#snippet children({ props })} |
66 | 92 | <Form.Label>{m.edit_task_title_label()}</Form.Label> |
67 | | - <Input {...props} bind:value={$formData.title} /> |
| 93 | + <Input {...props} bind:value={$editTaskFormData.title} /> |
68 | 94 | {/snippet} |
69 | 95 | </Form.Control> |
70 | 96 | <Form.FieldErrors /> |
71 | 97 | </Form.Field> |
72 | | - <Form.Field {form} name="archive"> |
| 98 | + <Form.Field form={editForm} name="archive"> |
73 | 99 | <Form.Control> |
74 | 100 | {#snippet children({ props })} |
75 | 101 | <Label for="archive">{m.task_form_task_file_label()}</Label> |
|
85 | 111 | <Form.Description>{m.task_form_task_file_description()}</Form.Description> |
86 | 112 | <Form.FieldErrors /> |
87 | 113 | </Form.Field> |
88 | | - {#if $message} |
89 | | - <p class="text-destructive my-2 text-sm font-medium">{$message}</p> |
| 114 | + {#if $editTaskMessage} |
| 115 | + <p class="text-destructive my-2 text-sm font-medium">{$editTaskMessage}</p> |
90 | 116 | {/if} |
91 | 117 | <Separator class="my-4" /> |
92 | 118 | <div class="flex-row w-full"> |
|
96 | 122 | <Form.Button type="submit">{m.edit_profile_submit()}</Form.Button> |
97 | 123 | </div> |
98 | 124 | </form> |
| 125 | + <Separator class="my-4" /> |
| 126 | + <h2 class="font-semibold text-lg text-foreground"> |
| 127 | + {m.task_assign_groups_form_title()} |
| 128 | + </h2> |
| 129 | + <form action="?/assignGroups" method="POST" use:assignToGroupsEnhance> |
| 130 | + <Form.Field form={assignToGroupsForm} name="taskId" hidden> |
| 131 | + <Form.Control> |
| 132 | + {#snippet children({ props })} |
| 133 | + <Input type="number" {...props} bind:value={$assignToGroupsFormData.taskId} /> |
| 134 | + {/snippet} |
| 135 | + </Form.Control> |
| 136 | + <Form.FieldErrors /> |
| 137 | + </Form.Field> |
| 138 | + <Form.Field form={assignToGroupsForm} name="groupIds" class="my-4"> |
| 139 | + <Form.Control> |
| 140 | + {#snippet children({ props })} |
| 141 | + <Select.Root |
| 142 | + type="multiple" |
| 143 | + bind:value={$assignToGroupsFormData.groupIds} |
| 144 | + {...props} |
| 145 | + > |
| 146 | + <Select.Trigger class="w-[180px]"> |
| 147 | + {m.task_assign_group_select_title()} |
| 148 | + </Select.Trigger> |
| 149 | + <Select.Content> |
| 150 | + <Select.Group> |
| 151 | + {#each userGroups as userGroup} |
| 152 | + <Select.Item value={userGroup.id.toString()} label={userGroup.name} |
| 153 | + >{userGroup.name}</Select.Item |
| 154 | + > |
| 155 | + {/each} |
| 156 | + </Select.Group> |
| 157 | + </Select.Content> |
| 158 | + </Select.Root> |
| 159 | + {/snippet} |
| 160 | + </Form.Control> |
| 161 | + <Form.FieldErrors /> |
| 162 | + </Form.Field> |
| 163 | + {#if $assignToGroupsMessage} |
| 164 | + <p class="text-destructive my-2 text-sm font-medium">{$assignToGroupsMessage}</p> |
| 165 | + {/if} |
| 166 | + <Form.Button type="submit">{m.task_assign_groups_submit()}</Form.Button> |
| 167 | + </form> |
99 | 168 | </AlertDialog.Description> |
100 | 169 | </AlertDialog.Header> |
101 | 170 | <AlertDialog.Footer> |
|
0 commit comments