@rocketsearch/vue-uappi 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rocketsearch/vue-uappi",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Rocket Search para storefronts Uappi em Vue 3 + Vite + Pinia: busca, autocomplete, vitrines, banners e eventos",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -74,7 +74,7 @@
74
74
  <li v-for="(value, j) in facet.values || []" :key="'f-' + j" :data-alias="facet.alias">
75
75
  <span class="option-title" :data-alias="facet.alias">
76
76
  <span class="name" v-text="facet.alias + ':'" />
77
- <span class="label" v-text="value" />
77
+ <span class="label" v-text="facet.type === 'rating' ? ratingLabel(value) : value" />
78
78
  </span>
79
79
  <button class="rocket-search__facets__selected__clear-filter" aria-label="Remover filtro" @click="clearFacet(value, facet)">
80
80
  x
@@ -185,7 +185,7 @@ import '../css/search.css';
185
185
  import RocketSearchFacets from './search/RocketSearchFacets.vue';
186
186
  import RocketSearchPagination from './search/RocketSearchPagination.vue';
187
187
  import { productImage, productPath, productPrice, formatCurrency } from '../uappi.js';
188
- import { TRACKING_PARAMS, didYouMeanQuery, dispatchDidYouMeanClick } from '../listing.js';
188
+ import { TRACKING_PARAMS, gridClickMeta, didYouMeanQuery, dispatchDidYouMeanClick, ratingLabel } from '../listing.js';
189
189
 
190
190
  // Parâmetros de campanha nunca viram filtro na busca.
191
191
  function emptyContext () {
@@ -295,6 +295,7 @@ export default {
295
295
  productPath,
296
296
  productPrice,
297
297
  formatCurrency,
298
+ ratingLabel,
298
299
 
299
300
  // ---- Busca ----
300
301
 
@@ -401,17 +402,22 @@ export default {
401
402
  // ---- Facets ----
402
403
 
403
404
  isSelected (value, facet) {
405
+ // Avaliação: 0 e 1 são opções (Sem avaliações / 1 estrela ou mais), não o booleano.
406
+ if (facet.type === 'rating') {
407
+ const rating = (this.context.appliedFilters || []).find(f => f.field === facet.field);
408
+ return !!rating && (rating.values || []).includes(Number(value));
409
+ }
404
410
  if (value === 0 || value === 1) {
405
411
  const booleanFilter = (this.context.appliedFilters || []).find(f => f.type === 'boolean' && f.field === facet.field);
406
412
  return !!booleanFilter && booleanFilter.value === (value === 1);
407
413
  }
408
- return (this.context.appliedFilters || []).some(f => (f.values || []).includes(value));
414
+ return (this.context.appliedFilters || []).some(f => f.type !== 'rating' && (f.values || []).includes(value));
409
415
  },
410
416
 
411
417
  clickFacet (value, facet) {
412
418
  const selected = this.isSelected(value.value, facet);
413
419
  this.updateQuery((query) => {
414
- if (value.value === 0 || value.value === 1) {
420
+ if (facet.type !== 'rating' && (value.value === 0 || value.value === 1)) {
415
421
  query[facet.label] = selected ? '' : String(value.value);
416
422
  }
417
423
  else {
@@ -505,7 +511,7 @@ export default {
505
511
  this.$rocket.dispatchEvent('click', {
506
512
  productId: String(product.id),
507
513
  position: index,
508
- meta: { source: 'search-page', query: this.term || '' }
514
+ meta: gridClickMeta(this.term, this.isCategory && this.categoryId)
509
515
  }).catch(() => {});
510
516
  }
511
517
  }
@@ -14,7 +14,7 @@
14
14
  <li v-for="(value, j) in facet.values || []" :key="'f-' + j" :data-alias="facet.alias">
15
15
  <span class="option-title" :data-alias="facet.alias">
16
16
  <span class="name" v-text="facet.alias + ':'" />
17
- <span class="label" v-text="value" />
17
+ <span class="label" v-text="facet.type === 'rating' ? ratingLabel(value) : value" />
18
18
  </span>
19
19
  <button class="rocket-search__facets__selected__clear-filter" @click="$emit('clear-facet', value, facet)">
20
20
  x
@@ -73,6 +73,22 @@
73
73
  :facet="facet"
74
74
  @apply="$emit('apply-price', $event)"
75
75
  />
76
+ <ul v-else-if="facet.type === 'rating'" class="facet-rating-list">
77
+ <li
78
+ v-for="value in facet.values || []"
79
+ :key="value.value"
80
+ :data-selected="isSelected(value.value, facet)"
81
+ :class="{ selected: isSelected(value.value, facet) }"
82
+ >
83
+ <a href="javascript:void(0);" @click="$emit('click-facet', value, facet)">
84
+ <span v-if="value.value > 0" style="display:flex;align-items:center;gap:6px;">
85
+ <span class="rating-stars"><span v-for="n in 5" :key="n" :class="n <= value.value ? 'star-filled' : 'star-empty'">★</span></span>
86
+ <span class="rating-label"><span v-text="value.value" /><span v-text="ratingSuffix(value.value)" /></span>
87
+ </span>
88
+ <span v-else class="rating-label no-reviews">Sem avaliações</span>
89
+ </a>
90
+ </li>
91
+ </ul>
76
92
  <!-- terms e boolean usam a mesma lista (na Linx o boolean reaproveitava o template de terms). -->
77
93
  <ul v-else>
78
94
  <li
@@ -110,6 +126,7 @@
110
126
  <script>
111
127
  import RocketSearchRange from './RocketSearchRange.vue';
112
128
  import { formatCurrency } from '../../uappi.js';
129
+ import { ratingLabel, ratingSuffix } from '../../listing.js';
113
130
 
114
131
  export default {
115
132
  name: 'RocketSearchFacets',
@@ -132,6 +149,8 @@ export default {
132
149
  },
133
150
  methods: {
134
151
  formatCurrency,
152
+ ratingLabel,
153
+ ratingSuffix,
135
154
  // Facet booleano (ex.: Disponibilidade/hasStock) vem como 0/1 — mesmo texto do chip aplicado.
136
155
  valueLabel (value) {
137
156
  if (value === 0) return 'Não';
@@ -717,3 +717,17 @@
717
717
  text-decoration: underline;
718
718
  }
719
719
  }
720
+
721
+ /* Faceta de avaliação (type 'rating'). */
722
+ .rocket-search .facet-rating-list .star-filled {
723
+ color: #f5a623;
724
+ }
725
+
726
+ .rocket-search .facet-rating-list .star-empty {
727
+ color: #d9d9d9;
728
+ }
729
+
730
+ .rocket-search .facet-rating-list li.selected .rating-label {
731
+ color: #232626;
732
+ font-weight: 700;
733
+ }
@@ -21,6 +21,19 @@ function splitValues (raw) {
21
21
  return String(raw || '').split(';').filter(v => v !== '');
22
22
  }
23
23
 
24
+ // Faceta de avaliação (type 'rating'): opções cumulativas 5..1 e 0 = sem avaliação.
25
+ export function ratingSuffix (n) {
26
+ n = Number(n);
27
+ if (n === 5) return ' estrelas';
28
+ if (n === 1) return ' estrela ou mais';
29
+ return ' estrelas ou mais';
30
+ }
31
+
32
+ export function ratingLabel (n) {
33
+ n = Number(n);
34
+ return n === 0 ? 'Sem avaliações' : n + ratingSuffix(n);
35
+ }
36
+
24
37
  function isForwardable (key) {
25
38
  const lower = key.toLowerCase();
26
39
  return !THEME_PARAMS.includes(key) && !TRACKING_PARAMS.includes(lower) && lower.indexOf('utm_') !== 0;
@@ -66,10 +79,11 @@ function facetValues (facet, query, path) {
66
79
  const active = splitValues(query[facet.label]);
67
80
  return (facet.values || []).map((v, index) => {
68
81
  const value = String(v.value);
82
+ const label = facet.type === 'rating' ? ratingLabel(v.value) : value;
69
83
  return {
70
84
  id: index + 1,
71
- nome: value,
72
- label: value,
85
+ nome: label,
86
+ label,
73
87
  imagem: '',
74
88
  imagemOriginal: '',
75
89
  ordem: index,
@@ -109,7 +123,7 @@ export function toUappiListing (response, products, rawQuery, options) {
109
123
  return;
110
124
  }
111
125
  // Disponibilidade (0/1) não tem par na listagem da Uappi.
112
- if (facet.type !== 'terms' || facet.field === 'hasStock' || !(facet.values || []).length) return;
126
+ if ((facet.type !== 'terms' && facet.type !== 'rating') || facet.field === 'hasStock' || !(facet.values || []).length) return;
113
127
  const values = facetValues(facet, query, path);
114
128
  if (facet.field === 'brand.name') marcas.push(...values);
115
129
  else atributos.push({ id: index + 1, nome: facet.label, tipo: 'texto', campo: facet.field, valores: values });
@@ -217,7 +231,8 @@ export function mergeIntoUappiPage (base, listing) {
217
231
  export async function rocketCategoryListing ({ $rocket, categoryId, query, limit, base, path }) {
218
232
  const params = Object.assign(toRocketParams(query, limit), { categoryId });
219
233
  const response = await $rocket.search(params);
220
- return mergeIntoUappiPage(base, await withUappiProducts($rocket, response, query, { limit, path }));
234
+ const listing = mergeIntoUappiPage(base, await withUappiProducts($rocket, response, query, { limit, path }));
235
+ return Object.assign(listing, { rocketCategory: true });
221
236
  }
222
237
 
223
238
  /** Coleção do painel pelo slug. null quando a Rocket não tem essa coleção no ar. */
@@ -228,7 +243,8 @@ export async function rocketCollectionListing ({ $rocket, slug, query, limit, ba
228
243
  const response = await $rocket.collection(slug, params);
229
244
  if (!response) return null;
230
245
  const defaultSort = response.sortOptions && response.sortOptions[0] && response.sortOptions[0].id;
231
- return mergeIntoUappiPage(base, await withUappiProducts($rocket, response, query, { limit, path, defaultSort }));
246
+ const listing = mergeIntoUappiPage(base, await withUappiProducts($rocket, response, query, { limit, path, defaultSort }));
247
+ return Object.assign(listing, { rocketCollection: slug });
232
248
  }
233
249
 
234
250
  export function dispatchListingSearch ($rocket, listing) {
@@ -264,11 +280,26 @@ export function dispatchDidYouMeanClick ($rocket, { from, suggestion, source })
264
280
  }).catch(() => {});
265
281
  }
266
282
 
283
+ // Contrato de eventos (spec 082): com termo é busca; grade sem termo (categoria, catálogo) é
284
+ // 'category-page' — 'search-page' vazio inflava a receita "Busca".
285
+ export function gridClickMeta (query, categoryId, collection) {
286
+ const term = String(query || '').trim();
287
+ if (term) return { source: 'search-page', query: term };
288
+ const meta = { source: 'category-page' };
289
+ if (categoryId) meta.categoryId = String(categoryId);
290
+ if (collection) meta.collection = collection;
291
+ return meta;
292
+ }
293
+
294
+ // Só grade servida pela Rocket (marcada por rocketSearchListing / rocketCategoryListing /
295
+ // rocketCollectionListing): a listagem de reserva da Uappi não é clique Rocket.
267
296
  export function dispatchListingClick ($rocket, listing, product, position) {
297
+ if (!listing || !(listing.rocketSearch || listing.rocketCategory || listing.rocketCollection)) return;
268
298
  if (!product || product.id == null) return;
299
+ const categoria = listing.conteudo && listing.conteudo.categoria;
269
300
  $rocket.dispatchEvent('click', {
270
301
  productId: String(product.id),
271
302
  position,
272
- meta: { source: 'search-page', query: (listing && listing.rocketSearch && listing.rocketSearch.query) || '' }
303
+ meta: gridClickMeta(listing.rocketSearch && listing.rocketSearch.query, categoria && categoria.id, listing.rocketCollection)
273
304
  }).catch(() => {});
274
305
  }
package/runtime/rocket.js CHANGED
@@ -16,7 +16,11 @@ const SHOWCASE_ATTR_TTL = 30 * 60 * 1000;
16
16
  // Mapa persistente produto -> vitrine (last-touch por produto). Diferente do slot único,
17
17
  // NÃO é apagado no add_to_cart: sobrevive até o checkout para atribuir GMV no purchase.
18
18
  const SHOWCASE_ATTR_MAP_KEY = 'rs_showcase_attr_map';
19
- const SEARCH_ATTR_KEY = 'rs_search_attr';
19
+ // Mapa produto -> origem do último clique em grade/autocomplete (search-page, category-page,
20
+ // autocomplete). Por produto, não por sessão (spec 082): o slot único de antes fazia todo
21
+ // view/add_to_cart dos 30 min seguintes herdar a origem de um clique em outro produto.
22
+ const SEARCH_ATTR_MAP_KEY = 'rs_search_attr_map';
23
+ const SEARCH_ATTR_SOURCES = ['autocomplete', 'search-page', 'category-page'];
20
24
  const PENDING_CART_EVENTS_KEY = 'rs_pending_add_to_cart_events';
21
25
  const ANON_SESSION_KEY = 'rs_anon_session_id';
22
26
  const ANON_SESSION_TTL = 30 * 60 * 1000;
@@ -381,24 +385,26 @@ export function createRocket (config) {
381
385
  return attr;
382
386
  }
383
387
 
384
- function saveShowcaseAttributionEntry (showcaseId, productId, position) {
385
- if (!showcaseId || productId == null || productId === '') return;
386
- const map = storageGet(SHOWCASE_ATTR_MAP_KEY) || {};
388
+ // Last-touch por produto; poda as entradas expiradas para o mapa não crescer na sessão.
389
+ function saveAttributionEntry (key, productId, entry) {
390
+ if (productId == null || productId === '') return;
391
+ const map = storageGet(key) || {};
387
392
  const now = Date.now();
388
- Object.keys(map).forEach((key) => {
389
- if (!map[key] || !map[key].ts || now - map[key].ts > SHOWCASE_ATTR_TTL) delete map[key];
393
+ Object.keys(map).forEach((k) => {
394
+ if (!map[k] || !map[k].ts || now - map[k].ts > SHOWCASE_ATTR_TTL) delete map[k];
390
395
  });
391
- map[String(productId)] = { showcaseId, position: position != null && position >= 0 ? position : null, ts: now };
392
- storageSet(SHOWCASE_ATTR_MAP_KEY, map);
396
+ map[String(productId)] = Object.assign(entry, { ts: now });
397
+ storageSet(key, map);
393
398
  }
394
399
 
395
- function getSearchAttribution () {
396
- const attr = storageGet(SEARCH_ATTR_KEY);
397
- if (!attr || !attr.ts || Date.now() - attr.ts > SHOWCASE_ATTR_TTL) {
398
- storageRemove(SEARCH_ATTR_KEY);
399
- return null;
400
- }
401
- return attr;
400
+ function saveShowcaseAttributionEntry (showcaseId, productId, position) {
401
+ if (!showcaseId) return;
402
+ saveAttributionEntry(SHOWCASE_ATTR_MAP_KEY, productId, { showcaseId, position: position != null && position >= 0 ? position : null });
403
+ }
404
+
405
+ function getSearchAttribution (productId) {
406
+ const entry = productId != null ? (storageGet(SEARCH_ATTR_MAP_KEY) || {})[String(productId)] : null;
407
+ return entry && entry.ts && Date.now() - entry.ts <= SHOWCASE_ATTR_TTL ? entry : null;
402
408
  }
403
409
 
404
410
  rocket.getShowcaseAttribution = getShowcaseAttribution;
@@ -428,8 +434,8 @@ export function createRocket (config) {
428
434
  });
429
435
  saveShowcaseAttributionEntry(data.vitrine, data.productId, data.position);
430
436
  }
431
- if (eventType === 'click' && (data.meta.source === 'autocomplete' || data.meta.source === 'search-page')) {
432
- storageSet(SEARCH_ATTR_KEY, { source: data.meta.source, ts: Date.now() });
437
+ if (eventType === 'click' && SEARCH_ATTR_SOURCES.includes(data.meta.source)) {
438
+ saveAttributionEntry(SEARCH_ATTR_MAP_KEY, data.productId, { source: data.meta.source });
433
439
  }
434
440
 
435
441
  const sessionId = await rocket.getSessionId();
@@ -530,10 +536,8 @@ export function createRocket (config) {
530
536
  rocket.fireProductView = function () {
531
537
  if (!page.productId) return;
532
538
  const viewData = { productId: page.productId };
533
- const showcaseAttr = getShowcaseAttribution(page.productId);
534
- const searchAttr = getSearchAttribution();
535
- if (showcaseAttr && showcaseAttr.showcaseId) viewData.meta = { source: 'showcase:' + showcaseAttr.showcaseId };
536
- else if (searchAttr && searchAttr.source) viewData.meta = { source: searchAttr.source };
539
+ const source = resolveProductSource(page.productId);
540
+ if (source) viewData.meta = { source };
537
541
  rocket.dispatchEvent('view', viewData).catch(() => {});
538
542
  };
539
543
 
@@ -583,11 +587,13 @@ export function createRocket (config) {
583
587
  return cached != null ? cached : null;
584
588
  }
585
589
 
586
- function resolveProductSource (productId, attr, searchAttr) {
590
+ // Origem só com clique NESTE produto: vitrine ou grade/autocomplete. Produto que apenas
591
+ // apareceu na grade (searchPriceCache) não ganha origem.
592
+ function resolveProductSource (productId) {
593
+ const attr = getShowcaseAttribution(productId);
587
594
  if (attr && attr.showcaseId) return 'showcase:' + attr.showcaseId;
588
- if (searchAttr && searchAttr.source) return searchAttr.source;
589
- if (searchPriceCache[String(productId)] != null) return 'search-page';
590
- return null;
595
+ const searchAttr = getSearchAttribution(productId);
596
+ return searchAttr ? searchAttr.source : null;
591
597
  }
592
598
 
593
599
  rocket.trackAddToCart = function (payload) {
@@ -602,7 +608,6 @@ export function createRocket (config) {
602
608
  if (!products.length) return;
603
609
 
604
610
  const attr = getShowcaseAttribution(products[0].productId);
605
- const searchAttr = getSearchAttribution();
606
611
  const experiment = attr && attr.experimentId != null
607
612
  ? { experimentId: attr.experimentId, variantId: attr.variantId != null ? attr.variantId : null }
608
613
  : {};
@@ -611,7 +616,7 @@ export function createRocket (config) {
611
616
  const p = products[0];
612
617
  const meta = { quantity: p.quantity };
613
618
  const price = resolveProductPrice(p.productId, attr);
614
- const source = resolveProductSource(p.productId, attr, searchAttr);
619
+ const source = resolveProductSource(p.productId);
615
620
  if (source) meta.source = source;
616
621
  if (price != null) meta.totalValue = price * p.quantity;
617
622
  const eventData = Object.assign({ productId: p.productId, meta }, experiment);
@@ -624,7 +629,7 @@ export function createRocket (config) {
624
629
  if (p.skuId) item.skuId = p.skuId;
625
630
  const price = resolveProductPrice(p.productId, attr);
626
631
  if (price != null) item.totalValue = price * p.quantity;
627
- const source = resolveProductSource(p.productId, attr, searchAttr);
632
+ const source = resolveProductSource(p.productId);
628
633
  if (source) item.source = source;
629
634
  if (attr && attr.showcaseId && String(attr.productId) === String(p.productId)) item.showcaseId = Number(attr.showcaseId);
630
635
  return item;