Skip to content

Open source personal finance application

feat: single amount input for same-currency transfers

Ilya Komichevcb7cc2239938
diff --git a/app/components/trnForm/SelectorWallet.vue b/app/components/trnForm/SelectorWallet.vue
index 2d32e294..02773310 100644
--- a/app/components/trnForm/SelectorWallet.vue
+++ b/app/components/trnForm/SelectorWallet.vue
@@ -5,6 +5,7 @@ import { useWalletsStore } from '~/components/wallets/useWalletsStore'
const props = defineProps<{
bottomSheetStyle?: Record<string, string>
+ disabledWalletIds?: WalletId[]
title?: string
walletId: WalletId
}>()
@@ -43,6 +44,7 @@ const isShow = ref(false)
<WalletsSelector
:hide="close"
:activeItemId="props.walletId"
+ :disabledIds="props.disabledWalletIds"
class="min-w-80 px-2"
@selected="(id: WalletId) => emit('selected', id)"
/>
diff --git a/app/components/trnForm/main/AmountTransfer.vue b/app/components/trnForm/main/AmountTransfer.vue
index 9c03967d..7cf4e335 100644
--- a/app/components/trnForm/main/AmountTransfer.vue
+++ b/app/components/trnForm/main/AmountTransfer.vue
@@ -1,18 +1,15 @@
<script setup lang="ts">
import type { MoneyTypeSlug } from '~/components/stat/types'
import type { TransferSide } from '~/components/trns/types'
-import type { WalletId } from '~/components/wallets/types'
import { useTrnsFormStore } from '~/components/trnForm/useTrnsFormStore'
import { TrnType } from '~/components/trns/types'
-import { useWalletsStore } from '~/components/wallets/useWalletsStore'
const props = defineProps<{
bottomSheetStyle?: Record<string, string>
}>()
const trnsFormStore = useTrnsFormStore()
-const walletsStore = useWalletsStore()
const { t } = useI18n()
const items = ref<Record<MoneyTypeSlug, { amountsIdx: 1 | 2, transferType: TransferSide }>>({
@@ -26,23 +23,15 @@ const items = ref<Record<MoneyTypeSlug, { amountsIdx: 1 | 2, transferType: Trans
},
})
-const incomeWalletId = computed<WalletId | undefined>(
- () => trnsFormStore.values.incomeWalletId ?? walletsStore.sortedIds[0],
-)
-
-const expenseWalletId = computed<WalletId | undefined>(
- () => trnsFormStore.values.expenseWalletId ?? walletsStore.sortedIds[1] ?? walletsStore.sortedIds[0],
-)
-
watch(
() => trnsFormStore.values.trnType,
(trnType) => {
if (trnType === TrnType.Transfer) {
if (!trnsFormStore.values.incomeWalletId)
- incomeWalletId.value && (trnsFormStore.values.incomeWalletId = incomeWalletId.value)
+ trnsFormStore.transferIncomeWalletId && (trnsFormStore.values.incomeWalletId = trnsFormStore.transferIncomeWalletId)
if (!trnsFormStore.values.expenseWalletId)
- expenseWalletId.value && (trnsFormStore.values.expenseWalletId = expenseWalletId.value)
+ trnsFormStore.transferExpenseWalletId && (trnsFormStore.values.expenseWalletId = trnsFormStore.transferExpenseWalletId)
if (trnsFormStore.values.amount[1] === 0) {
trnsFormStore.values.amount[1] = trnsFormStore.values.amount[0]
@@ -52,97 +41,114 @@ watch(
},
{ immediate: true },
)
-
-function switchWallets() {
- const incomeWalletId = trnsFormStore.values.incomeWalletId
- const expenseWalletId = trnsFormStore.values.expenseWalletId
- const incomeAmount = trnsFormStore.values.amount[1]
- const incomeAmountRaw = trnsFormStore.values.amountRaw[1]
- const expenseAmount = trnsFormStore.values.amount[2]
- const expenseAmountRaw = trnsFormStore.values.amountRaw[2]
-
- trnsFormStore.values.incomeWalletId = expenseWalletId
- trnsFormStore.values.expenseWalletId = incomeWalletId
- trnsFormStore.values.amount[1] = expenseAmount
- trnsFormStore.values.amountRaw[1] = expenseAmountRaw
- trnsFormStore.values.amount[2] = incomeAmount
- trnsFormStore.values.amountRaw[2] = incomeAmountRaw
-}
-
-function copyAmount() {
- const incomeAmount = trnsFormStore.values.amount[1]
- const incomeAmountRaw = trnsFormStore.values.amountRaw[1]
-
- trnsFormStore.values.amount[2] = incomeAmount
- trnsFormStore.values.amountRaw[2] = incomeAmountRaw
-}
</script>
<template>
<div>
- <template
- v-for="(item, slug) in items"
- :key="slug"
- >
- <div
- :class="cn(
- '-m-1 grid gap-2 overflow-hidden rounded-sm border border-transparent p-2',
- trnsFormStore.values.transferType === item.transferType && 'border-(--ui-primary)/50',
- )"
- @click="trnsFormStore.onChangeTransferType(item.transferType)"
- >
- <div class="flex items-center gap-2 whitespace-nowrap">
- <div
- :class="[{
- 'bg-(--item-5)': trnsFormStore.values.transferType === item.transferType,
- }]"
- class="text-1/70 bg-item-3 interactive flex min-h-[44px] w-1/2 grow items-center rounded-sm px-3 py-2 text-sm lg:min-h-[42px]"
- >
- {{ t(`trnForm.transfer.${slug}`) }}
- </div>
-
- <TrnFormSelectorWallet
- v-if="slug === 'income' && incomeWalletId"
- :bottomSheetStyle="props.bottomSheetStyle"
- :title="t(`trnForm.transfer.${slug}`)"
- :walletId="incomeWalletId"
- @selected="id => trnsFormStore.values.incomeWalletId = id"
- />
+ <!-- Same currency: input first, then wallets -->
+ <template v-if="trnsFormStore.isSameCurrencyTransfer">
+ <TrnFormMainInput
+ :amount="trnsFormStore.values.amount[1]"
+ :amountRaw="trnsFormStore.values.amountRaw[1]"
+ :isShowSum="trnsFormStore.shouldShowSum()"
+ @change="trnsFormStore.onChangeTransferAmountSynced"
+ />
+
+ <div class="flex items-center gap-2 pt-3">
+ <TrnFormSelectorWallet
+ v-if="trnsFormStore.transferExpenseWalletId"
+ :bottomSheetStyle="props.bottomSheetStyle"
+ :disabledWalletIds="trnsFormStore.transferIncomeWalletId ? [trnsFormStore.transferIncomeWalletId] : []"
+ :title="t('trnForm.transfer.expenseModal')"
+ :walletId="trnsFormStore.transferExpenseWalletId"
+ class="min-w-0 flex-1"
+ @selected="id => trnsFormStore.onTransferWalletSelected('expense', id)"
+ />
- <TrnFormSelectorWallet
- v-if="slug === 'expense' && expenseWalletId"
- :bottomSheetStyle="props.bottomSheetStyle"
- :title="t(`trnForm.transfer.${slug}`)"
- :walletId="expenseWalletId"
- @selected="id => trnsFormStore.values.expenseWalletId = id"
- />
- </div>
+ <UiActionButton :ariaLabel="$t('wallets.ariaSwitch')" @click="trnsFormStore.switchTransferWallets">
+ <Icon name="lucide:repeat" size="24" />
+ </UiActionButton>
- <!-- Input -->
- <TrnFormMainInput
- :key="item.amountsIdx"
- :amount="trnsFormStore.values.amount[item.amountsIdx]"
- :amountRaw="trnsFormStore.values.amountRaw[item.amountsIdx]"
- :highlight="item.transferType"
- :isShowSum="trnsFormStore.shouldShowSum()"
- isTransfer
- @change="trnsFormStore.onChangeAmount"
+ <TrnFormSelectorWallet
+ v-if="trnsFormStore.transferIncomeWalletId"
+ :bottomSheetStyle="props.bottomSheetStyle"
+ :disabledWalletIds="trnsFormStore.transferExpenseWalletId ? [trnsFormStore.transferExpenseWalletId] : []"
+ :title="t('trnForm.transfer.incomeModal')"
+ :walletId="trnsFormStore.transferIncomeWalletId"
+ class="min-w-0 flex-1"
+ @selected="id => trnsFormStore.onTransferWalletSelected('income', id)"
/>
</div>
+ </template>
- <!-- Actions -->
- <div
- v-if="item.transferType === 'expense'"
- class="flex justify-center gap-1 pt-3 pb-1"
+ <!-- Different currencies: current two-input layout -->
+ <template v-else>
+ <template
+ v-for="(item, slug) in items"
+ :key="slug"
>
- <UiActionButton :ariaLabel="$t('wallets.ariaSwitch')" @click="switchWallets">
- <Icon name="lucide:arrow-up-down" size="24" />
- </UiActionButton>
+ <div
+ :class="cn(
+ '-m-1 grid gap-2 overflow-hidden rounded-sm border border-transparent p-2',
+ trnsFormStore.values.transferType === item.transferType && 'border-(--ui-primary)/50',
+ )"
+ @click="trnsFormStore.onChangeTransferType(item.transferType)"
+ >
+ <div class="flex items-center gap-2 whitespace-nowrap">
+ <div
+ :class="[{
+ 'bg-(--item-5)': trnsFormStore.values.transferType === item.transferType,
+ }]"
+ class="text-1/70 bg-item-3 interactive flex min-h-[44px] w-1/2 grow items-center rounded-sm px-3 py-2 text-sm lg:min-h-[42px]"
+ >
+ {{ t(`trnForm.transfer.${slug}Label`) }}
+ </div>
+
+ <TrnFormSelectorWallet
+ v-if="slug === 'income' && trnsFormStore.transferIncomeWalletId"
+ :bottomSheetStyle="props.bottomSheetStyle"
+ :disabledWalletIds="trnsFormStore.transferExpenseWalletId ? [trnsFormStore.transferExpenseWalletId] : []"
+ :title="t(`trnForm.transfer.${slug}Modal`)"
+ :walletId="trnsFormStore.transferIncomeWalletId"
+ @selected="id => trnsFormStore.onTransferWalletSelected('income', id)"
+ />
+
+ <TrnFormSelectorWallet
+ v-if="slug === 'expense' && trnsFormStore.transferExpenseWalletId"
+ :bottomSheetStyle="props.bottomSheetStyle"
+ :disabledWalletIds="trnsFormStore.transferIncomeWalletId ? [trnsFormStore.transferIncomeWalletId] : []"
+ :title="t(`trnForm.transfer.${slug}Modal`)"
+ :walletId="trnsFormStore.transferExpenseWalletId"
+ @selected="id => trnsFormStore.onTransferWalletSelected('expense', id)"
+ />
+ </div>
- <UiActionButton :ariaLabel="$t('trnForm.ariaCopyAmount')" @click="copyAmount">
- <Icon name="lucide:clipboard-paste" size="24" />
- </UiActionButton>
- </div>
+ <!-- Input -->
+ <TrnFormMainInput
+ :key="item.amountsIdx"
+ :amount="trnsFormStore.values.amount[item.amountsIdx]"
+ :amountRaw="trnsFormStore.values.amountRaw[item.amountsIdx]"
+ :highlight="item.transferType"
+ :isShowSum="trnsFormStore.shouldShowSum()"
+ isTransfer
+ @change="trnsFormStore.onChangeAmount"
+ />
+ </div>
+
+ <!-- Actions -->
+ <div
+ v-if="item.transferType === 'expense'"
+ class="flex justify-center gap-1 pt-3 pb-1"
+ >
+ <UiActionButton :ariaLabel="$t('wallets.ariaSwitch')" @click="trnsFormStore.switchTransferWallets">
+ <Icon name="lucide:arrow-up-down" size="24" />
+ </UiActionButton>
+
+ <UiActionButton :ariaLabel="$t('trnForm.ariaCopyAmount')" @click="trnsFormStore.copyTransferAmount">
+ <Icon name="lucide:clipboard-paste" size="24" />
+ </UiActionButton>
+ </div>
+ </template>
</template>
</div>
</template>
diff --git a/app/components/trnForm/useTrnsFormStore.ts b/app/components/trnForm/useTrnsFormStore.ts
index e04ce1d2..5127918d 100644
--- a/app/components/trnForm/useTrnsFormStore.ts
+++ b/app/components/trnForm/useTrnsFormStore.ts
@@ -77,13 +77,93 @@ export const useTrnsFormStore = defineStore('trnForm', () => {
values.amountRaw[idx] = formatInput(values.amount[idx] ?? 0)
}
+ const transferIncomeWalletId = computed<WalletId | undefined>(
+ () => values.incomeWalletId ?? walletsStore.sortedIds[0],
+ )
+
+ const transferExpenseWalletId = computed<WalletId | undefined>(
+ () => values.expenseWalletId ?? walletsStore.sortedIds[1] ?? walletsStore.sortedIds[0],
+ )
+
+ const isSameCurrencyTransfer = computed(() => {
+ if (!transferExpenseWalletId.value || !transferIncomeWalletId.value)
+ return false
+
+ const expenseWallet = walletsStore.items?.[transferExpenseWalletId.value]
+ const incomeWallet = walletsStore.items?.[transferIncomeWalletId.value]
+
+ if (!expenseWallet || !incomeWallet)
+ return false
+
+ return expenseWallet.currency === incomeWallet.currency
+ })
+
function onChangeAmount(amountRaw: string) {
setAmountAt(activeAmountIdx.value, amountRaw)
}
+ function onChangeTransferAmountSynced(amountRaw: string) {
+ setAmountAt(1, amountRaw)
+ setAmountAt(2, amountRaw)
+ }
+
function onClickCalculator(key: CalculatorKey) {
const value = createExpressionString(key, values.amountRaw[activeAmountIdx.value] ?? '')
- onChangeAmount(value)
+ if (isSameCurrencyTransfer.value)
+ onChangeTransferAmountSynced(value)
+ else
+ onChangeAmount(value)
+ }
+
+ function onTransferWalletSelected(side: 'expense' | 'income', id: WalletId) {
+ const wasSame = isSameCurrencyTransfer.value
+
+ if (side === 'expense')
+ values.expenseWalletId = id
+ else
+ values.incomeWalletId = id
+
+ // same → different: reset income amount
+ if (wasSame && !isSameCurrencyTransfer.value) {
+ values.amount[2] = 0
+ values.amountRaw[2] = ''
+ }
+
+ // different → same: copy expense amount to income, reset transferType
+ if (!wasSame && isSameCurrencyTransfer.value) {
+ values.amount[2] = values.amount[1]
+ values.amountRaw[2] = values.amountRaw[1]
+ values.transferType = 'expense'
+ }
+ }
+
+ function switchTransferWallets() {
+ const incomeId = values.incomeWalletId
+ const expenseId = values.expenseWalletId
+
+ values.incomeWalletId = expenseId
+ values.expenseWalletId = incomeId
+
+ if (isSameCurrencyTransfer.value) {
+ values.amount[2] = values.amount[1]
+ values.amountRaw[2] = values.amountRaw[1]
+ }
+ else {
+ const amt1 = values.amount[1]
+ const raw1 = values.amountRaw[1]
+ const amt2 = values.amount[2]
+ const raw2 = values.amountRaw[2]
+
+ values.amount[1] = amt2
+ values.amountRaw[1] = raw2
+ values.amount[2] = amt1
+ values.amountRaw[2] = raw1
+ }
+ }
+
+ function copyTransferAmount() {
+ values.amount[2] = values.amount[1]
+ values.amountRaw[2] = values.amountRaw[1]
}
function shouldShowSum() {
@@ -270,20 +350,27 @@ export const useTrnsFormStore = defineStore('trnForm', () => {
$reset,
activeAmountIdx,
closeTrnFormModal,
+ copyTransferAmount,
+ isSameCurrencyTransfer,
modal,
onChangeAmount,
onChangeCountSum,
+ onChangeTransferAmountSynced,
onChangeTransferType,
onChangeTrnType,
onClear,
onClickCalculator,
onClose,
onSubmit,
+ onTransferWalletSelected,
openFormForCreate,
openFormForDuplicate,
openFormForEdit,
openTrnFormModal,
shouldShowSum,
+ switchTransferWallets,
+ transferExpenseWalletId,
+ transferIncomeWalletId,
ui,
values,
}
diff --git a/app/components/wallets/Selector.vue b/app/components/wallets/Selector.vue
index d66f204a..92bc6c03 100644
--- a/app/components/wallets/Selector.vue
+++ b/app/components/wallets/Selector.vue
@@ -9,6 +9,7 @@ import { useWalletsStore } from '~/components/wallets/useWalletsStore'
const props = defineProps<{
activeItemId?: WalletId
+ disabledIds?: WalletId[]
hide?: () => void
selectedIds?: WalletId[]
}>()
@@ -23,7 +24,9 @@ const currencyFiltered = useStorage<CurrencyCode>(WALLET_STORAGE_KEYS.selectorCu
const selectedWalletsIdsWithCurrency = computed<WalletId[]>(() => {
return Object.keys(walletsStore.itemsComputed).filter((id) => {
const wallet = walletsStore.itemsComputed[id]
- return !wallet?.isArchived && (currencyFiltered.value === 'all' || currencyFiltered.value === wallet?.currency)
+ return !wallet?.isArchived
+ && !props.disabledIds?.includes(id)
+ && (currencyFiltered.value === 'all' || currencyFiltered.value === wallet?.currency)
})
})
diff --git a/i18n/locales/en-US.js b/i18n/locales/en-US.js
index e880b68a..ad785b4b 100644
--- a/i18n/locales/en-US.js
+++ b/i18n/locales/en-US.js
@@ -396,8 +396,10 @@ export default {
filterWalletAndCategory: 'Wallet & Category',
titleEditTrn: 'Edit transaction',
transfer: {
- expense: 'Transfer from',
- income: 'Transfer to',
+ expenseLabel: 'Transfer from',
+ expenseModal: 'Transfer from wallet',
+ incomeLabel: 'Transfer to',
+ incomeModal: 'Transfer to wallet',
},
transferTitle: 'Transfer',
wallet: {
diff --git a/i18n/locales/ru-RU.js b/i18n/locales/ru-RU.js
index d0c7ffbd..81640326 100644
--- a/i18n/locales/ru-RU.js
+++ b/i18n/locales/ru-RU.js
@@ -396,8 +396,10 @@ export default {
filterWalletAndCategory: 'Кошелек и Категория',
titleEditTrn: 'Редактирование транзакции',
transfer: {
- expense: 'Перевод из',
- income: 'Перевод в',
+ expenseLabel: 'Перевод из',
+ expenseModal: 'Перевод из кошелька',
+ incomeLabel: 'Перевод в',
+ incomeModal: 'Перевод в кошелёк',
},
transferTitle: 'Перевод',
wallet: {