All files / apps/huella-legal/layers/interest/app/components InterestCalculatorAdvancedForm.vue

100% Statements 47/47
94.44% Branches 17/18
100% Functions 22/22
100% Lines 41/41

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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198  18x                   80x   37x                   1x       2x     2x 160x 2x     2x   480x                 69x             545x                         2x             11x       29x             29x                                                                         18x 18x           36x     108x     18x   18x   18x   18x                           18x 6x     18x       18x   18x 6x 6x     18x 6x 6x 6x                 18x     89x 89x         18x 49x   31x        
<template>
    <UForm
        ref="form"
        :schema
        :state
        :validate-on="[]"
        novalidate
        class="flex flex-col gap-6"
        @submit="onSubmit"
        @error="onError"
    >
        <FormErrorSummary :errors />
        <UFormField name="rate" :label="$t('interestCalculator.rate.label')">
            <URadioGroup v-model="state.rate" :items="rates" />
        </UFormField>
 
        <UFormField
            v-if="state.rate === 'custom'"
            name="customPercent"
            :label="$t('interestCalculator.customPercent.label')"
            :hint="$t('interestCalculator.customPercent.hint')"
        >
            <UInput
                v-model="state.customPercent"
                inputmode="decimal"
                autocomplete="off"
                class="w-full sm:w-48"
            />
        </UFormField>
 
        <div class="grid grid-cols-1 gap-6 sm:grid-cols-2">
            <fieldset v-for="{ date, errorPattern, parts } in DATES" :key="date" class="min-w-0">
                <legend class="mb-2 font-sans text-sm font-medium text-highlighted">
                    {{ $t(`interestCalculator.advanced.${date}.legend`) }}
                </legend>
                <div class="grid grid-cols-[1fr_1fr_1.5fr] gap-3">
                    <!-- Each part carries its date's issue for screen readers; it shows once below -->
                    <UFormField
                        v-for="part in parts"
                        :key="part.name"
                        :name="part.name"
                        :error-pattern
                        :label="$t(`interestCalculator.date.${part.label}`)"
                        :ui="{ error: 'sr-only' }"
                    >
                        <UInput
                            v-model.number="state[part.name]"
                            type="number"
                            inputmode="numeric"
                            :min="part.min"
                            :max="part.max"
                            step="1"
                            class="w-full"
                        />
                    </UFormField>
                </div>
                <p
                    v-if="form?.getErrors(`${date}Day`)[0]"
                    aria-hidden="true"
                    class="mt-2 text-sm text-error"
                >
                    {{ form.getErrors(`${date}Day`)[0]?.message }}
                </p>
            </fieldset>
        </div>
 
        <div class="grid grid-cols-1 gap-6 sm:grid-cols-2">
            <UFormField
                name="principal"
                :label="$t('interestCalculator.principal.label')"
                :hint="$t('interestCalculator.principal.hint')"
            >
                <UInput
                    v-model="state.principal"
                    inputmode="decimal"
                    autocomplete="off"
                    class="w-full"
                />
            </UFormField>
            <UFormField
                name="concept"
                :label="$t('interestCalculator.concept.label')"
                :hint="$t('interestCalculator.concept.hint')"
            >
                <UInput v-model="state.concept" maxlength="120" class="w-full" />
            </UFormField>
        </div>
 
        <UButton
            type="submit"
            :label="$t('interestCalculator.submit')"
            class="justify-center sm:self-start"
        />
    </UForm>
</template>
 
<script setup lang="ts">
import type { FormErrorEvent, FormSubmitEvent } from "@nuxt/ui";
import { useErrorSummary } from "#layers/forms/app/composables/useErrorSummary";
import {
    type InterestAdvancedForm,
    interestAdvancedFormSchema,
} from "../schemas/interestAdvancedForm";
import type { InterestInput } from "../types/InterestInput";
import type { InterestRate } from "../types/InterestRate";
 
type DatePart = number | "";
 
interface FormState {
    concept: string;
    principal: string;
    rate: InterestRate;
    customPercent: string;
    fromDay: DatePart;
    fromMonth: DatePart;
    fromYear: DatePart;
    toDay: DatePart;
    toMonth: DatePart;
    toYear: DatePart;
}
 
const emit = defineEmits<{ submit: [input: InterestInput & { concept: string }]; edit: [] }>();
const PARTS = [
    { key: "Day", label: "day", min: 1, max: 31 },
    { key: "Month", label: "month", min: 1, max: 12 },
    { key: "Year", label: "year", min: 1900, max: 2100 },
] as const;
 
const DATES = (["from", "to"] as const).map((date) => ({
    date,
    errorPattern: new RegExp(`^${date}Day$`),
    parts: PARTS.map((part) => ({ ...part, name: `${date}${part.key}` as const })),
}));
 
const { t } = useI18n();
 
const form = useTemplateRef("form");
 
const schema = interestAdvancedFormSchema((issue) => t(`interestCalculator.errors.${issue}`));
 
const state = reactive<FormState>({
    concept: "",
    principal: "",
    rate: "legal",
    customPercent: "",
    fromDay: "",
    fromMonth: "",
    fromYear: "",
    toDay: "",
    toMonth: "",
    toYear: "",
});
 
// A date's issue sits on its day field, which the summary names after the whole date
const { errors, list } = useErrorSummary((field) =>
    t(`interestCalculator.${field.replace(/(Day|Month|Year)$/, "")}.label`),
);
 
const rates = useInterestRateOptions(["legal", "judicial", "custom"]);
 
// Nothing validates before the first submit, since an error rendered on blur moves the button
// under the pointer; once one has failed, every edit revalidates the whole form
const failed = ref(false);
 
const onError = (event: FormErrorEvent): void => {
    failed.value = true;
    list(event.errors);
};
 
const onSubmit = ({ data }: FormSubmitEvent<InterestAdvancedForm>) => {
    failed.value = false;
    list([]);
    emit("submit", {
        concept: data.concept,
        principal: data.principal,
        ratePercent: data.rate === "custom" ? data.customPercent : INTEREST_RATES[data.rate],
        from: data.from,
        to: data.to,
    });
};
 
watch(
    state,
    () => {
        emit("edit");
        if (failed.value) form.value?.validate({ silent: true });
    },
    { deep: true },
);
 
watch(
    () => form.value?.errors,
    (current) => {
        if (failed.value) list(current ?? []);
    },
);
</script>