All files / apps/huella-legal/layers/contact/app/components ContactForm.vue

100% Statements 28/28
100% Branches 15/15
100% Functions 18/18
100% Lines 25/25

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    11x       1x   14x                   3x 2x   31x                   1x   37x       6x         37x       36x   1x     5x     36x                   31x 31x                                     9x 9x 9x   9x 9x   9x   9x   9x             5x        
<template>
    <FormSuccessPanel
        v-if="status === 'success'"
        :title="$t('contactForm.success.title')"
        :level="2"
    >
        {{ $t("contactForm.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="submit($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('contactForm.error.title')"
                :description="$t('contactForm.error.description')"
            />
            <div class="grid grid-cols-1 gap-6 md:grid-cols-2">
                <UFormField name="name" :label="$t('contactForm.name.label')" required>
                    <UInput v-model="state.name" autocomplete="name" class="w-full" />
                </UFormField>
                <UFormField name="email" :label="$t('contactForm.email.label')" required>
                    <UInput
                        v-model="state.email"
                        type="email"
                        autocomplete="email"
                        :placeholder="$t('contactForm.email.placeholder')"
                        class="w-full"
                    />
                </UFormField>
            </div>
            <UFormField name="message" :label="$t('contactForm.message.label')" required>
                <UTextarea v-model="state.message" :rows="6" autoresize class="w-full" />
            </UFormField>
            <FormHoneypot v-model="honeypot" />
            <UFormField name="consent">
                <UCheckbox
                    v-model="state.consent"
                    :label="$t('contactForm.consent.label')"
                    required
                />
            </UFormField>
            <I18nT
                keypath="contactForm.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("contactForm.privacy") }}
                    </ULink>
                </template>
            </I18nT>
            <UButton
                type="submit"
                :loading="submitting"
                :label="$t('contactForm.submit')"
                class="justify-center md:self-start"
            />
        </UForm>
    </FormChallenge>
</template>
 
<script setup lang="ts">
import { useFormSubmission } from "#layers/forms/app/composables/useFormSubmission";
import type { Form } from "@nuxt/ui";
import { type Contact, type ContactIssue, contactSchema } from "../../shared/schemas/contact";
import { useContact } from "../composables/useContact";
 
const { t } = useI18n();
const { nav } = useAppConfig();
const { status, errors, send } = useContact();
 
const issueMessage = (issue: ContactIssue): string => t(`contactForm.errors.${issue}`);
const schema = contactSchema(issueMessage);
 
const state = reactive<Contact>({ name: "", email: "", message: "", 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(`contactForm.${field}.name`),
    },
);
</script>