From a045fd77d7685a6b648589c141c97e7e77387e6c Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Fri, 15 Jul 2016 16:40:39 +0200 Subject: [PATCH] Improve forms. --- accountant-ui/index.html | 1 + accountant-ui/js/accounts.js | 56 +++++++++++++++----- accountant-ui/js/operations.js | 3 +- accountant-ui/js/scheduler.js | 1 + accountant-ui/views/account.form.tmpl.html | 35 ++++++++---- accountant-ui/views/accounts.html | 5 +- accountant-ui/views/operation.form.tmpl.html | 25 +++++---- bower.json | 3 +- 8 files changed, 90 insertions(+), 39 deletions(-) diff --git a/accountant-ui/index.html b/accountant-ui/index.html index 0c36c2b..e4bc302 100644 --- a/accountant-ui/index.html +++ b/accountant-ui/index.html @@ -66,6 +66,7 @@ + diff --git a/accountant-ui/js/accounts.js b/accountant-ui/js/accounts.js index 3a622a8..8ecc3cd 100644 --- a/accountant-ui/js/accounts.js +++ b/accountant-ui/js/accounts.js @@ -19,8 +19,8 @@ angular.module('accountant.accounts', [ 'ngResource', + 'ngMessages', 'ui-notification', - 'xeditable', 'ngBootbox' ]) @@ -160,7 +160,7 @@ angular.module('accountant.accounts', [ }]) .directive( - 'accountFormDialog', function($ngBootbox) { + 'accountFormDialog', function($ngBootbox, Notification) { return { restrict: 'A', scope: { @@ -175,8 +175,48 @@ angular.module('accountant.accounts', [ scope.form = {}; + scope.submitForm = function() { + // check to make sure the form is completely valid + if (!scope.form.$valid) { + return false; + } + + // Authorized overdraft is a positive integer but data is a negative integer. + scope.data.authorized_overdraft = -scope.data.authorized_overdraft + + angular.copy(scope.data, scope.account); + + // Save account + console.log(scope.account); + return scope.account.$save().then( + function(data) { + Notification.success('Account #' + data.id + ' saved.'); + + scope.account.getSolds(); + + return data; + }, function(data) { + Notification.error('Error while saving account #' + data.id); + scope.account.getSolds(); + console.log(data); + return false; + } + ); + }; + element.on('click', function() { - //angular.copy(scope.account, scope.form); + // Create new account if not passed in ng-model. + if(!scope.account) { + scope.account = new Account({ + authorized_overdraft: 0 + }); + } + + scope.data = {}; + angular.copy(scope.account, scope.data); + + // Authorized overdraft must be positive in form. + scope.data.authorized_overdraft = -scope.data.authorized_overdraft // Open dialog with form. $ngBootbox.customDialog({ @@ -188,15 +228,7 @@ angular.module('accountant.accounts', [ save: { label: 'Save', className: 'btn-success', - callback: function() { - // Validate form - console.log(scope.form); - - // Save account - console.log(scope.account); - - return false; - } + callback: scope.submitForm }, cancel: { label: 'Cancel', diff --git a/accountant-ui/js/operations.js b/accountant-ui/js/operations.js index 5e2722d..66304ed 100644 --- a/accountant-ui/js/operations.js +++ b/accountant-ui/js/operations.js @@ -482,7 +482,8 @@ angular.module('accountant.operations', [ scope.form = {}; element.on('click', function() { - //angular.copy(scope.operation, scope.form); + scope.data = {}; + angular.copy(scope.operation, scope.data); // Open dialog with form. $ngBootbox.customDialog({ diff --git a/accountant-ui/js/scheduler.js b/accountant-ui/js/scheduler.js index 510dd92..75aed46 100644 --- a/accountant-ui/js/scheduler.js +++ b/accountant-ui/js/scheduler.js @@ -21,6 +21,7 @@ angular.module('accountant.scheduler', [ 'ngRoute', 'ngBootbox', 'ui-notification', + 'xeditable', 'mgcrea.ngStrap' ]) diff --git a/accountant-ui/views/account.form.tmpl.html b/accountant-ui/views/account.form.tmpl.html index c3e0ec6..7a33eea 100644 --- a/accountant-ui/views/account.form.tmpl.html +++ b/accountant-ui/views/account.form.tmpl.html @@ -15,24 +15,39 @@ along with Accountant. If not, see . --> -
-
+ +
- + +
+

The account name is required.

+
-
+
- - +
+
-
+ + +
.00€
+
+
+

The authorized overdraft is required.

+

The authorized overdraft must be equal or greater than 0.

+
diff --git a/accountant-ui/views/accounts.html b/accountant-ui/views/accounts.html index 6d25b74..e0a0b83 100644 --- a/accountant-ui/views/accounts.html +++ b/accountant-ui/views/accounts.html @@ -30,8 +30,9 @@ - + diff --git a/accountant-ui/views/operation.form.tmpl.html b/accountant-ui/views/operation.form.tmpl.html index a9cb4d0..a898317 100644 --- a/accountant-ui/views/operation.form.tmpl.html +++ b/accountant-ui/views/operation.form.tmpl.html @@ -15,13 +15,13 @@ along with Accountant. If not, see . --> +
-
@@ -29,9 +29,9 @@
- +
@@ -39,9 +39,9 @@
- +
@@ -49,11 +49,10 @@
- +
- diff --git a/bower.json b/bower.json index 44eb582..326879b 100644 --- a/bower.json +++ b/bower.json @@ -18,12 +18,13 @@ ], "dependencies": { "jquery": "~2.2", - "moment": "~2.12", + "moment": "~2.14", "bootstrap": "~3.3.6", "bootstrap-additions": "~0.3.1", "angular": "~1.5", "angular-resource": "~1.5", "angular-route": "~1.5", + "angular-messages": "~1.5", "angular-strap": "~2.3.6", "angular-xeditable": "~0.1", "angular-ui-notification": "~0.2",