All files / apps/huella-legal/layers/comments/app/components CommentForm.vue

97.61% Statements 41/42
90.9% Branches 20/22
95.83% Functions 23/24
97.36% Lines 37/38

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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171    13x       1x   15x                   2x 2x   28x                                         4x         32x   4x             32x                 4x         32x           3x     31x                   28x 28x                                                           12x   12x   12x 12x 12x   12x 12x   12x   12x 3x   12x         1x       12x 12x   12x             2x       12x 2x   12x 13x   1x          
<template>
    <FormSuccessPanel
        v-if="status === 'success'"
        :title="$t('commentForm.success.title')"
        :level="4"
    >
        {{ $t("commentForm.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('commentForm.error.title')"
                :description="$t(`commentForm.error.${failure}`)"
            />
            <UAlert
                v-if="replyTo"
                color="neutral"
                variant="soft"
                orientation="horizontal"
                :title="replyingTo"
                :actions="replyActions"
            />
            <UFormField name="content" :label="$t('commentForm.content.label')" required>
                <UTextarea
                    ref="content"
                    v-model="state.content"
                    :rows="5"
                    :maxlength="MAX_COMMENT_LENGTH"
                    autoresize
                    class="w-full"
                />
            </UFormField>
            <div class="grid grid-cols-1 gap-6 md:grid-cols-2">
                <UFormField
                    name="name"
                    :label="$t('commentForm.name.label')"
                    :hint="$t('commentForm.name.hint')"
                    required
                >
                    <UInput v-model="state.name" autocomplete="name" class="w-full" />
                </UFormField>
                <UFormField
                    name="email"
                    :label="$t('commentForm.email.label')"
                    :hint="$t('commentForm.email.hint')"
                    required
                >
                    <UInput
                        v-model="state.email"
                        type="email"
                        autocomplete="email"
                        :placeholder="$t('commentForm.email.placeholder')"
                        class="w-full"
                    />
                </UFormField>
            </div>
            <FormHoneypot v-model="honeypot" />
            <UFormField name="consent">
                <UCheckbox
                    v-model="state.consent"
                    :label="$t('commentForm.consent.label')"
                    required
                />
            </UFormField>
            <I18nT
                keypath="commentForm.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("commentForm.privacy") }}
                    </ULink>
                </template>
            </I18nT>
            <UButton
                type="submit"
                :loading="submitting"
                :label="$t('commentForm.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 { ButtonProps, Form } from "@nuxt/ui";
import {
    type CommentFields,
    type CommentIssue,
    commentSchema,
    MAX_COMMENT_LENGTH,
} from "../../shared/schemas/comment";
import { usePostComment } from "../composables/usePostComment";
 
interface Props {
    postId: string;
    replyTo?: ReaderComment;
}
 
const props = defineProps<Props>();
 
const emit = defineEmits<{ cancelReply: [] }>();
 
const { t } = useI18n();
const { nav } = useAppConfig();
const { status, errors, failure, send } = usePostComment();
 
const issueMessage = (issue: CommentIssue): string => t(`commentForm.errors.${issue}`);
const schema = commentSchema(issueMessage);
 
const state = reactive<CommentFields>({ name: "", email: "", content: "", consent: false });
 
const replyingTo = computed<string>(() =>
    t("commentForm.replyingTo", { author: props.replyTo?.author || t("commentItem.anonymous") }),
);
const replyActions = computed<ButtonProps[]>(() => [
    {
        label: t("commentForm.cancelReply"),
        variant: "link",
        color: "neutral",
        onClick: () => emit("cancelReply"),
    },
]);
 
const form = useTemplateRef<Form<typeof schema>>("form");
const content = useTemplateRef<{ textareaRef?: HTMLTextAreaElement | null }>("content");
 
const { honeypot, submitting, validateOn, summaryErrors, submit, onError } = useFormSubmission(
    form,
    {
        status,
        errors,
        send,
        message: issueMessage,
        label: (field) => t(`commentForm.${field}.name`),
    },
);
 
const onSubmit = (fields: CommentFields, challenge: Challenge): Promise<void> =>
    submit({ ...fields, post: props.postId, parent: props.replyTo?.id ?? "0" }, challenge);
 
watch(
    () => props.replyTo,
    (comment) => {
        Eif (comment) content.value?.textareaRef?.focus();
    },
    { flush: "post" },
);
</script>