cardSearch.js 5.0 KB

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