request-link-form.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import Ember from 'ember';
  2. export default Ember.Component.extend({
  3. tagName: 'form',
  4. isLoading: false,
  5. email: '',
  6. router: function() {
  7. return this.container.lookup('router:main');
  8. }.property(),
  9. submit: function() {
  10. if (this.get('isLoading')) {
  11. return false;
  12. }
  13. var email = Ember.$.trim(this.get('email'));
  14. if (email === "") {
  15. this.toast.warning(gettext("Enter e-mail address."));
  16. return false;
  17. }
  18. this.set('isLoading', true);
  19. var self = this;
  20. this.ajax.post(this.get('url'), {
  21. email: email
  22. }).then(function(requestingUser) {
  23. if (self.isDestroyed) { return; }
  24. self.success(requestingUser);
  25. }, function(jqXHR) {
  26. if (self.isDestroyed) { return; }
  27. self.error(jqXHR);
  28. }).finally(function() {
  29. if (self.isDestroyed) { return; }
  30. self.set('isLoading', false);
  31. });
  32. return false;
  33. },
  34. success: function(requestingUser) {
  35. this.sendAction('action', requestingUser);
  36. this.set('email', '');
  37. },
  38. error: function(jqXHR) {
  39. if (jqXHR.status === 400){
  40. var rejection = jqXHR.responseJSON;
  41. if (rejection.code === 'banned') {
  42. this.get('router').intermediateTransitionTo('error-banned', rejection.detail);
  43. this.set('email', '');
  44. } else {
  45. this.toast.error(rejection.detail);
  46. }
  47. } else {
  48. this.toast.apiError(jqXHR);
  49. }
  50. }
  51. });