set-new-password-form.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import Ember from 'ember';
  2. export default Ember.Component.extend({
  3. tagName: 'form',
  4. isLoading: 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('isLoading')) {
  14. return false;
  15. }
  16. var password = Ember.$.trim(this.get('password'));
  17. if (password === "") {
  18. this.toast.warning(gettext("Enter new password."));
  19. return false;
  20. }
  21. this.set('isLoading', true);
  22. var self = this;
  23. this.ajax.post(this.get('url'), {
  24. password: password
  25. }).then(function() {
  26. if (self.isDestroyed) { return; }
  27. self.success();
  28. }, function(jqXHR) {
  29. if (self.isDestroyed) { return; }
  30. self.error(jqXHR);
  31. }).finally(function() {
  32. self.set('isLoading', false);
  33. });
  34. return false;
  35. },
  36. success: function() {
  37. this.set('password', '');
  38. this.modal.show('login-modal');
  39. this.toast.success(gettext("Your password has been changed."));
  40. },
  41. error: function(jqXHR) {
  42. var rejection = jqXHR.responseJSON;
  43. if (jqXHR.status === 400){
  44. this.toast.error(rejection.detail);
  45. } else {
  46. if (jqXHR.status === 404) {
  47. this.toast.error(rejection.detail);
  48. this.get('router').transitionTo('forgotten-password');
  49. } else {
  50. this.toast.apiError(jqXHR);
  51. }
  52. }
  53. }
  54. });