transaction edit dialog supports duplicate transaction, edit transaction and save new transaction

This commit is contained in:
MaysWind
2023-08-21 00:56:20 +08:00
parent c28158b041
commit 2a16260b05
4 changed files with 136 additions and 50 deletions
+39 -7
View File
@@ -15,6 +15,8 @@ import { isNumber, isString } from '@/lib/common.js';
import { import {
getCurrentUnixTime, getCurrentUnixTime,
getTimezoneOffsetMinutes, getTimezoneOffsetMinutes,
getBrowserTimezoneOffsetMinutes,
getActualUnixTimeForStore,
parseDateFromUnixTime, parseDateFromUnixTime,
getShortDate, getShortDate,
getYear, getYear,
@@ -651,25 +653,55 @@ export const useTransactionsStore = defineStore('transactions', {
}); });
}); });
}, },
saveTransaction({ transaction, defaultCurrency }) { saveTransaction({ transaction, defaultCurrency, isEdit }) {
const self = this; const self = this;
const settingsStore = useSettingsStore(); const settingsStore = useSettingsStore();
const exchangeRatesStore = useExchangeRatesStore(); const exchangeRatesStore = useExchangeRatesStore();
const submitTransaction = {
type: transaction.type,
time: getActualUnixTimeForStore(transaction.time, transaction.utcOffset, getBrowserTimezoneOffsetMinutes()),
sourceAccountId: transaction.sourceAccountId,
sourceAmount: transaction.sourceAmount,
destinationAccountId: '0',
destinationAmount: 0,
hideAmount: transaction.hideAmount,
tagIds: transaction.tagIds,
comment: transaction.comment,
geoLocation: transaction.geoLocation,
utcOffset: transaction.utcOffset
};
if (transaction.type === transactionConstants.allTransactionTypes.Expense) {
submitTransaction.categoryId = transaction.expenseCategory;
} else if (transaction.type === transactionConstants.allTransactionTypes.Income) {
submitTransaction.categoryId = transaction.incomeCategory;
} else if (transaction.type === transactionConstants.allTransactionTypes.Transfer) {
submitTransaction.categoryId = transaction.transferCategory;
submitTransaction.destinationAccountId = transaction.destinationAccountId;
submitTransaction.destinationAmount = transaction.destinationAmount;
} else {
return Promise.reject('An error has occurred');
}
if (isEdit) {
submitTransaction.id = transaction.id;
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let promise = null; let promise = null;
if (!transaction.id) { if (!submitTransaction.id) {
promise = services.addTransaction(transaction); promise = services.addTransaction(submitTransaction);
} else { } else {
promise = services.modifyTransaction(transaction); promise = services.modifyTransaction(submitTransaction);
} }
promise.then(response => { promise.then(response => {
const data = response.data; const data = response.data;
if (!data || !data.success || !data.result) { if (!data || !data.success || !data.result) {
if (!transaction.id) { if (!submitTransaction.id) {
reject({ message: 'Unable to add transaction' }); reject({ message: 'Unable to add transaction' });
} else { } else {
reject({ message: 'Unable to save transaction' }); reject({ message: 'Unable to save transaction' });
@@ -677,7 +709,7 @@ export const useTransactionsStore = defineStore('transactions', {
return; return;
} }
if (!transaction.id) { if (!submitTransaction.id) {
if (!self.transactionListStateInvalid) { if (!self.transactionListStateInvalid) {
self.updateTransactionListInvalidState(true); self.updateTransactionListInvalidState(true);
} }
@@ -710,7 +742,7 @@ export const useTransactionsStore = defineStore('transactions', {
if (error.response && error.response.data && error.response.data.errorMessage) { if (error.response && error.response.data && error.response.data.errorMessage) {
reject({ error: error.response.data }); reject({ error: error.response.data });
} else if (!error.processed) { } else if (!error.processed) {
if (!transaction.id) { if (!submitTransaction.id) {
reject({ message: 'Unable to add transaction' }); reject({ message: 'Unable to add transaction' });
} else { } else {
reject({ message: 'Unable to save transaction' }); reject({ message: 'Unable to save transaction' });
@@ -839,6 +839,8 @@ export default {
if (result && result.message) { if (result && result.message) {
self.$refs.snackbar.showMessage(result.message); self.$refs.snackbar.showMessage(result.message);
} }
self.reload(false);
}).catch(error => { }).catch(error => {
if (error) { if (error) {
self.$refs.snackbar.showError(error); self.$refs.snackbar.showError(error);
@@ -855,6 +857,8 @@ export default {
if (result && result.message) { if (result && result.message) {
self.$refs.snackbar.showMessage(result.message); self.$refs.snackbar.showMessage(result.message);
} }
self.reload(false);
}).catch(error => { }).catch(error => {
if (error) { if (error) {
self.$refs.snackbar.showError(error); self.$refs.snackbar.showError(error);
@@ -278,8 +278,17 @@
{{ $t(saveButtonTitle) }} {{ $t(saveButtonTitle) }}
<v-progress-circular indeterminate size="24" class="ml-2" v-if="submitting"></v-progress-circular> <v-progress-circular indeterminate size="24" class="ml-2" v-if="submitting"></v-progress-circular>
</v-btn> </v-btn>
<v-btn color="secondary" variant="tonal" <v-btn variant="tonal" :disabled="loading || submitting"
:disabled="loading || submitting" @click="cancel">{{ $t(cancelButtonTitle) }}</v-btn> v-if="mode === 'view'" @click="duplicate">{{ $t('Duplicate') }}</v-btn>
<v-btn color="warning" variant="tonal" :disabled="loading || submitting"
v-if="mode === 'view'" @click="edit">{{ $t('Edit') }}</v-btn>
<v-btn color="error" variant="tonal" :disabled="loading || submitting"
v-if="mode === 'view'" @click="remove">
{{ $t('Delete') }}
<v-progress-circular indeterminate size="24" class="ml-2" v-if="submitting"></v-progress-circular>
</v-btn>
<v-btn color="secondary" variant="tonal" :disabled="loading || submitting"
@click="cancel">{{ $t(cancelButtonTitle) }}</v-btn>
</div> </div>
</v-card-text> </v-card-text>
</v-card> </v-card>
@@ -303,10 +312,9 @@ import categoryConstants from '@/consts/category.js';
import transactionConstants from '@/consts/transaction.js'; import transactionConstants from '@/consts/transaction.js';
import logger from '@/lib/logger.js'; import logger from '@/lib/logger.js';
import { import {
getNameByKeyValue getUtcOffsetByUtcOffsetMinutes,
} from '@/lib/common.js'; getTimezoneOffsetMinutes,
import { getCurrentUnixTime
getUtcOffsetByUtcOffsetMinutes
} from '@/lib/datetime.js'; } from '@/lib/datetime.js';
import { import {
getFirstAvailableCategoryId getFirstAvailableCategoryId
@@ -619,7 +627,82 @@ export default {
return; return;
} }
const doSubmit = function () {
self.submitting = true;
self.transactionsStore.saveTransaction({
transaction: self.transaction,
defaultCurrency: self.defaultCurrency,
isEdit: self.mode === 'edit'
}).then(() => {
self.submitting = false;
if (self.resolve) {
if (self.mode === 'add') {
self.resolve({
message: 'You have added a new transaction'
});
} else if (self.mode === 'edit') {
self.resolve({
message: 'You have saved this transaction'
});
}
}
self.showState = false;
}).catch(error => {
self.submitting = false;
if (!error.processed) {
self.$refs.snackbar.showError(error);
}
});
};
if (self.transaction.sourceAmount === 0) {
self.$refs.confirmDialog.open('Are you sure you want to save this transaction whose amount is 0?').then(() => {
doSubmit();
});
} else {
doSubmit();
}
},
duplicate() {
this.editTransactionId = null;
this.transaction.id = null;
this.transaction.time = getCurrentUnixTime();
this.transaction.timeZone = this.settingsStore.appSettings.timeZone;
this.transaction.utcOffset = getTimezoneOffsetMinutes(this.transaction.timeZone);
this.transaction.geoLocation = null;
this.mode = 'add';
},
edit() {
this.mode = 'edit';
},
remove() {
const self = this;
self.$refs.confirmDialog.open('Are you sure you want to delete this transaction?').then(() => {
self.submitting = true;
self.transactionsStore.deleteTransaction({
transaction: self.transaction,
defaultCurrency: self.defaultCurrency
}).then(() => {
if (self.resolve) {
self.resolve();
}
self.submitting = false;
self.showState = false;
}).catch(error => {
self.submitting = false;
if (!error.processed) {
self.$refs.snackbar.showError(error);
}
});
});
}, },
cancel() { cancel() {
if (this.reject) { if (this.reject) {
@@ -676,9 +759,6 @@ export default {
this.geoLocationStatus = null; this.geoLocationStatus = null;
this.transaction.geoLocation = null; this.transaction.geoLocation = null;
}, },
getAccountNameByKeyValue(src, value, keyField, nameField, defaultName) {
return getNameByKeyValue(this.allAccounts, src, value, keyField, nameField, defaultName);
},
setTransaction(transaction, options, setContextData) { setTransaction(transaction, options, setContextData) {
setTransactionModelByTransaction( setTransactionModelByTransaction(
this.transaction, this.transaction,
+4 -34
View File
@@ -674,44 +674,14 @@ export default {
return; return;
} }
const submitTransaction = {
type: self.transaction.type,
time: getActualUnixTimeForStore(self.transaction.time, self.transaction.utcOffset, getBrowserTimezoneOffsetMinutes()),
sourceAccountId: self.transaction.sourceAccountId,
sourceAmount: self.transaction.sourceAmount,
destinationAccountId: '0',
destinationAmount: 0,
hideAmount: self.transaction.hideAmount,
tagIds: self.transaction.tagIds,
comment: self.transaction.comment,
geoLocation: self.transaction.geoLocation,
utcOffset: self.transaction.utcOffset
};
if (self.transaction.type === self.allTransactionTypes.Expense) {
submitTransaction.categoryId = self.transaction.expenseCategory;
} else if (self.transaction.type === self.allTransactionTypes.Income) {
submitTransaction.categoryId = self.transaction.incomeCategory;
} else if (self.transaction.type === self.allTransactionTypes.Transfer) {
submitTransaction.categoryId = self.transaction.transferCategory;
submitTransaction.destinationAccountId = self.transaction.destinationAccountId;
submitTransaction.destinationAmount = self.transaction.destinationAmount;
} else {
self.$toast('An error has occurred');
return;
}
if (self.mode === 'edit') {
submitTransaction.id = self.transaction.id;
}
const doSubmit = function () { const doSubmit = function () {
self.submitting = true; self.submitting = true;
self.$showLoading(() => self.submitting); self.$showLoading(() => self.submitting);
self.transactionsStore.saveTransaction({ self.transactionsStore.saveTransaction({
transaction: submitTransaction, transaction: self.transaction,
defaultCurrency: self.defaultCurrency defaultCurrency: self.defaultCurrency,
isEdit: self.mode === 'edit'
}).then(() => { }).then(() => {
self.submitting = false; self.submitting = false;
self.$hideLoading(); self.$hideLoading();
@@ -733,7 +703,7 @@ export default {
}); });
}; };
if (submitTransaction.sourceAmount === 0) { if (self.transaction.sourceAmount === 0) {
self.$confirm('Are you sure you want to save this transaction whose amount is 0?', () => { self.$confirm('Are you sure you want to save this transaction whose amount is 0?', () => {
doSubmit(); doSubmit();
}); });