All files / apps/huella-legal/layers/newsletter/app/components NewsletterForm.vue

100% Statements 43/43
100% Branches 17/17
100% Functions 15/15
100% Lines 40/40

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  25x 1x   30x                   4x 6x           12x                         73x 64x 1x                                   64x 64x                                                         22x       22x         132x   22x 22x 22x   22x 14x 22x   22x   22x 22x   22x             22x   22x 6x 6x 6x   6x 3x 3x     3x 3x 6x 6x   6x 6x 3x     22x    
<template>
    <FormSuccessPanel v-if="status === 'success'" :title="$t('newsletterForm.success.title')">
        {{ $t("newsletterForm.success.description") }}
    </FormSuccessPanel>
    <FormChallenge v-else v-slot="{ challenge }">
        <UForm
            ref="form"
            :schema
            :state
            :validate-on
            :disabled="submitting"
            class="flex flex-col gap-3"
            novalidate
            :aria-busy="submitting"
            @submit="submit($event.data, challenge)"
            @error="onInvalid($event.errors)"
        >
            <UFormField name="email" :label="$t('newsletterForm.email.label')" eager-validation>
                <div :class="arrangement.row">
                    <UInput
                        ref="email"
                        v-model="state.email"
                        type="email"
                        autocomplete="email"
                        :placeholder="$t('newsletterForm.email.placeholder')"
                        class="w-full"
                    />
                    <UButton
                        type="submit"
                        :block="arrangement.block"
                        :loading="submitting"
                        :label="$t('newsletterForm.submit')"
                        class="shrink-0 justify-center"
                    />
                </div>
            </UFormField>
            <FormHoneypot v-model="honeypot" />
            <UAlert
                v-if="status === 'error'"
                role="alert"
                color="error"
                variant="subtle"
                icon="i-lucide-circle-alert"
                :title="$t('newsletterForm.error.title')"
                :description="$t('newsletterForm.error.description')"
            />
            <I18nT
                keypath="newsletterForm.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("newsletterForm.privacy") }}
                    </ULink>
                </template>
            </I18nT>
            <p class="sr-only" aria-live="polite">{{ announcement }}</p>
        </UForm>
    </FormChallenge>
</template>
 
<script setup lang="ts">
import { useFormSubmission } from "#layers/forms/app/composables/useFormSubmission";
import type { Form, FormErrorWithId } from "@nuxt/ui";
import {
    type Subscription,
    type SubscriptionIssue,
    subscriptionSchema,
} from "../../shared/schemas/subscription";
import { useNewsletter } from "../composables/useNewsletter";
 
type Layout = "stacked" | "inline";
 
interface LayoutRecipe {
    row: string;
    block: boolean;
}
 
interface Props {
    layout?: Layout;
}
 
const props = withDefaults(defineProps<Props>(), {
    layout: "stacked",
});
 
const LAYOUTS = {
    stacked: { row: "flex flex-col gap-3", block: true },
    inline: { row: "flex flex-col gap-3 sm:flex-row", block: false },
} as const satisfies Record<Layout, LayoutRecipe>;
 
const SHAKE: Keyframe[] = [0, -6, 6, -4, 4, 0].map((x) => ({ transform: `translateX(${x}px)` }));
 
const { t } = useI18n();
const { nav } = useAppConfig();
const { status, errors, send } = useNewsletter();
 
const issueMessage = (issue: SubscriptionIssue): string =>
    t(`newsletterForm.email.errors.${issue}`);
const schema = subscriptionSchema(issueMessage);
 
const state = reactive<Subscription>({ email: "" });
 
const form = useTemplateRef<Form<typeof schema>>("form");
const email = useTemplateRef<{ inputRef?: HTMLInputElement | null }>("email");
 
const { honeypot, submitted, submitting, validateOn, submit, onError } = useFormSubmission(form, {
    status,
    errors,
    send,
    message: issueMessage,
});
 
const announcement = ref<string>("");
 
const onInvalid = async (formErrors: readonly FormErrorWithId[]): Promise<void> => {
    const required = issueMessage("required");
    const empty = formErrors.length > 0 && formErrors.every(({ message }) => message === required);
    const input = empty ? email.value?.inputRef : null;
 
    if (!input || matchMedia("(prefers-reduced-motion: reduce)").matches) {
        onError(formErrors);
        return;
    }
 
    submitted.value = false;
    form.value?.clear();
    input.focus();
    input.animate(SHAKE, { duration: 320, easing: "ease-in-out" });
 
    announcement.value = "";
    await nextTick();
    announcement.value = required;
};
 
const arrangement = computed<LayoutRecipe>(() => LAYOUTS[props.layout]);
</script>