Refactor new user creation

This commit is contained in:
Matthew Dillon 2015-11-10 09:15:28 -07:00
parent 367341c780
commit ec597cd419
7 changed files with 130 additions and 66 deletions

View file

@ -0,0 +1,66 @@
import Ember from 'ember';
const { Component } = Ember;
export default Component.extend({
// Read-only attributes
user: null,
isLoading: null,
// Actions
"on-save": null,
"on-cancel": null,
// Property mapping
propertiesList: ['name', 'email', 'password', 'passwordConfirm'],
name: null,
email: null,
password: null,
passwordConfirm: null,
resetOnInit: Ember.on('init', function() {
this.get('propertiesList').forEach((field) => {
const valueInUser = this.get('user').get(field);
this.set(field, valueInUser);
});
}),
updateField: function(property, value) {
this.set(property, value);
// Manually compare against passed in value
if (this.get('user').get(property) !== value) {
this.set('isDirty', true);
} else {
this.set('isDirty', false);
}
},
actions: {
save: function() {
// All validation is server-side, except for password verification matching
if (this.get('password') !== this.get('passwordConfirm')) {
this.get('flashMessages').clearMessages();
this.get('flashMessages').error("Password fields don't match");
return;
}
return this.attrs['on-save'](this.getProperties(this.get('propertiesList')));
},
nameDidChange: function(value) {
this.updateField('name', value);
},
emailDidChange: function(value) {
this.updateField('email', value);
},
passwordDidChange: function(value) {
this.updateField('password', value);
},
passwordConfirmDidChange: function(value) {
this.updateField('passwordConfirm', value);
}
}
});

View file

@ -0,0 +1,36 @@
{{#if isLoading}}
{{loading-panel}}
{{else}}
<div class="grid-1">
<div class="span-1">
<fieldset>
<legend>New User Signup</legend>
<form {{action 'save' on='submit'}}>
<ul>
<li>
<label>Name</label>
{{one-way-input type="text" class="user-name" value=name update=(action "nameDidChange")}}
</li>
<li>
<label>Email</label>
{{one-way-input type="text" class="email" value=email update=(action "emailDidChange")}}
</li>
<li>
<label>Password</label>
{{one-way-input type="password" class="password" value=password update=(action "passwordDidChange")}}
</li>
<li>
<label>Password (confirm)</label>
{{one-way-input type="password" class="password-verify" value=passwordConfirm update=(action "passwordConfirmDidChange")}}
</li>
<li>
<button type="submit" class="button-green smaller save-user">
Submit
</button>
</li>
</ul>
</form>
</fieldset>
</div>
</div>
{{/if}}