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

96% Statements 24/25
93.75% Branches 15/16
100% Functions 12/12
100% Lines 22/22

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  16x   4x             2x       4x 13x         2x 16x           3x   53x                                                       8x   8x   8x         8x   8x 16x             8x 6x             8x 3x   3x   3x 3x      
<template>
    <div class="flex flex-col gap-16 lg:gap-20">
        <UTabs
            v-model="mode"
            :items="tabs"
            :unmount-on-hide="false"
            variant="link"
            :ui="{ trigger: 'text-base', content: 'pt-8' }"
        >
            <template v-for="tab in tabs" :key="tab.value" #[tab.slot]>
                <div class="grid grid-cols-1 gap-8 lg:grid-cols-12 lg:gap-x-12">
                    <InterestCalculatorForm
                        v-if="tab.value === 'simple'"
                        class="lg:col-span-7"
                        @submit="calculate('simple', $event)"
                        @edit="calculations.simple = undefined"
                    />
                    <InterestCalculatorAdvancedForm
                        v-else
                        class="lg:col-span-7"
                        @submit="calculate('advanced', $event)"
                        @edit="calculations.advanced = undefined"
                    />
                    <InterestCalculatorResult
                        :result="calculations[tab.value]?.result"
                        :added="calculations[tab.value]?.added"
                        class="lg:col-span-5"
                        @add="add(tab.value)"
                    />
                </div>
            </template>
        </UTabs>
        <InterestCalculatorHistory
            :entries="history.entries.value"
            @rename="history.rename"
            @remove="history.remove"
            @clear="history.clear"
        />
    </div>
</template>
 
<script setup lang="ts">
import type { InterestInput } from "../types/InterestInput";
import type { InterestResult } from "../types/InterestResult";
import InterestCalculatorAdvancedForm from "./InterestCalculatorAdvancedForm.vue";
import InterestCalculatorForm from "./InterestCalculatorForm.vue";
import InterestCalculatorHistory from "./InterestCalculatorHistory.vue";
import InterestCalculatorResult from "./InterestCalculatorResult.vue";
 
type Mode = "simple" | "advanced";
 
interface Calculation {
    input: InterestInput & { concept: string };
    result: InterestResult;
    added: boolean;
}
 
const { t } = useI18n();
 
const mode = ref<Mode>("simple");
 
const calculations = reactive<Record<Mode, Calculation | undefined>>({
    simple: undefined,
    advanced: undefined,
});
 
const history = useInterestHistory();
 
const tabs = computed(() =>
    (["simple", "advanced"] as const).map((value) => ({
        value,
        slot: value,
        label: t(`interestCalculator.tabs.${value}`),
    })),
);
 
const calculate = (into: Mode, input: InterestInput & { concept?: string }): void => {
    calculations[into] = {
        input: { concept: "", ...input },
        result: calculateInterest(input),
        added: false,
    };
};
 
const add = (from: Mode): void => {
    const calculation = calculations[from];
 
    Iif (!calculation || calculation.added) return;
 
    history.add({ ...calculation.input, ...calculation.result });
    calculation.added = true;
};
</script>