cardSearch.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. export class CardSearchPagedComponent extends BlazeComponent {
  2. onCreated() {
  3. this.searching = new ReactiveVar(false);
  4. this.hasResults = new ReactiveVar(false);
  5. this.hasQueryErrors = new ReactiveVar(false);
  6. this.query = new ReactiveVar('');
  7. this.resultsHeading = new ReactiveVar('');
  8. this.searchLink = new ReactiveVar(null);
  9. this.results = new ReactiveVar([]);
  10. this.hasNextPage = new ReactiveVar(false);
  11. this.hasPreviousPage = new ReactiveVar(false);
  12. this.resultsCount = 0;
  13. this.totalHits = 0;
  14. this.queryErrors = null;
  15. this.resultsPerPage = 25;
  16. this.sessionId = SessionData.getSessionId();
  17. this.subscriptionHandle = null;
  18. this.serverError = new ReactiveVar(false);
  19. const that = this;
  20. this.subscriptionCallbacks = {
  21. onReady() {
  22. that.getResults();
  23. that.searching.set(false);
  24. that.hasResults.set(true);
  25. that.serverError.set(false);
  26. },
  27. onError(error) {
  28. that.searching.set(false);
  29. that.hasResults.set(false);
  30. that.serverError.set(true);
  31. console.log('Error.reason:', error.reason);
  32. console.log('Error.message:', error.message);
  33. console.log('Error.stack:', error.stack);
  34. }
  35. };
  36. }
  37. resetSearch() {
  38. this.searching.set(false);
  39. this.results.set([]);
  40. this.hasResults.set(false);
  41. this.hasQueryErrors.set(false);
  42. this.resultsHeading.set('');
  43. this.serverError.set(false);
  44. this.resultsCount = 0;
  45. this.totalHits = 0;
  46. this.queryErrors = null;
  47. }
  48. getSessionData(sessionId) {
  49. return SessionData.findOne({
  50. sessionId: sessionId ? sessionId : SessionData.getSessionId(),
  51. });
  52. }
  53. getResults() {
  54. // eslint-disable-next-line no-console
  55. // console.log('getting results');
  56. const sessionData = this.getSessionData();
  57. // eslint-disable-next-line no-console
  58. // console.log('selector:', sessionData.getSelector());
  59. console.log('session data:', sessionData);
  60. const projection = sessionData.getProjection();
  61. projection.skip = 0;
  62. const cards = Cards.find({ _id: { $in: sessionData.cards } }, projection);
  63. this.queryErrors = sessionData.errors;
  64. if (this.queryErrors.length) {
  65. // console.log('queryErrors:', this.queryErrorMessages());
  66. this.hasQueryErrors.set(true);
  67. return null;
  68. }
  69. if (cards) {
  70. this.totalHits = sessionData.totalHits;
  71. this.resultsCount = cards.count();
  72. this.resultsStart = sessionData.lastHit - this.resultsCount + 1;
  73. this.resultsEnd = sessionData.lastHit;
  74. this.resultsHeading.set(this.getResultsHeading());
  75. this.results.set(cards);
  76. this.hasNextPage.set(sessionData.lastHit < sessionData.totalHits);
  77. this.hasPreviousPage.set(
  78. sessionData.lastHit - sessionData.resultsCount > 0,
  79. );
  80. return cards;
  81. }
  82. this.resultsCount = 0;
  83. return null;
  84. }
  85. stopSubscription() {
  86. if (this.subscriptionHandle) {
  87. this.subscriptionHandle.stop();
  88. }
  89. }
  90. runGlobalSearch(params) {
  91. this.searching.set(true);
  92. this.stopSubscription();
  93. this.subscriptionHandle = Meteor.subscribe(
  94. 'globalSearch',
  95. this.sessionId,
  96. params,
  97. this.subscriptionCallbacks,
  98. );
  99. }
  100. queryErrorMessages() {
  101. const messages = [];
  102. this.queryErrors.forEach(err => {
  103. let value = err.color ? TAPi18n.__(`color-${err.value}`) : err.value;
  104. if (!value) {
  105. value = err.value;
  106. }
  107. messages.push(TAPi18n.__(err.tag, value));
  108. });
  109. return messages;
  110. }
  111. nextPage() {
  112. this.searching.set(true);
  113. this.stopSubscription();
  114. this.subscriptionHandle = Meteor.subscribe(
  115. 'nextPage',
  116. this.sessionId,
  117. this.subscriptionCallbacks,
  118. );
  119. }
  120. previousPage() {
  121. this.searching.set(true);
  122. this.stopSubscription();
  123. this.subscriptionHandle = Meteor.subscribe(
  124. 'previousPage',
  125. this.sessionId,
  126. this.subscriptionCallbacks,
  127. );
  128. }
  129. getResultsHeading() {
  130. if (this.resultsCount === 0) {
  131. return TAPi18n.__('no-cards-found');
  132. } else if (this.resultsCount === 1) {
  133. return TAPi18n.__('one-card-found');
  134. } else if (this.resultsCount === this.totalHits) {
  135. return TAPi18n.__('n-cards-found', this.resultsCount);
  136. }
  137. return TAPi18n.__('n-n-of-n-cards-found', {
  138. start: this.resultsStart,
  139. end: this.resultsEnd,
  140. total: this.totalHits,
  141. });
  142. }
  143. getSearchHref() {
  144. const baseUrl = window.location.href.replace(/([?#].*$|\s*$)/, '');
  145. return `${baseUrl}?q=${encodeURIComponent(this.query.get())}`;
  146. }
  147. events() {
  148. return [
  149. {
  150. 'click .js-next-page'(evt) {
  151. evt.preventDefault();
  152. this.nextPage();
  153. },
  154. 'click .js-previous-page'(evt) {
  155. evt.preventDefault();
  156. this.previousPage();
  157. },
  158. },
  159. ];
  160. }
  161. }