Browse Source

refactored form handling in change forgotten password.

Rafał Pitoń 10 years ago
parent
commit
a0c70c0139

+ 24 - 20
misago/emberapp/app/controllers/forgotten-password/change-form.js

@@ -6,7 +6,7 @@ export default Ember.ObjectController.extend({
   password: '',
 
   actions: {
-    changePassword: function() {
+    submit: function() {
       if (this.get('isLoading')) {
         return;
       }
@@ -24,29 +24,33 @@ export default Ember.ObjectController.extend({
       rpc(this.get('change_password_url'), {
         password: password
       }).then(function() {
-        self.set('password', '');
-
-        self.send('openLoginModal');
-        self.get('toast').success(gettext("Your password has been changed."));
-
+        self.send('success');
       }, function(jqXHR) {
-        var rejection = jqXHR.responseJSON;
-        if (jqXHR.status === 400){
-          self.get('toast').error(rejection.detail);
-        } else {
-          self.set('password', '');
-
-          if (jqXHR.status === 404) {
-            self.get('toast').error(rejection.detail);
-            self.transitionTo('forgotten-password');
-          } else {
-            self.send("error", jqXHR);
-          }
-        }
-
+        self.send('error', jqXHR);
       }).finally(function() {
         self.set('isLoading', false);
       });
+    },
+
+    success: function() {
+      this.set('password', '');
+
+      this.send('openLoginModal');
+      this.get('toast').success(gettext("Your password has been changed."));
+    },
+
+    error: function(jqXHR) {
+      var rejection = jqXHR.responseJSON;
+      if (jqXHR.status === 400){
+        this.get('toast').error(rejection.detail);
+      } else {
+        if (jqXHR.status === 404) {
+          this.get('toast').error(rejection.detail);
+          this.transitionTo('forgotten-password');
+        } else {
+          this.send('toastError', jqXHR);
+        }
+      }
     }
   }
 });

+ 22 - 19
misago/emberapp/app/controllers/forgotten-password/request-link.js

@@ -6,7 +6,7 @@ export default Ember.ObjectController.extend({
   email: '',
 
   actions: {
-    sendLink: function() {
+    submit: function() {
       if (this.get('isLoading')) {
         return;
       }
@@ -24,28 +24,31 @@ export default Ember.ObjectController.extend({
       rpc('change-password/send-link/', {
         email: email
       }).then(function(requestingUser) {
-        self.send('showSentPage', requestingUser);
-        self.set('email', '');
-
+        self.send('success', requestingUser);
       }, function(jqXHR) {
-        if (jqXHR.status === 400){
-          var rejection = jqXHR.responseJSON;
-          if (rejection.code === 'banned') {
-            self.send('showBan', rejection.detail);
-            self.set('email', '');
-          } else {
-            self.get('toast').error(rejection.detail);
-          }
-        } else {
-          self.send("error", jqXHR);
-          self.set('email', '');
-        }
-
+        self.send('error', jqXHR);
       }).finally(function() {
         self.set('isLoading', false);
       });
-
-      return false;
+    },
+
+    success: function(requestingUser) {
+      this.send('showSentPage', requestingUser);
+      this.set('email', '');
+    },
+
+    error: function(jqXHR) {
+      if (jqXHR.status === 400){
+        var rejection = jqXHR.responseJSON;
+        if (rejection.code === 'banned') {
+          this.send('showBan', rejection.detail);
+          this.set('email', '');
+        } else {
+          this.get('toast').error(rejection.detail);
+        }
+      } else {
+        this.send('toastError', jqXHR);
+      }
     }
   }
 });

+ 1 - 1
misago/emberapp/app/templates/forgotten-password/change-form.hbs

@@ -11,7 +11,7 @@
       <div class="col-md-4 col-md-offset-4">
 
         <div class="well well-form">
-          <form {{action "changePassword" on="submit"}}>
+          <form {{action "submit" on="submit"}}>
 
             <div class="form-group">
               <div class="control-input">

+ 1 - 1
misago/emberapp/app/templates/forgotten-password/request-link.hbs

@@ -20,7 +20,7 @@
       <div class="col-md-4">
 
         <div class="well well-form">
-          <form {{action "sendLink" on="submit"}}>
+          <form {{action "submit" on="submit"}}>
 
             <div class="form-group">
               <div class="control-input">