change-form.js 1.4 KB

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