peopleBody.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const usersPerPage = 25;
  2. BlazeComponent.extendComponent({
  3. mixins() {
  4. return [Mixins.InfiniteScrolling];
  5. },
  6. onCreated() {
  7. this.error = new ReactiveVar('');
  8. this.loading = new ReactiveVar(false);
  9. this.people = new ReactiveVar(true);
  10. this.page = new ReactiveVar(1);
  11. this.loadNextPageLocked = false;
  12. this.callFirstWith(null, 'resetNextPeak');
  13. this.autorun(() => {
  14. const limit = this.page.get() * usersPerPage;
  15. this.subscribe('people', limit, () => {
  16. this.loadNextPageLocked = false;
  17. const nextPeakBefore = this.callFirstWith(null, 'getNextPeak');
  18. this.calculateNextPeak();
  19. const nextPeakAfter = this.callFirstWith(null, 'getNextPeak');
  20. if (nextPeakBefore === nextPeakAfter) {
  21. this.callFirstWith(null, 'resetNextPeak');
  22. }
  23. });
  24. });
  25. },
  26. loadNextPage() {
  27. if (this.loadNextPageLocked === false) {
  28. this.page.set(this.page.get() + 1);
  29. this.loadNextPageLocked = true;
  30. }
  31. },
  32. calculateNextPeak() {
  33. const element = this.find('.main-body');
  34. if (element) {
  35. const altitude = element.scrollHeight;
  36. this.callFirstWith(this, 'setNextPeak', altitude);
  37. }
  38. },
  39. reachNextPeak() {
  40. this.loadNextPage();
  41. },
  42. setError(error) {
  43. this.error.set(error);
  44. },
  45. setLoading(w) {
  46. this.loading.set(w);
  47. },
  48. peopleList() {
  49. return Users.find({}, {
  50. fields: {_id: true},
  51. });
  52. },
  53. }).register('people');
  54. Template.peopleRow.helpers({
  55. userData() {
  56. const userCollection = this.esSearch ? ESSearchResults : Users;
  57. return userCollection.findOne(this.userId);
  58. },
  59. });
  60. Template.editUserPopup.helpers({
  61. user() {
  62. return Users.findOne(this.userId);
  63. },
  64. });
  65. BlazeComponent.extendComponent({
  66. onCreated() {
  67. },
  68. user() {
  69. return Users.findOne(this.userId);
  70. },
  71. events() {
  72. return [{
  73. 'click a.edit-user': Popup.open('editUser'),
  74. }];
  75. },
  76. }).register('peopleRow');
  77. Template.editUserPopup.events({
  78. submit(evt, tpl) {
  79. evt.preventDefault();
  80. const user = Users.findOne(this.userId);
  81. const fullname = tpl.find('.js-profile-fullname').value.trim();
  82. const username = tpl.find('.js-profile-username').value.trim();
  83. const password = tpl.find('.js-profile-password').value;
  84. const isAdmin = tpl.find('.js-profile-isadmin').value.trim();
  85. const isActive = tpl.find('.js-profile-isactive').value.trim();
  86. const email = tpl.find('.js-profile-email').value.trim();
  87. const isChangePassword = password.length > 0;
  88. const isChangeUserName = username !== user.username;
  89. const isChangeEmail = email.toLowerCase() !== user.emails[0].address.toLowerCase();
  90. Users.update(this.userId, {
  91. $set: {
  92. 'profile.fullname': fullname,
  93. 'isAdmin': isAdmin === 'true',
  94. 'loginDisabled': isActive === 'true',
  95. },
  96. });
  97. if(isChangePassword){
  98. Meteor.call('setPassword', password, this.userId);
  99. }
  100. if (isChangeUserName && isChangeEmail) {
  101. Meteor.call('setUsernameAndEmail', username, email.toLowerCase(), this.userId, function (error) {
  102. const usernameMessageElement = tpl.$('.username-taken');
  103. const emailMessageElement = tpl.$('.email-taken');
  104. if (error) {
  105. const errorElement = error.error;
  106. if (errorElement === 'username-already-taken') {
  107. usernameMessageElement.show();
  108. emailMessageElement.hide();
  109. } else if (errorElement === 'email-already-taken') {
  110. usernameMessageElement.hide();
  111. emailMessageElement.show();
  112. }
  113. } else {
  114. usernameMessageElement.hide();
  115. emailMessageElement.hide();
  116. Popup.close();
  117. }
  118. });
  119. } else if (isChangeUserName) {
  120. Meteor.call('setUsername', username, this.userId, function (error) {
  121. const usernameMessageElement = tpl.$('.username-taken');
  122. if (error) {
  123. const errorElement = error.error;
  124. if (errorElement === 'username-already-taken') {
  125. usernameMessageElement.show();
  126. }
  127. } else {
  128. usernameMessageElement.hide();
  129. Popup.close();
  130. }
  131. });
  132. } else if (isChangeEmail) {
  133. Meteor.call('setEmail', email.toLowerCase(), this.userId, function (error) {
  134. const emailMessageElement = tpl.$('.email-taken');
  135. if (error) {
  136. const errorElement = error.error;
  137. if (errorElement === 'email-already-taken') {
  138. emailMessageElement.show();
  139. }
  140. } else {
  141. emailMessageElement.hide();
  142. Popup.close();
  143. }
  144. });
  145. } else Popup.close();
  146. },
  147. });