Skip to content

Open source personal finance application

fix(wallets): update WalletForm to WalletItem and normalize wallet handling

Ilya Komichev5058caefe750
diff --git a/app/components/wallets/Form.vue b/app/components/wallets/Form.vue
index f07c838d..20e5390f 100644
--- a/app/components/wallets/Form.vue
+++ b/app/components/wallets/Form.vue
@@ -2,21 +2,22 @@
import { generateId } from '~~/utils/generateId'
import type { CurrencyCode } from '~/components/currencies/types'
-import type { WalletForm, WalletId, WalletType } from '~/components/wallets/types'
+import type { WalletForm, WalletId, WalletItem, WalletType } from '~/components/wallets/types'
import { errorEmo, random } from '~/assets/js/emo'
import UiToastContent from '~/components/ui/ToastContent.vue'
import { icons, types } from '~/components/wallets/types'
import { useWalletsStore } from '~/components/wallets/useWalletsStore'
+import { normalizeWalletItem } from '~/components/wallets/utils'
const props = defineProps<{
- walletForm: WalletForm
+ walletForm: WalletItem
walletId?: WalletId
}>()
const emit = defineEmits<{
afterSave: []
- updateValue: [key: keyof WalletForm, value: string | number | boolean]
+ updateValue: [key: keyof WalletItem, value: WalletItem[keyof WalletItem]]
}>()
const { $toast } = useNuxtApp()
@@ -95,7 +96,10 @@ async function onSave() {
if (!validate(props.walletForm))
return
- await walletsStore.addWallet({ id: editWalletId, values: props.walletForm })
+ await walletsStore.createOrUpdateWallet({
+ id: editWalletId,
+ values: normalizeWalletItem(props.walletForm),
+ })
emit('afterSave')
}
</script>
@@ -177,17 +181,13 @@ async function onSave() {
</template>
<FormInput
:placeholder="t('wallets.form.credit.limit')"
- :value="props.walletForm.creditLimit ?? 0"
+ :value="props.walletForm.creditLimit ?? ''"
@updateValue="(value: string) => emit('updateValue', 'creditLimit', value)"
/>
</FormElement>
<!-- Options -->
<div class="grid gap-1">
- <!-- <UiTitle3>
- {{ t('settings.options') }}
- </UiTitle3>
- -->
<div>
<UiCheckbox
:checkboxValue="props.walletForm.isWithdrawal ?? false"
diff --git a/app/components/wallets/page/id/Edit.vue b/app/components/wallets/page/id/Edit.vue
index eba4a1f9..326f6e67 100644
--- a/app/components/wallets/page/id/Edit.vue
+++ b/app/components/wallets/page/id/Edit.vue
@@ -1,5 +1,5 @@
<script setup lang="ts">
-import type { WalletForm, WalletId } from '~/components/wallets/types'
+import type { WalletForm, WalletId, WalletItem } from '~/components/wallets/types'
import { useWalletsStore } from '~/components/wallets/useWalletsStore'
import { normalizeWalletItem } from '~/components/wallets/utils'
@@ -32,7 +32,7 @@ useHead({ title: `${t('base.edit')}: ${walletForm.value?.name || t('wallets.form
:walletId
:walletForm
@afterSave="() => router.replace(`/wallets/${walletId}`)"
- @updateValue="(key: keyof WalletForm, newValue: any) => walletForm[key] = newValue"
+ @updateValue="(key: keyof WalletForm, value: WalletItem[keyof WalletItem]) => walletForm[key] = value"
/>
</UiPage>
</template>
diff --git a/app/components/wallets/useWalletsStore.ts b/app/components/wallets/useWalletsStore.ts
index 05fd95bf..fadae09c 100644
--- a/app/components/wallets/useWalletsStore.ts
+++ b/app/components/wallets/useWalletsStore.ts
@@ -5,14 +5,14 @@ import { uniqueElementsBy } from '~~/utils/simple'
import type { CurrencyCode } from '~/components/currencies/types'
import type { TrnId } from '~/components/trns/types'
-import type { WalletForm, WalletId, WalletItemComputed, Wallets, WalletsComputed } from '~/components/wallets/types'
+import type { WalletId, WalletItem, WalletItemComputed, Wallets, WalletsComputed } from '~/components/wallets/types'
import { getAmountInRate, getTotal } from '~/components/amount/getTotal'
import { useCurrenciesStore } from '~/components/currencies/useCurrenciesStore'
import { useDemo } from '~/components/demo/useDemo'
import { useTrnsStore } from '~/components/trns/useTrnsStore'
import { useUserStore } from '~/components/user/useUserStore'
-import { normalizeWalletItem, normalizeWallets } from '~/components/wallets/utils'
+import { normalizeWallets } from '~/components/wallets/utils'
export const useWalletsStore = defineStore('wallets', () => {
const trnsStore = useTrnsStore()
@@ -33,16 +33,15 @@ export const useWalletsStore = defineStore('wallets', () => {
localforage.setItem('finapp.wallets', deepUnref(wallets))
}
- async function addWallet({ id, values }: { id: WalletId, values: WalletForm }) {
- const walletValues = normalizeWalletItem(values)
+ async function createOrUpdateWallet({ id, values }: { id: WalletId, values: WalletItem }) {
// Set default currency based on first created wallet
if (!hasItems.value)
- currenciesStore.updateBase(walletValues.currency)
+ currenciesStore.updateBase(values.currency)
if (isDemo.value) {
items.value = {
...items.value,
- [id]: walletValues,
+ [id]: values,
}
localforage.setItem('finapp.wallets', deepUnref(items.value))
return
@@ -132,7 +131,7 @@ export const useWalletsStore = defineStore('wallets', () => {
}
return {
- addWallet,
+ createOrUpdateWallet,
currenciesUsed,
deleteWallet,
getWalletTotal,
diff --git a/app/components/wallets/utils.ts b/app/components/wallets/utils.ts
index ba773911..2c6e835a 100644
--- a/app/components/wallets/utils.ts
+++ b/app/components/wallets/utils.ts
@@ -23,11 +23,11 @@ export function normalizeWalletItem(wallet?: WalletItemDirty | WalletForm) {
// Set type based on the wallet type property
if (wallet?.type === 'cash' || wallet?.isCash)
walletItem.type = 'cash'
- if (wallet?.type === 'cashless' || wallet?.isCashless)
+ else if (wallet?.type === 'cashless' || wallet?.isCashless)
walletItem.type = 'cashless'
- if (wallet?.type === 'debt' || wallet?.isDebt)
+ else if (wallet?.type === 'debt' || wallet?.isDebt)
walletItem.type = 'debt'
- if (wallet?.type === 'deposit' || wallet?.isDeposit)
+ else if (wallet?.type === 'deposit' || wallet?.isDeposit)
walletItem.type = 'deposit'
return walletItem