From 5795c828e2cc4fa3b0cb64173c51150270e4b0ce Mon Sep 17 00:00:00 2001 From: zinyando Date: Mon, 25 Jul 2016 12:05:46 +0200 Subject: [PATCH] User profile refactor --- app/app/components/user-profile.js | 96 +++++++++++++++++++ app/app/pods/profile/controller.js | 57 ++++------- app/app/pods/profile/template.hbs | 28 +----- app/app/templates/components/user-profile.hbs | 27 ++++++ 4 files changed, 140 insertions(+), 68 deletions(-) create mode 100644 app/app/components/user-profile.js create mode 100644 app/app/templates/components/user-profile.hbs diff --git a/app/app/components/user-profile.js b/app/app/components/user-profile.js new file mode 100644 index 00000000..7c0f503b --- /dev/null +++ b/app/app/components/user-profile.js @@ -0,0 +1,96 @@ +import Ember from 'ember'; + +const { + computed, + isEmpty, + isEqual, + isPresent +} = Ember; + +export default Ember.Component.extend({ + password: { password: "", confirmation: "" }, + hasFirstnameError: computed('model.firstname', { + get() { + if (isEmpty(this.get('model.firstname'))) { + return `error`; + } + + return; + } + }), + hasLastnameError: computed('model.lastname', { + get() { + if (isEmpty(this.get('model.lastname'))) { + return `error`; + } + + return; + } + }), + hasEmailError: computed('model.email', { + get() { + if (isEmpty(this.get('model.email'))) { + return `error`; + } + + return; + } + }), + hasPasswordError: computed('passwordError', 'password.password', { + get() { + if (isPresent(this.get('passwordError'))) { + return `error`; + } + + if (isEmpty(this.get('password.password'))) { + return null; + } + } + }), + hasConfirmPasswordError: computed('confirmPasswordError', { + get() { + if (isPresent(this.get("confirmPasswordError"))) { + return `error`; + } + + return; + } + }), + + actions: { + save() { + let password = this.get('password.password'); + let confirmation = this.get('password.confirmation'); + + if (isEmpty(this.model.get('firstname'))) { + return $("#firstname").focus(); + } + if (isEmpty(this.model.get('lastname'))) { + return $("#lastname").focus(); + } + if (isEmpty(this.model.get('email'))) { + return $("#email").focus(); + } + + if (isPresent(password) && isEmpty(confirmation)) { + Ember.set(this, 'confirmPasswordError', 'error'); + return $("#confirmPassword").focus(); + } + if (isEmpty(password) && isPresent(confirmation)) { + Ember.set(this, 'passwordError', 'error'); + return $("#password").focus(); + } + if (!isEqual(password, confirmation)) { + Ember.set(this, 'passwordError', 'error'); + return $("#password").focus(); + } + + let passwords = this.get('password'); + + this.get('save')(passwords).finally(() => { + Ember.set(this, 'password.password', ''); + Ember.set(this, 'password.confirmation', ''); + }); + } + } +}); diff --git a/app/app/pods/profile/controller.js b/app/app/pods/profile/controller.js index 431dc2b8..fb4d2edd 100644 --- a/app/app/pods/profile/controller.js +++ b/app/app/pods/profile/controller.js @@ -1,62 +1,37 @@ // Copyright 2016 Documize Inc. . All rights reserved. // -// This software (Documize Community Edition) is licensed under +// This software (Documize Community Edition) is licensed under // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html // // You can operate outside the AGPL restrictions by purchasing // Documize Enterprise Edition and obtaining a commercial license -// by contacting . +// by contacting . // // https://documize.com import Ember from 'ember'; +const { + isPresent +} = Ember; + export default Ember.Controller.extend({ userService: Ember.inject.service('user'), - password: { password: "", confirmation: "" }, session: Ember.inject.service(), actions: { - save: function () { - if (is.empty(this.model.get('firstname'))) { - $("#firstname").addClass("error").focus(); - return; - } - if (is.empty(this.model.get('lastname'))) { - $("#lastname").addClass("error").focus(); - return; - } - if (is.empty(this.model.get('email'))) { - $("#email").addClass("error").focus(); - return; - } - if (is.not.empty(this.password.password) && is.empty(this.password.confirmation)) { - $("#confirmPassword").addClass("error").focus(); - return; - } - if (is.empty(this.password.password) && is.not.empty(this.password.confirmation)) { - $("#password").addClass("error").focus(); - return; - } - if (is.not.equal(this.password.password, this.password.confirmation)) { - $("#password").addClass("error").focus(); - return; - } + save(passwords) { + let password = passwords.password; + let confirmation = passwords.confirmation; - let self = this; - - this.get('userService').save(this.model).then(function () { - if (is.not.empty(self.password.password) && is.not.empty(self.password.confirmation)) { - self.get('userService').updatePassword(self.model.get('id'), self.password.password).then(function () { - self.password.password = ""; - self.password.confirmation = ""; - }); + return this.get('userService').save(this.model).then(() => { + if (isPresent(password) && isPresent(confirmation)) { + this.get('userService').updatePassword(this.get('model.id'), password); } - self.model.generateInitials(); - self.get('session').set('user', self.model); + this.model.generateInitials(); + this.get('session').set('user', this.model); + this.transitionToRoute('folders'); }); - - this.transitionToRoute('folders'); } } -}); \ No newline at end of file +}); diff --git a/app/app/pods/profile/template.hbs b/app/app/pods/profile/template.hbs index 69cc6f6d..f01bedae 100644 --- a/app/app/pods/profile/template.hbs +++ b/app/app/pods/profile/template.hbs @@ -13,33 +13,7 @@ {{/layout/zone-sidebar}} {{#layout/zone-content}} -
-
-
- - {{focus-input id="firstname" type="text" value=model.firstname}} -
-
- - {{input id="lastname" type="text" value=model.lastname}} -
-
- - {{input id="email" type="text" value=model.email}} -
-
- -
Optional change your password
- {{input id="password" type="password" value=password.password}} -
-
- -
Confirm your new password
- {{input id="confirmPassword" type="password" value=password.confirmation}} -
-
save
-
-
+ {{user-profile model=model save=(action 'save')}} {{/layout/zone-content}} {{/layout/zone-container}} diff --git a/app/app/templates/components/user-profile.hbs b/app/app/templates/components/user-profile.hbs new file mode 100644 index 00000000..992be356 --- /dev/null +++ b/app/app/templates/components/user-profile.hbs @@ -0,0 +1,27 @@ +
+
+
+ + {{focus-input id="firstname" type="text" value=model.firstname class=hasFirstnameError}} +
+
+ + {{input id="lastname" type="text" value=model.lastname class=hasLastnameError}} +
+
+ + {{input id="email" type="text" value=model.email class=hasEmailError}} +
+
+ +
Optional change your password
+ {{input id="password" type="password" value=password.password class=hasPasswordError}} +
+
+ +
Confirm your new password
+ {{input id="confirmPassword" type="password" value=password.confirmation class=hasConfirmPasswordError}} +
+
save
+
+