cardSearch.js 5.0 KB

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