request-link-form.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.rpc.ajax(this.get('url'), {
  21. email: email
  22. }).then(function(requestingUser) {
  23. self.success(requestingUser);
  24. }, function(jqXHR) {
  25. self.error(jqXHR);
  26. }).finally(function() {
  27. self.set('isLoading', false);
  28. });
  29. return false;
  30. },
  31. success: function(requestingUser) {
  32. this.sendAction('action', requestingUser);
  33. this.set('email', '');
  34. },
  35. error: function(jqXHR) {
  36. if (jqXHR.status === 400){
  37. var rejection = jqXHR.responseJSON;
  38. if (rejection.code === 'banned') {
  39. this.get('router').intermediateTransitionTo('error-banned', rejection.detail);
  40. this.set('email', '');
  41. } else {
  42. this.toast.error(rejection.detail);
  43. }
  44. } else {
  45. this.toast.apiError(jqXHR);
  46. }
  47. }
  48. });