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

100% Statements 66/66
100% Branches 17/17
100% Functions 24/24
100% Lines 64/64

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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267  26x   3x                       3x   3x   112x                   3x           3x 30x 3x                                           180x               3x               1x           3x           3x       3x     3x               3x                   10x                             1x                             17x   17x           17x 17x   17x 17x 17x 17x   17x   17x 10x 10x         10x 10x             10x 93x 9x 22x 44x       17x 21x     17x 11x 20x                 17x 11x   11x 20x                                   17x 1x 1x 7x 2x                                       1x 28x       17x 1x 1x     17x 4x 3x   3x     1x 1x 1x      
<template>
    <section :aria-labelledby="headingId">
        <BaseSectionHeading :id="headingId" :title="$t('interestCalculator.history.title')" />
        <p class="mt-4 max-w-measure font-serif text-base/relaxed text-pretty text-toned">
            {{ $t("interestCalculator.history.lead") }}
        </p>
 
        <p
            v-if="!rows.length"
            class="mt-6 rounded-sm border border-dashed border-default p-6 font-sans text-sm text-toned"
        >
            {{ $t("interestCalculator.history.empty") }}
        </p>
 
        <template v-else>
            <div class="relative mt-6 overflow-x-auto rounded-sm border border-default bg-muted">
                <table :aria-labelledby="headingId" class="w-full min-w-3xl font-sans text-sm">
                    <thead class="border-b border-default text-left text-toned">
                        <tr>
                            <th
                                v-for="column in COLUMNS"
                                :key="column"
                                scope="col"
                                class="px-3 py-2 font-semibold"
                                :class="column !== 'concept' && 'text-right'"
                            >
                                {{ $t(`interestCalculator.history.columns.${column}`) }}
                            </th>
                            <th scope="col" class="px-3 py-2">
                                <span class="sr-only">
                                    {{ $t("interestCalculator.history.columns.actions") }}
                                </span>
                            </th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-default tabular-nums">
                        <tr v-for="(row, index) in displayed.rows" :key="row.id">
                            <td class="px-3 py-1.5">
                                <UInput
                                    :model-value="row.concept"
                                    :aria-label="
                                        $t('interestCalculator.history.conceptLabel', {
                                            row: index + 1,
                                        })
                                    "
                                    maxlength="120"
                                    size="md"
                                    variant="ghost"
                                    class="w-full min-w-60"
                                    :ui="{ base: 'min-h-9 bg-transparent' }"
                                    @change="
                                        emit(
                                            'rename',
                                            row.id,
                                            ($event.target as HTMLInputElement).value,
                                        )
                                    "
                                />
                            </td>
                            <td
                                v-for="cell in CELLS"
                                :key="cell"
                                class="px-3 py-2 text-right whitespace-nowrap"
                                :class="cell === 'total' && 'font-semibold text-highlighted'"
                            >
                                {{ row[cell] }}
                            </td>
                            <td class="p-1 text-right">
                                <UButton
                                    variant="ghost"
                                    color="neutral"
                                    icon="i-lucide-trash-2"
                                    :aria-label="
                                        $t('interestCalculator.history.remove', { row: index + 1 })
                                    "
                                    @click="emit('remove', row.id)"
                                />
                            </td>
                        </tr>
                    </tbody>
                    <tfoot
                        class="border-t-2 border-inverted font-semibold text-highlighted tabular-nums"
                    >
                        <tr>
                            <th scope="row" class="px-3 py-2 text-left">
                                {{ $t("interestCalculator.history.totals") }}
                            </th>
                            <td class="px-3 py-2 text-right whitespace-nowrap">
                                {{ displayed.totals.principal }}
                            </td>
                            <td colspan="3" />
                            <td class="px-3 py-2 text-right whitespace-nowrap">
                                {{ displayed.totals.interest }}
                            </td>
                            <td class="px-3 py-2 text-right whitespace-nowrap">
                                {{ displayed.totals.total }}
                            </td>
                            <td />
                        </tr>
                    </tfoot>
                </table>
            </div>
            <div class="mt-4 flex flex-col gap-3 sm:flex-row">
                <ClientOnly>
                    <UButton
                        v-if="canCopy"
                        variant="outline"
                        color="neutral"
                        icon="i-lucide-copy"
                        :label="$t('interestCalculator.history.copy')"
                        class="justify-center"
                        @click="copyTable"
                    />
                </ClientOnly>
                <UButton
                    variant="ghost"
                    :color="confirmingClear ? 'error' : 'neutral'"
                    icon="i-lucide-eraser"
                    :label="
                        $t(
                            confirmingClear
                                ? 'interestCalculator.history.confirmClear'
                                : 'interestCalculator.history.clear',
                        )
                    "
                    class="justify-center"
                    @click="onClear"
                    @blur="confirmingClear = false"
                />
            </div>
        </template>
    </section>
</template>
 
<script setup lang="ts">
import { useClipboard } from "@vueuse/core";
import type { InterestEntry } from "../types/InterestEntry";
 
interface Props {
    entries: InterestEntry[];
}
 
const props = defineProps<Props>();
 
const emit = defineEmits<{
    rename: [id: string, concept: string];
    remove: [id: string];
    clear: [];
}>();
 
const CELLS = ["principal", "from", "to", "rate", "interest", "total"] as const;
const COLUMNS = ["concept", ...CELLS] as const;
 
const { t, locale } = useI18n();
const toast = useToast();
const headingId = useId();
const { copy, isSupported: canCopy } = useClipboard();
 
const confirmingClear = ref(false);
 
const formats = computed(() => {
    const euros = new Intl.NumberFormat(locale.value, { style: "currency", currency: "EUR" });
    const plain = new Intl.NumberFormat(locale.value, {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
        useGrouping: false,
    });
    const percent = new Intl.NumberFormat(locale.value, { maximumFractionDigits: 4 });
    const date = new Intl.DateTimeFormat(locale.value, {
        day: "2-digit",
        month: "2-digit",
        year: "numeric",
        timeZone: "UTC",
    });
 
    return {
        euros: (cents: number) => euros.format(cents / 100),
        plain: (cents: number) => plain.format(cents / 100),
        percent: (value: number) => percent.format(value),
        date: (iso: string) => date.format(Date.parse(`${iso}T00:00:00Z`)),
    };
});
 
const rows = computed(() =>
    props.entries.map((entry) => ({ ...entry, principalCents: Math.round(entry.principal * 100) })),
);
 
const totals = computed(() =>
    rows.value.reduce(
        (sum, row) => ({
            principalCents: sum.principalCents + row.principalCents,
            interestCents: sum.interestCents + row.interestCents,
            totalCents: sum.totalCents + row.totalCents,
        }),
        { principalCents: 0, interestCents: 0, totalCents: 0 },
    ),
);
 
const displayed = computed(() => {
    const { euros, percent, date } = formats.value;
 
    return {
        rows: rows.value.map((row) => ({
            id: row.id,
            concept: row.concept,
            principal: euros(row.principalCents),
            from: date(row.from),
            to: date(row.to),
            rate: percent(row.ratePercent),
            interest: euros(row.interestCents),
            total: euros(row.totalCents),
        })),
        totals: {
            principal: euros(totals.value.principalCents),
            interest: euros(totals.value.interestCents),
            total: euros(totals.value.totalCents),
        },
    };
});
 
const toTsv = (): string => {
    const { plain, percent, date } = formats.value;
    const lines = [
        COLUMNS.map((column) => t(`interestCalculator.history.columns.${column}`)),
        ...rows.value.map((row) => [
            row.concept,
            plain(row.principalCents),
            date(row.from),
            date(row.to),
            percent(row.ratePercent),
            plain(row.interestCents),
            plain(row.totalCents),
        ]),
        [
            t("interestCalculator.history.totals"),
            plain(totals.value.principalCents),
            "",
            "",
            "",
            plain(totals.value.interestCents),
            plain(totals.value.totalCents),
        ],
    ];
 
    return lines
        .map((cells) => cells.map((cell) => cell.replace(/[\t\n]/g, " ")).join("\t"))
        .join("\n");
};
 
const copyTable = async (): Promise<void> => {
    await copy(toTsv());
    toast.add({ title: t("interestCalculator.history.copied"), icon: "i-lucide-check" });
};
 
const onClear = (): void => {
    if (!confirmingClear.value) {
        confirmingClear.value = true;
 
        return;
    }
 
    confirmingClear.value = false;
    emit("clear");
    toast.add({ title: t("interestCalculator.history.cleared"), icon: "i-lucide-check" });
};
</script>