cardSearch.js 4.8 KB

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