Open source personal finance application
refactor: improve trnForm type safety, fix bugs, extract helpers
Ilya Komichevd57090fc73d2
diff --git a/app/components/trnForm/Bottom.vue b/app/components/trnForm/Bottom.vueindex b55de6d3..7f8e06aa 100644--- a/app/components/trnForm/Bottom.vue+++ b/app/components/trnForm/Bottom.vue@@ -17,9 +17,6 @@ const isShow = computed(() =>&& trnsFormStore.ui.isShow,)-/**- * Slider- */const sliderRef = ref<HTMLElement | null>(null)const sliderObj = ref<Swiper | null>(null)const maxHeight = ref('550px')@@ -27,6 +24,8 @@ const maxHeight = ref('550px')let resizeObserver: ResizeObserver | null = nullfunction setTrnFormHeight() {+ resizeObserver?.disconnect()+const el = document.querySelector('.getHeight')resizeObserver = new ResizeObserver((entries) => {@@ -47,7 +46,6 @@ onBeforeUnmount(() => {function init() {if (!sliderObj.value) {sliderObj.value = new Swiper(sliderRef.value!, {- // centeredSlides: true,init: false,initialSlide: 1,longSwipesMs: 60,diff --git a/app/components/trnForm/Main.vue b/app/components/trnForm/Main.vueindex fba0e68e..8b392dbe 100644--- a/app/components/trnForm/Main.vue+++ b/app/components/trnForm/Main.vue@@ -4,6 +4,7 @@ import type { WalletId } from '~/components/wallets/types'import { useCategoriesStore } from '~/components/categories/useCategoriesStore'import { useTrnsFormStore } from '~/components/trnForm/useTrnsFormStore'+import { TrnType } from '~/components/trns/types'import { useWalletsStore } from '~/components/wallets/useWalletsStore'const { maxHeight = '60vh' } = defineProps<{@@ -33,10 +34,10 @@ const walletId = computed(() =></div><TrnFormMainInput- v-if="trnsFormStore.values.trnType !== 2"+ v-if="trnsFormStore.values.trnType !== TrnType.Transfer":amount="trnsFormStore.values.amount[trnsFormStore.activeAmountIdx]":amountRaw="trnsFormStore.values.amountRaw[trnsFormStore.activeAmountIdx]"- :highlight="trnsFormStore.values.trnType === 0 ? 'expense' : 'income'"+ :highlight="trnsFormStore.values.trnType === TrnType.Expense ? 'expense' : 'income'":isShowSum="trnsFormStore.shouldShowSum()"class="px-3 pb-2"@change="trnsFormStore.onChangeAmount"@@ -45,7 +46,7 @@ const walletId = computed(() =><div class="grid gap-3 px-3 pb-6"><!-- Selected --><div- v-if="trnsFormStore.values.trnType !== 2"+ v-if="trnsFormStore.values.trnType !== TrnType.Transfer"class="grid grid-cols-2 gap-2"><TrnFormSelectorWallet@@ -67,7 +68,7 @@ const walletId = computed(() =></div><TrnFormMainAmountTransfer- v-if="trnsFormStore.values.trnType === 2"+ v-if="trnsFormStore.values.trnType === TrnType.Transfer":bottomSheetStyle="{ maxHeight }"/>diff --git a/app/components/trnForm/Sidebar.vue b/app/components/trnForm/Sidebar.vueindex a576ed7c..64742e47 100644--- a/app/components/trnForm/Sidebar.vue+++ b/app/components/trnForm/Sidebar.vue@@ -3,6 +3,7 @@ import Swiper from 'swiper'import { Pagination } from 'swiper/modules'import { useTrnsFormStore } from '~/components/trnForm/useTrnsFormStore'+import { TrnType } from '~/components/trns/types'import 'swiper/css'@@ -10,9 +11,6 @@ const { t } = useI18n()const trnsFormStore = useTrnsFormStore()const isShow = computed(() => trnsFormStore.ui.isShow)-/**- * Slider- */const sliderRef = ref<HTMLElement | null>(null)const sliderObj = ref<Swiper | null>(null)@@ -67,7 +65,7 @@ watch(isShow, async (v) => {class="!pb-0"/><div- v-if="trnsFormStore.values.trnType !== 2"+ v-if="trnsFormStore.values.trnType !== TrnType.Transfer"class="-mt-8 grid gap-2 pb-6"><TrnFormSelectionCategoriesFastdiff --git a/app/components/trnForm/TrnsSlide.vue b/app/components/trnForm/TrnsSlide.vueindex c0ae0bb1..8b011dd7 100644--- a/app/components/trnForm/TrnsSlide.vue+++ b/app/components/trnForm/TrnsSlide.vue@@ -37,7 +37,8 @@ const trnsIds = computed(() => {if (trnsFormStore.values.walletId)walletsIds.push(trnsFormStore.values.walletId)- categoriesIds = categoriesStore.getChildrenIdsOrParent(trnsFormStore.values.categoryId!)+ if (trnsFormStore.values.categoryId)+ categoriesIds = categoriesStore.getChildrenIdsOrParent(trnsFormStore.values.categoryId)}return trnsStore.getStoreTrnsIds({diff --git a/app/components/trnForm/useTrnsFormStore.ts b/app/components/trnForm/useTrnsFormStore.tsindex 26422281..fc19eb59 100644--- a/app/components/trnForm/useTrnsFormStore.ts+++ b/app/components/trnForm/useTrnsFormStore.ts@@ -42,7 +42,7 @@ export const useTrnsFormStore = defineStore('trnForm', () => {incomeWalletId: null,transferType: 'expense',trnId: null,- trnType: 0,+ trnType: TrnType.Expense,walletId: null,})@@ -68,9 +68,17 @@ export const useTrnsFormStore = defineStore('trnForm', () => {: values.transferType === 'expense' ? 1 : 2,)+ function setAmountAt(idx: number, raw: string) {+ values.amount[idx] = evaluateExpression(raw)+ values.amountRaw[idx] = formatInput(raw)+ }++ function syncAmountRawAt(idx: number) {+ values.amountRaw[idx] = formatInput(values.amount[idx])+ }+function onChangeAmount(amountRaw: string) {- values.amount[activeAmountIdx.value] = evaluateExpression(amountRaw)- values.amountRaw[activeAmountIdx.value] = formatInput(amountRaw)+ setAmountAt(activeAmountIdx.value, amountRaw)}function onClickCalculator(key: CalculatorKey) {@@ -116,7 +124,7 @@ export const useTrnsFormStore = defineStore('trnForm', () => {values.incomeWalletId = nullvalues.transferType = 'expense'values.trnId = null- values.trnType = 0+ values.trnType = TrnType.Expensevalues.walletId = nullui.value = {@@ -138,7 +146,7 @@ export const useTrnsFormStore = defineStore('trnForm', () => {values.trnId = nullif (props.action === 'create') {- values.trnType = 0+ values.trnType = TrnType.Expenseif (!values.walletId || !props.walletsIds.includes(values.walletId))values.walletId = props.walletId ?? (props.trn && 'walletId' in props.trn ? props.trn.walletId : undefined) ?? props.walletsIds[0] ?? nullif (!values.categoryId || !props.categoriesIds.includes(values.categoryId as CategoryId))@@ -207,20 +215,13 @@ export const useTrnsFormStore = defineStore('trnForm', () => {}function onChangeCountSum() {- // Transferif (values.trnType === TrnType.Transfer) {- values.amountRaw[1] = formatInput(values.amount[1])- values.amountRaw[2] = formatInput(values.amount[2])- return- }-- // Transaction- if (values.amount[0] !== 0) {- values.amountRaw[0] = formatInput(values.amount[0])+ syncAmountRawAt(1)+ syncAmountRawAt(2)return}- values.amountRaw[0] = formatInput(0)+ syncAmountRawAt(0)}function openFormForEdit(trnId: TrnId) {diff --git a/app/components/trnForm/utils/validate.ts b/app/components/trnForm/utils/validate.tsindex e6e7ade2..5578bdfb 100644--- a/app/components/trnForm/utils/validate.ts+++ b/app/components/trnForm/utils/validate.ts@@ -8,8 +8,11 @@ export function validate(values: Transaction | Transfer): string | null {if (!result.success) {const issue = result.error.issues[0]- if (values.type === TrnType.Transfer)+ if (values.type === TrnType.Transfer) {+ if (issue?.path?.includes('incomeWalletId') || issue?.path?.includes('expenseWalletId'))+ return 'trnForm.errors.selectWallet'return 'trnForm.errors.transferAmountEmpty'+ }if (issue?.path?.includes('amount'))return 'trnForm.errors.amountEmpty'