Skip to content

Commit

Permalink
do not save transaction draft when category / account / tag is same a…
Browse files Browse the repository at this point in the history
…s the initial value (#37)
  • Loading branch information
mayswind committed Feb 5, 2025
1 parent 9797e7e commit b077b99
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
20 changes: 20 additions & 0 deletions src/lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ export function isYearMonthEquals(val1: unknown, val2: unknown): boolean {
return (!!parseInt(items1[0]) && !!parseInt(items1[1])) && (parseInt(items1[0]) === parseInt(items2[0])) && (parseInt(items1[1]) === parseInt(items2[1]));
}

export function isArray1SubsetOfArray2<T>(array1: T[], array2: T[]): boolean {
if (array1.length > array2.length) {
return false;
}

const array2ValuesMap: Map<T, boolean> = new Map<T, boolean>();

for (let i = 0; i < array2.length; i++) {
array2ValuesMap.set(array2[i], true);
}

for (let i = 0; i < array1.length; i++) {
if (!array2ValuesMap.get(array1[i])) {
return false;
}
}

return true;
}

export function isObjectEmpty(obj: object): boolean {
if (!obj) {
return true;
Expand Down
19 changes: 10 additions & 9 deletions src/stores/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
isDefined,
isNumber,
isString,
isArray1SubsetOfArray2,
splitItemsToMap,
countSplitItems
} from '@/lib/common.ts';
Expand Down Expand Up @@ -422,7 +423,7 @@ export const useTransactionsStore = defineStore('transactions', () => {
}
}

function isTransactionDraftModified(transaction?: Transaction): boolean {
function isTransactionDraftModified(transaction?: Transaction, initCategoryId?: string, initAccountId?: string, initTagIds?: string): boolean {
if (!transaction) {
return false;
}
Expand All @@ -435,11 +436,11 @@ export const useTransactionsStore = defineStore('transactions', () => {
return true;
}

if (transaction.sourceAccountId && transaction.sourceAccountId !== '0' && transaction.sourceAccountId !== userStore.currentUserDefaultAccountId) {
if (transaction.sourceAccountId && transaction.sourceAccountId !== '0' && transaction.sourceAccountId !== userStore.currentUserDefaultAccountId && transaction.sourceAccountId !== initAccountId) {
return true;
}

if (transaction.type === TransactionType.Transfer && transaction.destinationAccountId && transaction.destinationAccountId !== '0' && transaction.destinationAccountId !== userStore.currentUserDefaultAccountId) {
if (transaction.type === TransactionType.Transfer && transaction.destinationAccountId && transaction.destinationAccountId !== '0' && transaction.destinationAccountId !== userStore.currentUserDefaultAccountId && transaction.destinationAccountId !== initAccountId) {
return true;
}

Expand All @@ -449,19 +450,19 @@ export const useTransactionsStore = defineStore('transactions', () => {
if (transaction.type === TransactionType.Expense) {
const defaultCategoryId = getFirstAvailableCategoryId(allCategories[CategoryType.Expense]);

if (transaction.expenseCategoryId && transaction.expenseCategoryId !== '0' && transaction.expenseCategoryId !== defaultCategoryId) {
if (transaction.expenseCategoryId && transaction.expenseCategoryId !== '0' && transaction.expenseCategoryId !== defaultCategoryId && transaction.expenseCategoryId !== initCategoryId) {
return true;
}
} else if (transaction.type === TransactionType.Income) {
const defaultCategoryId = getFirstAvailableCategoryId(allCategories[CategoryType.Income]);

if (transaction.incomeCategoryId && transaction.incomeCategoryId !== '0' && transaction.incomeCategoryId !== defaultCategoryId) {
if (transaction.incomeCategoryId && transaction.incomeCategoryId !== '0' && transaction.incomeCategoryId !== defaultCategoryId && transaction.incomeCategoryId !== initCategoryId) {
return true;
}
} else if (transaction.type === TransactionType.Transfer) {
const defaultCategoryId = getFirstAvailableCategoryId(allCategories[CategoryType.Transfer]);

if (transaction.transferCategoryId && transaction.transferCategoryId !== '0' && transaction.transferCategoryId !== defaultCategoryId) {
if (transaction.transferCategoryId && transaction.transferCategoryId !== '0' && transaction.transferCategoryId !== defaultCategoryId && transaction.transferCategoryId !== initCategoryId) {
return true;
}
}
Expand All @@ -472,7 +473,7 @@ export const useTransactionsStore = defineStore('transactions', () => {
}

if (transaction.tagIds && transaction.tagIds.length > 0) {
return true;
return !initTagIds || !isArray1SubsetOfArray2(transaction.tagIds, initTagIds.split(','));
}

if (transaction.pictures && transaction.pictures.length > 0) {
Expand All @@ -486,14 +487,14 @@ export const useTransactionsStore = defineStore('transactions', () => {
return false;
}

function saveTransactionDraft(transaction?: Transaction): void {
function saveTransactionDraft(transaction?: Transaction, initCategoryId?: string, initAccountId?: string, initTagIds?: string): void {
if (settingsStore.appSettings.autoSaveTransactionDraft !== 'enabled' && settingsStore.appSettings.autoSaveTransactionDraft !== 'confirmation') {
clearTransactionDraft();
return;
}

if (transaction) {
if (!isTransactionDraftModified(transaction)) {
if (!isTransactionDraftModified(transaction, initCategoryId, initAccountId, initTagIds)) {
clearTransactionDraft();
return;
}
Expand Down
14 changes: 11 additions & 3 deletions src/views/desktop/transactions/list/dialogs/EditDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,10 @@ const geoMenuState = ref<boolean>(false);
const tagSearchContent = ref<string>('');
const removingPictureId = ref<string>('');
const initCategoryId = ref<string | undefined>(undefined);
const initAccountId = ref<string | undefined>(undefined);
const initTagIds = ref<string | undefined>(undefined);
let resolveFunc: ((response?: TransactionEditResponse) => void) | null = null;
let rejectFunc: ((reason?: unknown) => void) | null = null;
Expand Down Expand Up @@ -603,6 +607,10 @@ function open(options: TransactionEditOptions): Promise<TransactionEditResponse
geoLocationStatus.value = null;
originalTransactionEditable.value = false;
initCategoryId.value = options.categoryId;
initAccountId.value = options.accountId;
initTagIds.value = options.tagIds;
const newTransaction = createNewTransactionModel(options.type);
setTransaction(newTransaction, options, true, false);
Expand Down Expand Up @@ -903,9 +911,9 @@ function cancel(): void {
}
if (settingsStore.appSettings.autoSaveTransactionDraft === 'confirmation') {
if (transactionsStore.isTransactionDraftModified(transaction.value)) {
if (transactionsStore.isTransactionDraftModified(transaction.value, initCategoryId.value, initAccountId.value, initTagIds.value)) {
confirmDialog.value?.open('Do you want to save this transaction draft?').then(() => {
transactionsStore.saveTransactionDraft(transaction.value);
transactionsStore.saveTransactionDraft(transaction.value, initCategoryId.value, initAccountId.value, initTagIds.value);
doClose();
}).catch(() => {
transactionsStore.clearTransactionDraft();
Expand All @@ -916,7 +924,7 @@ function cancel(): void {
doClose();
}
} else if (settingsStore.appSettings.autoSaveTransactionDraft === 'enabled') {
transactionsStore.saveTransactionDraft(transaction.value);
transactionsStore.saveTransactionDraft(transaction.value, initCategoryId.value, initAccountId.value, initTagIds.value);
doClose();
} else {
doClose();
Expand Down
6 changes: 3 additions & 3 deletions src/views/mobile/transactions/EditPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1127,17 +1127,17 @@ function onPageBeforeOut(): void {
}
if (settingsStore.appSettings.autoSaveTransactionDraft === 'confirmation') {
if (transactionsStore.isTransactionDraftModified(transaction.value)) {
if (transactionsStore.isTransactionDraftModified(transaction.value, query['categoryId'], query['accountId'], query['tagIds'])) {
showConfirm('Do you want to save this transaction draft?', () => {
transactionsStore.saveTransactionDraft(transaction.value);
transactionsStore.saveTransactionDraft(transaction.value, query['categoryId'], query['accountId'], query['tagIds']);
}, () => {
transactionsStore.clearTransactionDraft();
});
} else {
transactionsStore.clearTransactionDraft();
}
} else if (settingsStore.appSettings.autoSaveTransactionDraft === 'enabled') {
transactionsStore.saveTransactionDraft(transaction.value);
transactionsStore.saveTransactionDraft(transaction.value, query['categoryId'], query['accountId'], query['tagIds']);
}
}
Expand Down

0 comments on commit b077b99

Please sign in to comment.