request-link-form.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import Ember from 'ember';
  2. export default Ember.Component.extend({
  3. tagName: 'form',
  4. isBusy: false,
  5. email: '',
  6. router: function() {
  7. return this.container.lookup('router:main');
  8. }.property(),
  9. submit: function() {
  10. if (this.get('isBusy')) {
  11. return false;
  12. }
  13. var email = Ember.$.trim(this.get('email'));
  14. if (email.length === 0) {
  15. this.toast.warning(gettext("Enter e-mail address."));
  16. return false;
  17. }
  18. this.set('isBusy', 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('isBusy', 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. });