All files / apps/huella-legal/layers/publish/app/components PublishForm.vue

92.5% Statements 37/40
100% Branches 21/21
88.88% Functions 24/27
97.05% Lines 33/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153  12x 1x   15x                   3x 2x   31x                   2x   37x       6x         37x     31x     31x                 36x               2x           33x         5x     36x                   31x 31x                                                   10x 10x 10x   10x 10x   10x   10x 27x     10x                   10x   10x             6x         10x 3x    
<template>
    <FormSuccessPanel v-if="status === 'success'" :title="$t('publishForm.success.title')">
        {{ $t("publishForm.success.description") }}
    </FormSuccessPanel>
    <FormChallenge v-else v-slot="{ challenge }">
        <UForm
            ref="form"
            :schema
            :state
            :validate-on
            :disabled="submitting"
            :aria-busy="submitting"
            novalidate
            class="flex flex-col gap-6"
            @submit="onSubmit($event.data, challenge)"
            @error="onError($event.errors)"
        >
            <FormErrorSummary :errors="summaryErrors" />
            <UAlert
                v-if="status === 'error'"
                role="alert"
                color="error"
                variant="subtle"
                icon="i-lucide-circle-alert"
                :title="$t('publishForm.error.title')"
                :description="$t('publishForm.error.description')"
            />
            <div class="grid grid-cols-1 gap-6 md:grid-cols-2">
                <UFormField name="name" :label="$t('publishForm.name.label')" required>
                    <UInput v-model="state.name" autocomplete="name" class="w-full" />
                </UFormField>
                <UFormField name="email" :label="$t('publishForm.email.label')" required>
                    <UInput
                        v-model="state.email"
                        type="email"
                        autocomplete="email"
                        :placeholder="$t('publishForm.email.placeholder')"
                        class="w-full"
                    />
                </UFormField>
                <UFormField name="kind" :label="$t('publishForm.kind.label')" required>
                    <USelect v-model="state.kind" :items="kinds" class="w-full" />
                </UFormField>
                <UFormField name="title" :label="$t('publishForm.title.label')">
                    <UInput v-model="state.title" class="w-full" />
                </UFormField>
            </div>
            <UFormField
                name="summary"
                :label="$t('publishForm.summary.label')"
                :help="$t('publishForm.summary.help')"
                required
            >
                <UTextarea v-model="state.summary" :rows="4" autoresize class="w-full" />
            </UFormField>
            <UFormField
                name="file"
                :label="$t('publishForm.file.label')"
                :help="$t('publishForm.file.help')"
            >
                <UFileUpload
                    v-model="state.file"
                    :label="$t('publishForm.file.upload')"
                    :description="$t('publishForm.file.description')"
                    icon="i-lucide-file-text"
                    :accept="ACCEPT"
                    class="w-full"
                />
            </UFormField>
            <FormHoneypot v-model="honeypot" />
            <UFormField name="consent">
                <UCheckbox
                    v-model="state.consent"
                    :label="$t('publishForm.consent.label')"
                    required
                />
            </UFormField>
            <I18nT
                keypath="publishForm.notice"
                scope="global"
                tag="p"
                class="font-sans text-meta text-muted"
            >
                <template #privacy>
                    <ULink raw :to="nav.privacy" class="underline underline-offset-2">
                        {{ $t("publishForm.privacy") }}
                    </ULink>
                </template>
            </I18nT>
            <UButton
                type="submit"
                :loading="submitting"
                :label="$t('publishForm.submit')"
                class="justify-center md:self-start"
            />
        </UForm>
    </FormChallenge>
</template>
 
<script setup lang="ts">
import type { Challenge } from "#layers/forms/app/types/Challenge";
import { useFormSubmission } from "#layers/forms/app/composables/useFormSubmission";
import type { Form, SelectItem } from "@nuxt/ui";
import {
    MANUSCRIPT_TYPES,
    SUBMISSION_KINDS,
    type Submission,
    type SubmissionIssue,
    submissionSchema,
} from "../../shared/schemas/submission";
import { useSubmission } from "../composables/useSubmission";
 
const { t } = useI18n();
const { nav } = useAppConfig();
const { status, errors, send } = useSubmission();
 
const issueMessage = (issue: SubmissionIssue): string => t(`publishForm.errors.${issue}`);
const schema = submissionSchema(issueMessage);
 
const ACCEPT = MANUSCRIPT_TYPES.join(",");
 
const kinds = computed<SelectItem[]>(() =>
    SUBMISSION_KINDS.map((kind) => ({ value: kind, label: t(`publishForm.kind.items.${kind}`) })),
);
 
const state = reactive<Omit<Submission, "file"> & { file: File | null }>({
    name: "",
    email: "",
    kind: "article",
    title: "",
    summary: "",
    file: null,
    consent: false,
});
 
const form = useTemplateRef<Form<typeof schema>>("form");
 
const { honeypot, submitting, validateOn, summaryErrors, submit, onError } = useFormSubmission(
    form,
    {
        status,
        errors,
        send,
        message: issueMessage,
        label: (field) => t(`publishForm.${field}.name`),
    },
);
 
// The schema keeps only the file's name and size, and `state.file` is a proxy FormData refuses
const onSubmit = (data: Submission, challenge: Challenge) =>
    submit({ ...data, file: toRaw(state.file) }, challenge);
</script>