diff --git a/app/pods/application/route.js b/app/pods/application/route.js index 2f1869b..ef5b286 100644 --- a/app/pods/application/route.js +++ b/app/pods/application/route.js @@ -6,5 +6,11 @@ export default Ember.Route.extend(ApplicationRouteMixin, { invalidateSession: function() { this.get('session').invalidate(); }, + + didTransition: function() { + this.get('flashMessages').clearMessages(); + return true; + }, + } }); diff --git a/app/pods/login/controller.js b/app/pods/login/controller.js index 1282885..144d2aa 100644 --- a/app/pods/login/controller.js +++ b/app/pods/login/controller.js @@ -7,6 +7,7 @@ export default Ember.Controller.extend({ let authenticator = 'simple-auth-authenticator:token'; this.set('loading', true); + // Manually clean up because there might not be a transition this.get('flashMessages').clearMessages(); this.get('session').authenticate(authenticator, credentials).then(()=>{ this.set('loading', false); diff --git a/app/pods/user/adapter.js b/app/pods/user/adapter.js index 9e430e9..6d9c191 100644 --- a/app/pods/user/adapter.js +++ b/app/pods/user/adapter.js @@ -1,3 +1,4 @@ +import Ember from 'ember'; import DS from 'ember-data'; export default DS.RESTAdapter.extend({ @@ -6,4 +7,21 @@ export default DS.RESTAdapter.extend({ return this.get('globals.apiURL'); }.property(), coalesceFindRequests: true, + ajaxError: function(jqXHR) { + // http://stackoverflow.com/a/24027443 + var error = this._super(jqXHR); + if (jqXHR && jqXHR.status === 422) { + var response = Ember.$.parseJSON(jqXHR.responseText), + errors = {}; + if (response.errors !== undefined) { + var jsonErrors = response.errors; + Ember.EnumerableUtils.forEach(Ember.keys(jsonErrors), function(key) { + errors[Ember.String.camelize(key)] = jsonErrors[key]; + }); + } + return new DS.InvalidError(errors); + } else { + return error; + } + } }); diff --git a/app/pods/users/new/new-user-form/component.js b/app/pods/users/new/new-user-form/component.js index 6f3629a..a0060a5 100644 --- a/app/pods/users/new/new-user-form/component.js +++ b/app/pods/users/new/new-user-form/component.js @@ -9,7 +9,15 @@ export default Ember.Component.extend({ var user = this.get('user'); if (user.get('isDirty')) { - user.save(); + user.save().then(() => { + this.sendAction(); + }).catch(() => { + // Manually clean up messages because there is no transition + this.get('flashMessages').clearMessages(); + user.get('errors').forEach((error) => { + this.get('flashMessages').error(`${error.attribute.capitalize()} - ${error.message}`); + }); + }); } }, }, diff --git a/app/pods/users/new/route.js b/app/pods/users/new/route.js index 5e18e0d..7d5b4a3 100644 --- a/app/pods/users/new/route.js +++ b/app/pods/users/new/route.js @@ -11,4 +11,12 @@ export default Ember.Route.extend(UnauthenticatedRouteMixin, { setupController: function(controller, model) { controller.setProperties(model); }, + + actions: { + success: function() { + this.transitionTo('login'); + this.get('flashMessages').information(`You have successfully signed up. + Please check your email for further instructions.`); + } + }, }); diff --git a/app/pods/users/new/template.hbs b/app/pods/users/new/template.hbs index abf9f72..075ede5 100644 --- a/app/pods/users/new/template.hbs +++ b/app/pods/users/new/template.hbs @@ -1 +1 @@ -{{users/new/new-user-form user=user}} +{{users/new/new-user-form user=user action="success"}} diff --git a/server/mocks/users.js b/server/mocks/users.js index a01f5a8..428b3e9 100644 --- a/server/mocks/users.js +++ b/server/mocks/users.js @@ -47,7 +47,16 @@ module.exports = function(app) { }); usersRouter.post('/', function(req, res) { - res.status(201).end(); + // req.body.user.id = Math.max.apply(Math, USERS.map(function(o){return o.id;})) + 1; + // res.status(201).send(req.body); + // NOTE - use the following for testing errors + res.status(422).send({ + 'errors':{ + "name": ["Must provide a value"], + "email": ["Must provide a value"], + "password": ["Must provide a value"], + } + }).end(); }); usersRouter.get('/:id', function(req, res) {