set-new-password-form.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import Ember from 'ember';
  2. export default Ember.Component.extend({
  3. tagName: 'form',
  4. isBusy: false,
  5. password: '',
  6. router: function() {
  7. return this.container.lookup('router:main');
  8. }.property(),
  9. url: function() {
  10. return 'auth/change-password/' + this.get('model.user_id') + '/' + this.get('model.token');
  11. }.property('model'),
  12. submit: function() {
  13. if (this.get('isBusy')) { return false; }
  14. var password = Ember.$.trim(this.get('password'));
  15. if (password === "") {
  16. this.toast.warning(gettext("Enter new password."));
  17. return false;
  18. }
  19. this.set('isBusy', true);
  20. var self = this;
  21. this.ajax.post(this.get('url'), {
  22. password: password
  23. }).then(function() {
  24. if (self.isDestroyed) { return; }
  25. self.success();
  26. }, function(jqXHR) {
  27. if (self.isDestroyed) { return; }
  28. self.error(jqXHR);
  29. }).finally(function() {
  30. self.set('isBusy', false);
  31. });
  32. return false;
  33. },
  34. success: function() {
  35. this.set('password', '');
  36. this.modal.show('login-modal');
  37. this.toast.success(gettext("Your password has been changed."));
  38. },
  39. error: function(jqXHR) {
  40. var rejection = jqXHR.responseJSON;
  41. if (jqXHR.status === 400){
  42. this.toast.error(rejection.detail);
  43. } else {
  44. if (jqXHR.status === 404) {
  45. this.toast.error(rejection.detail);
  46. this.get('router').transitionTo('forgotten-password');
  47. } else {
  48. this.toast.apiError(jqXHR);
  49. }
  50. }
  51. }
  52. });