edit-signature-form.js 2.1 KB

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