forum-options-form.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import Ember from 'ember';
  2. export default Ember.Component.extend({
  3. tagName: 'form',
  4. classNames: 'form-horizontal',
  5. isBusy: false,
  6. subscribeToChoices: [
  7. {value: 0, label: gettext('No')},
  8. {value: 1, label: gettext('Bookmark')},
  9. {value: 2, label: gettext('Bookmark with e-mail notification')}
  10. ],
  11. privateThreadInvitesChoices: [
  12. {value: 0, label: gettext('Everybody')},
  13. {value: 1, label: gettext('Users I follow')},
  14. {value: 2, label: gettext('Nobody')}
  15. ],
  16. is_hiding_presence: null,
  17. limits_private_thread_invites_to: null,
  18. subscribe_to_started_threads: null,
  19. subscribe_to_replied_threads: null,
  20. setInitialValues: function() {
  21. this.setProperties({
  22. is_hiding_presence: this.get('auth.user.is_hiding_presence'),
  23. limits_private_thread_invites_to: this.get('auth.user.limits_private_thread_invites_to'),
  24. subscribe_to_started_threads: this.get('auth.user.subscribe_to_started_threads'),
  25. subscribe_to_replied_threads: this.get('auth.user.subscribe_to_replied_threads'),
  26. });
  27. }.on('init'),
  28. validation: null,
  29. setValidation: function() {
  30. this.set('validation', Ember.Object.create({}));
  31. }.on('init'),
  32. isHidingPresenceValidation: function() {
  33. this.set('validation.is_hiding_presence', undefined);
  34. }.observes('is_hiding_presence'),
  35. limitsPrivateThreadInvitesToValidation: function() {
  36. this.set('validation.limits_private_thread_invites_to', undefined);
  37. }.observes('limits_private_thread_invites_to'),
  38. subscribeToStartedThreadsValidation: function() {
  39. this.set('validation.subscribe_to_started_threads', undefined);
  40. }.observes('subscribe_to_started_threads'),
  41. subscribeToRepliedThreadsValidation: function() {
  42. this.set('validation.subscribe_to_replied_threads', undefined);
  43. }.observes('subscribe_to_replied_threads'),
  44. apiUrl: function() {
  45. return 'users/' + this.auth.get('user.id') + '/forum-options';
  46. }.property(),
  47. submit: function() {
  48. if (this.get('isBusy')) {
  49. return false;
  50. }
  51. var newOptions = {
  52. is_hiding_presence: this.get('is_hiding_presence'),
  53. limits_private_thread_invites_to: this.get('limits_private_thread_invites_to'),
  54. subscribe_to_started_threads: this.get('subscribe_to_started_threads'),
  55. subscribe_to_replied_threads: this.get('subscribe_to_replied_threads')
  56. };
  57. this.set('isBusy', true);
  58. var self = this;
  59. this.ajax.post(this.get('apiUrl'), newOptions
  60. ).then(function(response) {
  61. if (self.isDestroyed) { return; }
  62. self.success(response, newOptions);
  63. }, function(jqXHR) {
  64. if (self.isDestroyed) { return; }
  65. self.error(jqXHR);
  66. }).finally(function() {
  67. if (self.isDestroyed) { return; }
  68. self.set('isBusy', false);
  69. });
  70. return false;
  71. },
  72. success: function(responseJSON, newOptions) {
  73. this.get('auth.user').setProperties(newOptions);
  74. this.toast.success(responseJSON.detail);
  75. },
  76. error: function(jqXHR) {
  77. if (jqXHR.status === 400) {
  78. this.toast.error(gettext("Form contains errors."));
  79. this.get('validation').setProperties(jqXHR.responseJSON);
  80. } else {
  81. this.toast.apiError(jqXHR);
  82. }
  83. }
  84. });