edit-signature-form.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import Ember from 'ember';
  2. export default Ember.Component.extend({
  3. tagName: 'form',
  4. isLoaded: false,
  5. loadError: false,
  6. isBusy: false,
  7. options: null,
  8. signature: '',
  9. apiUrl: function() {
  10. return 'users/' + this.auth.get('user.id') + '/signature';
  11. }.property(),
  12. loadOptions: function() {
  13. var self = this;
  14. this.ajax.get(this.get('apiUrl')
  15. ).then(function(options) {
  16. if (self.isDestroyed) { return; }
  17. self.setProperties({
  18. 'options': Ember.Object.create(options),
  19. 'signature': self.get('options.signature.plain') || '',
  20. 'isLoaded': true
  21. });
  22. }, function(jqXHR) {
  23. if (self.isDestroyed) { return; }
  24. if (typeof jqXHR.responseJSON !== 'undefined') {
  25. self.set('loadError', jqXHR.responseJSON);
  26. } else if (jqXHR.status === 0) {
  27. self.set('loadError', {'detail': gettext('Lost connection with application.')});
  28. } else {
  29. self.set('loadError', {'detail': gettext('Application has errored.')});
  30. }
  31. });
  32. }.on('init'),
  33. submit: function() {
  34. if (this.get('isBusy')) {
  35. return false;
  36. }
  37. if (this.get('signature').length > this.get('options.limit')) {
  38. this.toast.warning(gettext('Signature is too long.'));
  39. return false;
  40. }
  41. this.set('isBusy', true);
  42. var self = this;
  43. this.ajax.post(this.get('apiUrl'), {'signature': this.get('signature')}
  44. ).then(function(response) {
  45. if (self.isDestroyed) { return; }
  46. self.success(response);
  47. }, function(jqXHR) {
  48. if (self.isDestroyed) { return; }
  49. self.error(jqXHR);
  50. }).finally(function() {
  51. if (self.isDestroyed) { return; }
  52. self.set('isBusy', false);
  53. });
  54. return false;
  55. },
  56. success: function(responseJSON) {
  57. this.get('options').setProperties(responseJSON);
  58. if (this.get('options.signature.plain')) {
  59. this.toast.success(gettext('Your signature was updated.'));
  60. } else {
  61. this.toast.info(gettext('Your signature was cleared.'));
  62. }
  63. },
  64. error: function(jqXHR) {
  65. if (jqXHR.status === 400) {
  66. this.toast.error(jqXHR.responseJSON.detail);
  67. } else {
  68. this.toast.apiError(jqXHR);
  69. }
  70. }
  71. });