|
| 1 | +<template> |
| 2 | + <UForm |
| 3 | + :validate="createValidator(createFlowItemSchema)" |
| 4 | + :state="state" |
| 5 | + class="flex flex-col gap-3" |
| 6 | + @submit="onSubmit" |
| 7 | + > |
| 8 | + <UFormField |
| 9 | + :label="$t('common.title')" |
| 10 | + name="title" |
| 11 | + required |
| 12 | + > |
| 13 | + <UInput |
| 14 | + v-model="state.title" |
| 15 | + size="xl" |
| 16 | + class="w-full" |
| 17 | + /> |
| 18 | + </UFormField> |
| 19 | + |
| 20 | + <UFormField label="Текст" name="description"> |
| 21 | + <UTextarea |
| 22 | + v-model="state.description" |
| 23 | + placeholder="Основной текст поста" |
| 24 | + autoresize |
| 25 | + size="xl" |
| 26 | + class="w-full" |
| 27 | + /> |
| 28 | + </UFormField> |
| 29 | + |
| 30 | + <UButton |
| 31 | + type="submit" |
| 32 | + variant="solid" |
| 33 | + color="secondary" |
| 34 | + size="xl" |
| 35 | + block |
| 36 | + class="mt-3" |
| 37 | + :label="$t('common.create')" |
| 38 | + /> |
| 39 | + </UForm> |
| 40 | +</template> |
| 41 | + |
| 42 | +<script setup lang="ts"> |
| 43 | +import type { CreateFlowItem } from '#shared/services/flow' |
| 44 | +import type { FormSubmitEvent } from '@nuxt/ui' |
| 45 | +import { createFlowItemSchema } from '#shared/services/flow' |
| 46 | +
|
| 47 | +const emit = defineEmits(['success', 'submitted']) |
| 48 | +
|
| 49 | +const { t } = useI18n() |
| 50 | +const { vibrate } = useFeedback() |
| 51 | +const actionToast = useActionToast() |
| 52 | +
|
| 53 | +const flowStore = useFlowStore() |
| 54 | +const userStore = useUserStore() |
| 55 | +
|
| 56 | +const state = ref<Partial<CreateFlowItem>>({ |
| 57 | + title: undefined, |
| 58 | + description: undefined, |
| 59 | + type: 'user_post', |
| 60 | + userId: userStore.id, |
| 61 | +}) |
| 62 | +
|
| 63 | +async function onSubmit(event: FormSubmitEvent<CreateFlowItem>) { |
| 64 | + const toastId = actionToast.start() |
| 65 | + emit('submitted') |
| 66 | +
|
| 67 | + try { |
| 68 | + await $fetch('/api/flow', { |
| 69 | + method: 'POST', |
| 70 | + headers: { |
| 71 | + Authorization: `tma ${userStore.initDataRaw}`, |
| 72 | + }, |
| 73 | + body: event.data, |
| 74 | + }) |
| 75 | +
|
| 76 | + await Promise.all([ |
| 77 | + flowStore.update(), |
| 78 | + userStore.update(), |
| 79 | + ]) |
| 80 | +
|
| 81 | + actionToast.success(toastId, t('toast.flow-item-created')) |
| 82 | + vibrate('success') |
| 83 | + emit('success') |
| 84 | + } catch (error) { |
| 85 | + console.error(error) |
| 86 | + actionToast.error(toastId) |
| 87 | + vibrate('error') |
| 88 | + } |
| 89 | +} |
| 90 | +</script> |
0 commit comments