Browse Source

fix #513

* added apiError to toasts service
* added routes for error pages
Rafał Pitoń 10 years ago
parent
commit
872b04e6ba

+ 2 - 5
misago/emberapp/app/components/login-modal.js

@@ -51,20 +51,17 @@ export default Ember.Component.extend({
     $form.submit();
   },
 
-  toastError: 'toastError',
-  showBan: 'showBan',
-
   error: function(jqXHR) {
     var rejection = jqXHR.responseJSON;
     if (jqXHR.status !== 400) {
-      this.sendAction('toastError', jqXHR);
+      this.toast.apiError(jqXHR);
     } else if (rejection.code === 'inactive_admin') {
       this.toast.info(rejection.detail);
     } else if (rejection.code === 'inactive_user') {
       this.toast.info(rejection.detail);
       this.set('showActivation', true);
     } else if (rejection.code === 'banned') {
-      this.sendAction('showBan', rejection.detail);
+      this.get('router').intermediateTransitionTo('error-banned', rejection.detail);
       Ember.run(function() {
         Ember.$('#loginModal').modal('hide');
       });

+ 25 - 21
misago/emberapp/app/controllers/activation/request-link.js

@@ -5,6 +5,29 @@ export default Ember.ObjectController.extend({
   isLoading: false,
   email: '',
 
+  router: function() {
+    return this.container.lookup('router:main');
+  }.property(),
+
+  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.get('router').intermediateTransitionTo('error-banned', rejection.detail);
+        this.set('email', '');
+      } else {
+        this.toast.error(rejection.detail);
+      }
+    } else {
+      this.toast.apiError(jqXHR);
+    }
+  },
+
   actions: {
     submit: function() {
       if (this.get('isLoading')) {
@@ -24,31 +47,12 @@ export default Ember.ObjectController.extend({
       this.rpc.ajax(this.get('rpcUrl'), {
         email: email
       }).then(function(requestingUser) {
-        self.send('success', requestingUser);
+        self.success(requestingUser);
       }, function(jqXHR) {
-        self.send('error', jqXHR);
+        self.error(jqXHR);
       }).finally(function() {
         self.set('isLoading', 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.toast.error(rejection.detail);
-        }
-      } else {
-        this.send('toastError', jqXHR);
-      }
     }
   }
 });

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

@@ -51,7 +51,7 @@ export default Ember.ObjectController.extend({
           this.toast.error(rejection.detail);
           this.transitionTo('forgotten-password');
         } else {
-          this.send('toastError', jqXHR);
+          this.toast.apiError(jqXHR);
         }
       }
     }

+ 0 - 113
misago/emberapp/app/controllers/login-modal.js

@@ -1,113 +0,0 @@
-import Ember from 'ember';
-
-export default Ember.Component.extend({
-  modal: null,
-
-  isLoading: false,
-  showActivation: false,
-
-  username: '',
-  password: '',
-
-  setup: function() {
-    this.modal = Ember.$('#loginModal').modal({show: false});
-
-    this.modal.on('shown.bs.modal', function () {
-      Ember.$('#loginModal').focus();
-    });
-
-    var self = this;
-    this.modal.on('hidden.bs.modal', function() {
-      self.reset();
-    });
-  }.on('didInsertElement'),
-
-  reset: function() {
-    this.setProperties({
-      'username': '',
-      'password': '',
-
-      'isLoading': false,
-      'showActivation': false
-    });
-  },
-
-  actions: {
-    open: function() {
-      Ember.$('#loginModal').modal('show');
-    },
-
-    submit: function() {
-      if (this.get('isLoading')) {
-        return;
-      }
-
-      var credentials = {
-        username: Ember.$.trim(this.get('username')),
-        password: Ember.$.trim(this.get('password'))
-      };
-
-      if (!credentials.username || !credentials.password) {
-        this.toast.warning(gettext("Fill out both fields."));
-        return;
-      }
-
-      var self = this;
-      this.rpc.ajax(this.get('settings.loginApiUrl'), credentials
-      ).then(function() {
-        self.send('success', credentials);
-      }, function(jqXHR) {
-        self.send('error', jqXHR);
-      }).finally(function() {
-        self.set('isLoading', false);
-      });
-    },
-
-    success: function(credentials) {
-      var $form = Ember.$('#hidden-login-form');
-
-      // refresh CSRF token because parent api call changed it
-      this.csrf.updateFormToken($form);
-
-      // fill out form with user credentials and submit it, this will tell
-      // misago to redirect user back to right page, and will trigger browser's
-      // key ring feature
-      $form.find('input[name=redirect_to]').val(window.location.href);
-      $form.find('input[name=username]').val(credentials.username);
-      $form.find('input[name=password]').val(credentials.password);
-      $form.submit();
-    },
-
-    error: function(jqXHR) {
-      var rejection = jqXHR.responseJSON;
-      if (jqXHR.status !== 400) {
-        this.send('toastError', jqXHR);
-      } else if (rejection.code === 'inactive_admin') {
-        this.toast.info(rejection.detail);
-      } else if (rejection.code === 'inactive_user') {
-        this.toast.info(rejection.detail);
-        this.set('showActivation', true);
-      } else if (rejection.code === 'banned') {
-        this.send('showBan', rejection.detail);
-        Ember.run(function() {
-          Ember.$('#loginModal').modal('hide');
-        });
-      } else {
-        this.toast.error(rejection.detail);
-      }
-      return false;
-    },
-
-    // Go-to links
-
-    activateAccount: function() {
-      this.transitionToRoute('activation');
-      Ember.$('#loginModal').modal('hide');
-    },
-
-    forgotPassword: function() {
-      this.transitionToRoute('forgotten-password');
-      Ember.$('#loginModal').modal('hide');
-    }
-  }
-});

+ 2 - 3
misago/emberapp/app/mixins/reset-scroll.js

@@ -1,8 +1,7 @@
 import Ember from 'ember';
 
 export default Ember.Mixin.create({
-  activate: function() {
-    this._super();
+  resetScroll: function() {
     window.scrollTo(0,0);
-  }
+  }.on('activate')
 });

+ 0 - 32
misago/emberapp/app/routes/application.js

@@ -13,18 +13,14 @@ export default MisagoRoute.extend({
 
     error: function(reason) {
       if (reason.status === 0) {
-        this.set('title', gettext('Connection lost'));
         return this.intermediateTransitionTo('error-0');
       }
 
       if (typeof reason.responseJSON !== 'undefined' && typeof reason.responseJSON.ban !== 'undefined') {
-        this.set('title', gettext('You are banned'));
         return this.intermediateTransitionTo('error-banned', reason.responseJSON.ban);
       }
 
       if (reason.status === 403) {
-        this.set('title', gettext('Page not available'));
-
         var final_error = {status: 403, message: null};
         if (typeof reason.responseJSON !== 'undefined' && typeof reason.responseJSON.detail !== 'undefined' && reason.responseJSON.detail !== 'Permission denied') {
           final_error.message = reason.responseJSON.detail;
@@ -34,39 +30,11 @@ export default MisagoRoute.extend({
       }
 
       if (reason.status === 404) {
-        this.set('title', gettext('Page not found'));
         return this.intermediateTransitionTo('error-404');
       }
 
       this.set('title', gettext('Error'));
       return true;
-    },
-
-    toastError: function(reason) {
-      var errorMessage = gettext('Unknown error has occured.');
-
-      if (reason.status === 0) {
-        errorMessage = gettext('Lost connection with application.');
-      }
-
-      if (reason.status === 403) {
-        if (typeof reason.responseJSON !== 'undefined' && typeof reason.responseJSON.detail !== 'undefined' && reason.responseJSON.detail !== 'Permission denied') {
-          errorMessage = reason.responseJSON.detail;
-        } else {
-          errorMessage = gettext("You don't have permission to perform this action.");
-        }
-      }
-
-      if (reason.status === 404) {
-        errorMessage = gettext('Action link is invalid.');
-      }
-
-      this.toast.error(errorMessage);
-    },
-
-    showBan: function(ban) {
-      this.set('title', gettext('You are banned'));
-      this.intermediateTransitionTo('error-banned', ban);
     }
   }
 });

+ 7 - 0
misago/emberapp/app/routes/error-0.js

@@ -0,0 +1,7 @@
+import MisagoRoute from 'misago/routes/misago';
+
+export default MisagoRoute.extend({
+  setTitle: function() {
+    this.set('title', gettext('Connection lost'));
+  }.on('activate')
+});

+ 7 - 0
misago/emberapp/app/routes/error-403.js

@@ -0,0 +1,7 @@
+import MisagoRoute from 'misago/routes/misago';
+
+export default MisagoRoute.extend({
+  setTitle: function() {
+    this.set('title', gettext('Page not available'));
+  }.on('activate')
+});

+ 7 - 0
misago/emberapp/app/routes/error-404.js

@@ -0,0 +1,7 @@
+import MisagoRoute from 'misago/routes/misago';
+
+export default MisagoRoute.extend({
+  setTitle: function() {
+    this.set('title', gettext('Page not found'));
+  }.on('activate')
+});

+ 7 - 0
misago/emberapp/app/routes/error-banned.js

@@ -0,0 +1,7 @@
+import MisagoRoute from 'misago/routes/misago';
+
+export default MisagoRoute.extend({
+  setTitle: function() {
+    this.set('title', gettext('You are banned'));
+  }.on('activate')
+});

+ 23 - 0
misago/emberapp/app/services/toast-message.js

@@ -58,5 +58,28 @@ export default Ember.Service.extend({
 
   error: function(message) {
     this._setToast('error', message);
+  },
+
+  apiError: function(reason) {
+    reason = Ember.Object.create(reason);
+
+    var errorMessage = gettext('Unknown error has occured.');
+
+    if (reason.get('status') === 0) {
+      errorMessage = gettext('Lost connection with application.');
+    }
+
+    if (reason.get('status') === 403) {
+      errorMessage = reason.get('responseJSON.detail');
+      if (errorMessage === 'Permission denied') {
+        errorMessage = gettext("You don't have permission to perform this action.");
+      }
+    }
+
+    if (reason.get('status') === 404) {
+      errorMessage = gettext('Action link is invalid.');
+    }
+
+    this.error(errorMessage);
   }
 });