|
| 1 | +<template> |
| 2 | + <UForm |
| 3 | + :validate="createValidator(createTaskSchema)" |
| 4 | + :state="state" |
| 5 | + class="flex flex-col gap-3" |
| 6 | + @submit="onSubmit" |
| 7 | + > |
| 8 | + <UFormField |
| 9 | + :label="$t('common.title')" |
| 10 | + name="name" |
| 11 | + required |
| 12 | + > |
| 13 | + <UInput |
| 14 | + v-model="state.name" |
| 15 | + :placeholder="$t('app.task.name-placeholder')" |
| 16 | + size="lg" |
| 17 | + class="w-full items-center justify-center" |
| 18 | + /> |
| 19 | + </UFormField> |
| 20 | + |
| 21 | + <UFormField :label="$t('app.task.description')" name="description"> |
| 22 | + <UTextarea |
| 23 | + v-model="state.description" |
| 24 | + :rows="4" |
| 25 | + size="lg" |
| 26 | + class="w-full" |
| 27 | + /> |
| 28 | + </UFormField> |
| 29 | + |
| 30 | + <UFormField label="Исполнитель" name="performerId"> |
| 31 | + <USelectMenu |
| 32 | + v-model="selectedPerformer" |
| 33 | + :items="availablePerformersItems" |
| 34 | + :avatar="selectedPerformer?.avatar" |
| 35 | + :placeholder="$t('common.select')" |
| 36 | + :content="{ |
| 37 | + side: 'top', |
| 38 | + }" |
| 39 | + size="lg" |
| 40 | + class="w-full" |
| 41 | + /> |
| 42 | + </UFormField> |
| 43 | + |
| 44 | + <div class="grid grid-cols-1 md:grid-cols-2"> |
| 45 | + <UPopover> |
| 46 | + <UFormField :label="$t('common.date')" name="date"> |
| 47 | + <UInput |
| 48 | + :value="selectedDate ? df.format(selectedDate.toDate(getLocalTimeZone())) : ''" |
| 49 | + placeholder="Выберите дату" |
| 50 | + size="lg" |
| 51 | + class="w-full items-center justify-center cursor-pointer" |
| 52 | + :ui="{ trailing: 'pe-1.5' }" |
| 53 | + > |
| 54 | + <template v-if="selectedDate" #trailing> |
| 55 | + <UButton |
| 56 | + color="neutral" |
| 57 | + variant="ghost" |
| 58 | + size="md" |
| 59 | + icon="i-lucide-x" |
| 60 | + @click="selectedDate = undefined" |
| 61 | + /> |
| 62 | + </template> |
| 63 | + </UInput> |
| 64 | + </UFormField> |
| 65 | + |
| 66 | + <template #content> |
| 67 | + <UCalendar v-model="selectedDate" class="p-2" /> |
| 68 | + </template> |
| 69 | + </UPopover> |
| 70 | + </div> |
| 71 | + |
| 72 | + <UButton |
| 73 | + type="submit" |
| 74 | + variant="solid" |
| 75 | + color="secondary" |
| 76 | + size="lg" |
| 77 | + block |
| 78 | + class="mt-3" |
| 79 | + :label="$t('common.create')" |
| 80 | + /> |
| 81 | + </UForm> |
| 82 | +</template> |
| 83 | + |
| 84 | +<script setup lang="ts"> |
| 85 | +import type { CreateTask } from '#shared/services/task' |
| 86 | +import type { CalendarDate } from '@internationalized/date' |
| 87 | +import type { FormSubmitEvent } from '@nuxt/ui' |
| 88 | +import { createTaskSchema } from '#shared/services/task' |
| 89 | +import { DateFormatter, getLocalTimeZone } from '@internationalized/date' |
| 90 | +
|
| 91 | +const { performerId, listId } = defineProps<{ |
| 92 | + performerId?: string |
| 93 | + listId: string |
| 94 | +}>() |
| 95 | +
|
| 96 | +const emit = defineEmits(['success', 'submitted']) |
| 97 | +
|
| 98 | +const { t } = useI18n() |
| 99 | +const actionToast = useActionToast() |
| 100 | +
|
| 101 | +const taskStore = useTaskStore() |
| 102 | +const userStore = useUserStore() |
| 103 | +
|
| 104 | +const list = computed(() => taskStore.lists.find((list) => list.id === listId)) |
| 105 | +
|
| 106 | +const availablePerformers = computed(() => userStore.staff.filter((s) => list.value?.chat?.members.some((m) => m.user.id === s.id))) |
| 107 | +const availablePerformersItems = computed(() => [{ |
| 108 | + label: 'Без исполнителя', |
| 109 | + value: '', |
| 110 | + avatar: { |
| 111 | + src: undefined, |
| 112 | + alt: '', |
| 113 | + }, |
| 114 | +}, ...availablePerformers.value.map((staff) => ({ |
| 115 | + label: `${staff.name} ${staff.surname}`, |
| 116 | + value: staff.id, |
| 117 | + avatar: { |
| 118 | + src: staff.avatarUrl ?? undefined, |
| 119 | + alt: '', |
| 120 | + }, |
| 121 | +}))]) |
| 122 | +
|
| 123 | +const state = ref<Partial<CreateTask>>({ |
| 124 | + name: undefined, |
| 125 | + description: undefined, |
| 126 | + date: undefined, |
| 127 | + performerId, |
| 128 | + listId, |
| 129 | +}) |
| 130 | +
|
| 131 | +const selectedPerformer = ref<{ label: string, value: string, avatar: { src: string | undefined, alt: string } } | undefined>(state.value.performerId ? availablePerformersItems.value.find((performer) => performer?.value === state.value.performerId) : undefined) |
| 132 | +
|
| 133 | +watch(selectedPerformer, () => { |
| 134 | + if (selectedPerformer.value?.value === '') { |
| 135 | + state.value.performerId = null |
| 136 | + return |
| 137 | + } |
| 138 | +
|
| 139 | + state.value.performerId = selectedPerformer.value?.value |
| 140 | +}) |
| 141 | +
|
| 142 | +const df = new DateFormatter('ru-RU', { |
| 143 | + dateStyle: 'long', |
| 144 | +}) |
| 145 | +
|
| 146 | +const selectedDate = shallowRef<CalendarDate | undefined>() |
| 147 | +
|
| 148 | +watch(selectedDate, () => { |
| 149 | + if (!selectedDate.value) { |
| 150 | + state.value.date = null |
| 151 | + return |
| 152 | + } |
| 153 | +
|
| 154 | + state.value.date = selectedDate.value.toString() |
| 155 | +}) |
| 156 | +
|
| 157 | +async function onSubmit(event: FormSubmitEvent<CreateTask>) { |
| 158 | + const toastId = actionToast.start() |
| 159 | + emit('submitted') |
| 160 | +
|
| 161 | + try { |
| 162 | + await $fetch(`/api/task`, { |
| 163 | + method: 'POST', |
| 164 | + headers: { |
| 165 | + Authorization: `tma ${userStore.initDataRaw}`, |
| 166 | + }, |
| 167 | + body: event.data, |
| 168 | + }) |
| 169 | +
|
| 170 | + await taskStore.update() |
| 171 | +
|
| 172 | + actionToast.success(toastId, t('toast.task-created')) |
| 173 | + emit('success') |
| 174 | + } catch (error) { |
| 175 | + console.error(error) |
| 176 | + actionToast.error(toastId) |
| 177 | + } |
| 178 | +} |
| 179 | +</script> |
0 commit comments