From a8824c360d40da20f1d930d1d7f08f77a8f77599 Mon Sep 17 00:00:00 2001 From: Tilman Liero Date: Sat, 9 Jul 2016 14:20:37 +0200 Subject: [PATCH] show changes after modal has been closed --- .../SpringDataRestEventHandler.java | 2 +- src/main/resources/static/js/controllers.js | 102 +++++++++++------- .../static/partials/transaction-list.html | 8 +- 3 files changed, 65 insertions(+), 47 deletions(-) diff --git a/src/main/java/de/tilman/transactions/repository/SpringDataRestEventHandler.java b/src/main/java/de/tilman/transactions/repository/SpringDataRestEventHandler.java index 5eeefa6..17a3f4a 100644 --- a/src/main/java/de/tilman/transactions/repository/SpringDataRestEventHandler.java +++ b/src/main/java/de/tilman/transactions/repository/SpringDataRestEventHandler.java @@ -22,7 +22,7 @@ public class SpringDataRestEventHandler { @HandleBeforeSave public void applyUserInformationUsingSecurityContext(Transaction transaction) { Date timestamp = new Date(); - log.info("Setting date '" + timestamp + "' on new transaction: " + transaction.getDescription()); + log.info("Setting date '" + timestamp + "' on transaction: " + transaction.getId() + "(" + transaction.getDescription() + ")"); transaction.setLastChange(timestamp); } } \ No newline at end of file diff --git a/src/main/resources/static/js/controllers.js b/src/main/resources/static/js/controllers.js index 3ef9d20..e6b1a4b 100644 --- a/src/main/resources/static/js/controllers.js +++ b/src/main/resources/static/js/controllers.js @@ -1,7 +1,6 @@ var transactionControllers = angular.module('transactionControllers', ['ngResource']); function amountToNumber(amount) { - console.log("typeof amount: " + (typeof amount)); amount = amount.toString().replace(',', '.'); if (amount.charAt(0) !== '+' && amount.charAt(0) !== '-') { amount = '-' + amount; @@ -10,36 +9,8 @@ function amountToNumber(amount) { } transactionControllers.controller('TransactionListCtrl', [ '$scope', '$location', '$resource', '$q', '$uibModal', '$log', 'Transaction', function($scope, $location, $resource, $q, $uibModal, $log, Transaction) { - - $scope.refreshList = function() { - - // reset form - if ($scope.form) { - $scope.form.$setPristine(); - $scope.form.$setUntouched(); - } - - $scope.transaction = { - amount: undefined, - date: new Date(), - creditor: "" - }; - - if (typeof $scope.categories !== 'undefined') { - $scope.transaction.category = $scope.categories[0]; - } - - // get recent transactions - $log.info('get transactions:'); - var transactions = $resource('http://localhost:8080/transactions/search/forAccount', { accountId: $scope.account.id, size: $scope.pageSize, sort: 'date,desc' }).get(); - transactions.$promise.then(function(data) { - $log.info(transactions._embedded.transactions); - $scope.transactions = transactions._embedded.transactions; - $scope.nextPageAvailable = (transactions.page.totalPages !== transactions.page.number + 1); - $scope.prevPageAvailable = (transactions.page.number > 0); - $scope.pageNumber = transactions.page.number; - }); - + + function refreshExpenses() { var accountUsers = $resource($scope.account._links.users.href).get(); accountUsers.$promise.then(function(data) { $scope.accountUsers = accountUsers._embedded.users; @@ -87,6 +58,36 @@ transactionControllers.controller('TransactionListCtrl', [ '$scope', '$location' }); } + $scope.refreshList = function() { + + // reset form + if ($scope.form) { + $scope.form.$setPristine(); + $scope.form.$setUntouched(); + } + + $scope.transaction = { + amount: undefined, + date: new Date(), + creditor: "" + }; + + if (typeof $scope.categories !== 'undefined') { + $scope.transaction.category = $scope.categories[0]; + } + + // get recent transactions + var transactions = $resource('http://localhost:8080/transactions/search/forAccount', { accountId: $scope.account.id, size: $scope.pageSize, sort: 'date,desc' }).get(); + transactions.$promise.then(function(data) { + $scope.transactions = transactions._embedded.transactions; + $scope.nextPageAvailable = (transactions.page.totalPages !== transactions.page.number + 1); + $scope.prevPageAvailable = (transactions.page.number > 0); + $scope.pageNumber = transactions.page.number; + }); + + refreshExpenses(); + } + $scope.checkCategory = function() { var radioButtons = document.getElementsByName('expenseBy'); @@ -217,12 +218,12 @@ transactionControllers.controller('TransactionListCtrl', [ '$scope', '$location' }); }; - $scope.openDetails = function(transactionId) { + $scope.openDetails = function(transaction) { var modalInstance = $uibModal.open({ templateUrl: 'detailsTemplate.html', controller: 'DetailsModalCtrl', resolve: { - transactionId: transactionId, + transactionId: transaction.id, categories: function () { return $scope.categories; }, @@ -232,9 +233,29 @@ transactionControllers.controller('TransactionListCtrl', [ '$scope', '$location' } }); - modalInstance.result.then(function(transaction) { + modalInstance.result.then(function(editedTransaction) { + $scope.lastSubmission = undefined; - $scope.refreshList(); + + if (editedTransaction.amount != undefined) { + transaction.amount = editedTransaction.amount; + transaction.date = editedTransaction.date; + transaction.description = editedTransaction.description; + + transaction.creditor = $resource(editedTransaction.creditor).get(); + transaction.category = $resource(editedTransaction.category).get(); + } + else { + // if the transaction has been deleted, only the ID is returned + for (i = 0; i < $scope.transactions.length; i++) { + if ($scope.transactions[i].id == editedTransaction) { + $scope.transactions.splice(i, 1); + } + } + } + + refreshExpenses(); + }, function () { $log.info('Modal dismissed at: ' + new Date()); }); @@ -277,12 +298,11 @@ transactionControllers.controller('DetailsModalCtrl', [ '$scope', '$resource', ' }); $scope.ok = function () { - // set URI for category and save via PUT request $scope.transaction.category = $scope.transaction.category._links.self.href; $scope.transaction.amount = amountToNumber($scope.transaction.amount); - $scope.transaction.$update(function() {}); - - $uibModalInstance.close($scope.transaction); + $scope.transaction.$update(function(data) {}).then( + $uibModalInstance.close($scope.transaction) + ); }; $scope.cancel = function () { @@ -290,9 +310,9 @@ transactionControllers.controller('DetailsModalCtrl', [ '$scope', '$resource', ' }; $scope.deleteTransaction = function() { - // TODO auf return code reagieren + var deletedTransactionId = $scope.transaction.id; $resource($scope.transaction._links.self.href).remove().$promise.then(function() { - $uibModalInstance.close(); + $uibModalInstance.close(deletedTransactionId); }); } } ]); diff --git a/src/main/resources/static/partials/transaction-list.html b/src/main/resources/static/partials/transaction-list.html index eb660e8..1b37544 100644 --- a/src/main/resources/static/partials/transaction-list.html +++ b/src/main/resources/static/partials/transaction-list.html @@ -61,8 +61,7 @@ @@ -89,7 +88,7 @@ Betrag Beschreibung - + {{transaction.date | date:'dd.MM.yyyy'}} {{transaction.category.name}} {{transaction.amount | number : 2}} € @@ -151,8 +150,7 @@