@nuvler/cli 0.5.16 → 0.5.17

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.
@@ -1,10 +1,23 @@
1
1
  import type { ProductDetailProps, ThemeImage } from "@nuvler/theme-sdk";
2
2
 
3
3
  import { ProductDetailInteractive } from "./product-detail-interactive.js";
4
- import { displayPrice, ThemeImageView, type ThemeImageComponent } from "./shared.js";
4
+ import {
5
+ displayPrice,
6
+ ThemeImageView,
7
+ type ThemeImageComponent,
8
+ } from "./shared.js";
5
9
 
6
- function discountPercent(price: number | null, promotionalPrice: number | null) {
7
- if (price === null || promotionalPrice === null || promotionalPrice >= price || price <= 0) return null;
10
+ function discountPercent(
11
+ price: number | null,
12
+ promotionalPrice: number | null,
13
+ ) {
14
+ if (
15
+ price === null ||
16
+ promotionalPrice === null ||
17
+ promotionalPrice >= price ||
18
+ price <= 0
19
+ )
20
+ return null;
8
21
  return Math.round((1 - promotionalPrice / price) * 100);
9
22
  }
10
23
 
@@ -28,47 +41,56 @@ export function ProductDetail({
28
41
  if (image?.url) uniqueImages.set(image.url, image);
29
42
  });
30
43
  const images = [...uniqueImages.values()];
31
- const galleryImages = images.length > 0
32
- ? images.map((image, index) => (
33
- <span
34
- data-product-main-image={image.url}
35
- hidden={index !== 0}
36
- key={image.url}
37
- >
38
- <ImageComponent
39
- image={image}
40
- fallbackAlt={initialVariant?.name || product.title}
41
- priority={index === 0}
42
- sizes="(max-width: 900px) 100vw, 50vw"
43
- fallbackWidth={1000}
44
- fallbackHeight={1000}
45
- />
44
+ const galleryImages =
45
+ images.length > 0 ? (
46
+ images.map((image, index) => (
47
+ <span
48
+ data-product-main-image={image.url}
49
+ hidden={index !== 0}
50
+ key={image.url}
51
+ >
52
+ <ImageComponent
53
+ image={image}
54
+ fallbackAlt={initialVariant?.name || product.title}
55
+ priority={index === 0}
56
+ sizes="(max-width: 900px) 100vw, 50vw"
57
+ fallbackWidth={1000}
58
+ fallbackHeight={1000}
59
+ />
60
+ </span>
61
+ ))
62
+ ) : (
63
+ <span key="empty-image" data-product-main-image="">
64
+ <ImageComponent image={null} fallbackAlt={product.title} />
46
65
  </span>
47
- ))
48
- : <span data-product-main-image=""><ImageComponent image={null} fallbackAlt={product.title} /></span>;
49
- const galleryThumbnails = images.length > 1
50
- ? images.map((image, index) => (
51
- <button
52
- className={index === 0 ? "active" : ""}
53
- type="button"
54
- aria-pressed={index === 0}
55
- aria-label={`Exibir imagem ${index + 1} de ${product.title}`}
56
- data-product-thumbnail={image.url}
57
- key={`${image.url}-${index}`}
58
- >
59
- <ImageComponent
60
- image={image}
61
- fallbackAlt={`${product.title} ${index + 1}`}
62
- sizes="96px"
63
- fallbackWidth={192}
64
- fallbackHeight={192}
65
- />
66
- </button>
67
- ))
68
- : null;
69
- const previousPrice = product.promotionalPrice !== null && product.price !== null && product.promotionalPrice < product.price
70
- ? displayPrice({ ...product, promotionalPrice: null })
71
- : null;
66
+ );
67
+ const galleryThumbnails =
68
+ images.length > 1
69
+ ? images.map((image, index) => (
70
+ <button
71
+ className={index === 0 ? "active" : ""}
72
+ type="button"
73
+ aria-pressed={index === 0}
74
+ aria-label={`Exibir imagem ${index + 1} de ${product.title}`}
75
+ data-product-thumbnail={image.url}
76
+ key={`${image.url}-${index}`}
77
+ >
78
+ <ImageComponent
79
+ image={image}
80
+ fallbackAlt={`${product.title} ${index + 1}`}
81
+ sizes="96px"
82
+ fallbackWidth={192}
83
+ fallbackHeight={192}
84
+ />
85
+ </button>
86
+ ))
87
+ : null;
88
+ const previousPrice =
89
+ product.promotionalPrice !== null &&
90
+ product.price !== null &&
91
+ product.promotionalPrice < product.price
92
+ ? displayPrice({ ...product, promotionalPrice: null })
93
+ : null;
72
94
  const currentPrice = displayPrice(product);
73
95
  const discount = discountPercent(product.price, product.promotionalPrice);
74
96
  const details = [
@@ -76,25 +98,85 @@ export function ProductDetail({
76
98
  product.model && { label: "Modelo", value: product.model },
77
99
  product.ean && { label: "EAN", value: product.ean },
78
100
  product.sku && { label: "Referência", value: product.sku },
79
- ].filter((detail): detail is { label: string; value: string } => Boolean(detail));
80
- const stockLabel = product.stockQuantity === 0
81
- ? "Indisponível no momento"
82
- : product.stockQuantity !== null
83
- ? "Disponível"
84
- : null;
85
- const information = <>
86
- {product.categories[0] && <span className="product-category">{product.categories[0].name}</span>}
87
- <h1>{product.title}</h1>
88
- {showPrices && <div className="product-price-block">{previousPrice && <s>De: {previousPrice}</s>}{currentPrice ? <strong>{previousPrice ? "por " : ""}{currentPrice}</strong> : <span>Consulte condições com a loja</span>}{discount && <small>{discount}% de desconto</small>}</div>}
89
- {stockLabel && <p className={`product-availability${product.stockQuantity === 0 ? " unavailable" : ""}`}>{stockLabel}</p>}
90
- {product.sku && <p className="product-reference">Ref. {product.sku}</p>}
91
- <div className="product-description">{descriptionContent}</div>
92
- {details.length > 0 && <section className="product-specifications" aria-labelledby="product-specifications-title"><h2 id="product-specifications-title">Informações do produto</h2><dl>{details.map((detail) => <div key={detail.label}><dt>{detail.label}</dt><dd>{detail.value}</dd></div>)}</dl></section>}
93
- </>;
94
- const benefits = (contact?.whatsapp || contact?.email || contact?.phone) ? <div className="purchase-benefits"><span><strong>Precisa de ajuda?</strong> Fale com a loja para tirar dúvidas sobre este produto.</span></div> : null;
101
+ ].filter((detail): detail is { label: string; value: string } =>
102
+ Boolean(detail),
103
+ );
104
+ const stockLabel =
105
+ product.stockQuantity === 0
106
+ ? "Indisponível no momento"
107
+ : product.stockQuantity !== null
108
+ ? "Disponível"
109
+ : null;
110
+ const information = (
111
+ <>
112
+ {product.categories[0] && (
113
+ <span key="category" className="product-category">
114
+ {product.categories[0].name}
115
+ </span>
116
+ )}
117
+ <h1 key="title">{product.title}</h1>
118
+ {showPrices && (
119
+ <div key="price" className="product-price-block">
120
+ {previousPrice && <s>De: {previousPrice}</s>}
121
+ {currentPrice ? (
122
+ <strong>
123
+ {previousPrice ? "por " : ""}
124
+ {currentPrice}
125
+ </strong>
126
+ ) : (
127
+ <span>Consulte condições com a loja</span>
128
+ )}
129
+ {discount && <small>{discount}% de desconto</small>}
130
+ </div>
131
+ )}
132
+ {stockLabel && (
133
+ <p
134
+ key="availability"
135
+ className={`product-availability${product.stockQuantity === 0 ? " unavailable" : ""}`}
136
+ >
137
+ {stockLabel}
138
+ </p>
139
+ )}
140
+ {product.sku && (
141
+ <p key="reference" className="product-reference">
142
+ Ref. {product.sku}
143
+ </p>
144
+ )}
145
+ <div key="description" className="product-description">
146
+ {descriptionContent}
147
+ </div>
148
+ {details.length > 0 && (
149
+ <section
150
+ key="specifications"
151
+ className="product-specifications"
152
+ aria-labelledby="product-specifications-title"
153
+ >
154
+ <h2 id="product-specifications-title">Informações do produto</h2>
155
+ <dl>
156
+ {details.map((detail) => (
157
+ <div key={detail.label}>
158
+ <dt>{detail.label}</dt>
159
+ <dd>{detail.value}</dd>
160
+ </div>
161
+ ))}
162
+ </dl>
163
+ </section>
164
+ )}
165
+ </>
166
+ );
167
+ const benefits =
168
+ contact?.whatsapp || contact?.email || contact?.phone ? (
169
+ <div key="benefits" className="purchase-benefits">
170
+ <span>
171
+ <strong>Precisa de ajuda?</strong> Fale com a loja para tirar dúvidas
172
+ sobre este produto.
173
+ </span>
174
+ </div>
175
+ ) : null;
95
176
 
96
177
  return (
97
178
  <ProductDetailInteractive
179
+ key={product.slug}
98
180
  product={product}
99
181
  showPrices={showPrices}
100
182
  contact={contact}
@@ -1,13 +1,38 @@
1
- import { productUrl, type ProductGridProps, type ThemeProduct } from "@nuvler/theme-sdk";
1
+ import {
2
+ productUrl,
3
+ type ProductGridProps,
4
+ type ThemeProduct,
5
+ } from "@nuvler/theme-sdk";
2
6
  import { ProductCardAction } from "./product-detail-interactive.js";
3
- import { displayPrice, effectivePrice, plainText, ThemeImageView, type ThemeImageComponent } from "./shared.js";
7
+ import {
8
+ displayPrice,
9
+ effectivePrice,
10
+ plainText,
11
+ ThemeImageView,
12
+ type ThemeImageComponent,
13
+ } from "./shared.js";
4
14
 
5
- function discountPercent(price: number | null, promotionalPrice: number | null) {
6
- if (price === null || promotionalPrice === null || promotionalPrice >= price || price <= 0) return null;
15
+ function discountPercent(
16
+ price: number | null,
17
+ promotionalPrice: number | null,
18
+ ) {
19
+ if (
20
+ price === null ||
21
+ promotionalPrice === null ||
22
+ promotionalPrice >= price ||
23
+ price <= 0
24
+ )
25
+ return null;
7
26
  return Math.round((1 - promotionalPrice / price) * 100);
8
27
  }
9
28
 
10
- export function ProductCard({ product, showPrices = true, showCategory = true, showDescription = true, ImageComponent = ThemeImageView }: {
29
+ export function ProductCard({
30
+ product,
31
+ showPrices = true,
32
+ showCategory = true,
33
+ showDescription = true,
34
+ ImageComponent = ThemeImageView,
35
+ }: {
11
36
  product: ThemeProduct;
12
37
  showPrices?: boolean;
13
38
  showCategory?: boolean;
@@ -18,13 +43,119 @@ export function ProductCard({ product, showPrices = true, showCategory = true, s
18
43
  const unavailable = product.stockQuantity === 0;
19
44
  const discount = discountPercent(product.price, product.promotionalPrice);
20
45
  const currentPrice = displayPrice(product);
21
- const previousPrice = product.promotionalPrice !== null && product.price !== null && product.promotionalPrice < product.price
22
- ? displayPrice({ ...product, promotionalPrice: null })
23
- : null;
24
- return <article className="product-card" data-unavailable={unavailable || undefined}><a className="product-card-image" href={href} aria-label={`Ver ${product.title}`}>{showPrices && discount && <span className="product-card-badge">-{discount}%</span>}{unavailable && <span className="product-card-stock">Indisponível</span>}<ImageComponent image={product.image} fallbackAlt={product.title} sizes="(max-width: 720px) 92vw, (max-width: 1100px) 45vw, 25vw" fallbackWidth={720} fallbackHeight={720} /></a><div className="product-card-content">{showCategory && <span className={`product-category${product.categories[0] ? "" : " product-category-empty"}`}>{product.categories[0]?.name || "Sem categoria"}</span>}<h3><a className="product-card-details-link" href={href}>{product.title}</a></h3>{showDescription && product.description && <p className="product-card-description">{plainText(product.description)}</p>}{showPrices && <div className="product-card-price">{previousPrice && <s>De: {previousPrice}</s>}{currentPrice ? <strong>{previousPrice ? "por " : ""}{currentPrice}</strong> : <span>Consulte condições</span>}</div>}{product.sku && <small className="product-card-sku">Ref. {product.sku}</small>}<ProductCardAction href={href} hasVariants={product.variantOptions.length > 0} unavailable={unavailable} item={{ productSlug: product.slug, productUrl: href, title: product.title, sku: product.sku, image: product.image, unitPrice: showPrices ? effectivePrice(product) : null }} /></div></article>;
46
+ const previousPrice =
47
+ product.promotionalPrice !== null &&
48
+ product.price !== null &&
49
+ product.promotionalPrice < product.price
50
+ ? displayPrice({ ...product, promotionalPrice: null })
51
+ : null;
52
+ return (
53
+ <article
54
+ className="product-card"
55
+ data-unavailable={unavailable || undefined}
56
+ >
57
+ <a
58
+ className="product-card-image"
59
+ href={href}
60
+ aria-label={`Ver ${product.title}`}
61
+ >
62
+ {showPrices && discount && (
63
+ <span className="product-card-badge">-{discount}%</span>
64
+ )}
65
+ {unavailable && (
66
+ <span className="product-card-stock">Indisponível</span>
67
+ )}
68
+ <ImageComponent
69
+ image={product.image}
70
+ fallbackAlt={product.title}
71
+ sizes="(max-width: 720px) 92vw, (max-width: 1100px) 45vw, 25vw"
72
+ fallbackWidth={720}
73
+ fallbackHeight={720}
74
+ />
75
+ </a>
76
+ <div className="product-card-content">
77
+ {showCategory && (
78
+ <span
79
+ className={`product-category${product.categories[0] ? "" : " product-category-empty"}`}
80
+ >
81
+ {product.categories[0]?.name || "Sem categoria"}
82
+ </span>
83
+ )}
84
+ <h3>
85
+ <a className="product-card-details-link" href={href}>
86
+ {product.title}
87
+ </a>
88
+ </h3>
89
+ {showDescription && product.description && (
90
+ <p className="product-card-description">
91
+ {plainText(product.description)}
92
+ </p>
93
+ )}
94
+ {showPrices && (
95
+ <div className="product-card-price">
96
+ {previousPrice && <s>De: {previousPrice}</s>}
97
+ {currentPrice ? (
98
+ <strong>
99
+ {previousPrice ? "por " : ""}
100
+ {currentPrice}
101
+ </strong>
102
+ ) : (
103
+ <span>Consulte condições</span>
104
+ )}
105
+ </div>
106
+ )}
107
+ {product.sku && (
108
+ <small className="product-card-sku">Ref. {product.sku}</small>
109
+ )}
110
+ <ProductCardAction
111
+ href={href}
112
+ hasVariants={
113
+ Boolean(product.kitGroups?.length || product.kitSize) ||
114
+ product.variantOptions.length > 0
115
+ }
116
+ unavailable={unavailable}
117
+ item={{
118
+ productSlug: product.slug,
119
+ productUrl: href,
120
+ title: product.title,
121
+ sku: product.sku,
122
+ image: product.image,
123
+ unitPrice: showPrices ? effectivePrice(product) : null,
124
+ }}
125
+ />
126
+ </div>
127
+ </article>
128
+ );
25
129
  }
26
130
 
27
- export function ProductGrid({ products, columns = 4, showPrices = true, showCategory = true, showDescription = true, ImageComponent = ThemeImageView }: ProductGridProps & { ImageComponent?: ThemeImageComponent }) {
28
- if (!products.length) return <div className="empty-state"><span>7</span><h2>Nenhum produto encontrado</h2><p>Novos itens aparecerão aqui assim que forem publicados.</p></div>;
29
- return <div className="product-grid" data-columns={columns}>{products.map((product) => <ProductCard key={product.slug} product={product} showPrices={showPrices} showCategory={showCategory} showDescription={showDescription} ImageComponent={ImageComponent} />)}</div>;
131
+ export function ProductGrid({
132
+ products,
133
+ columns = 4,
134
+ showPrices = true,
135
+ showCategory = true,
136
+ showDescription = true,
137
+ ImageComponent = ThemeImageView,
138
+ }: ProductGridProps & { ImageComponent?: ThemeImageComponent }) {
139
+ if (!products.length)
140
+ return (
141
+ <div className="empty-state">
142
+ <span>7</span>
143
+ <h2>Nenhum produto encontrado</h2>
144
+ <p>Novos itens aparecerão aqui assim que forem publicados.</p>
145
+ </div>
146
+ );
147
+ return (
148
+ <div className="product-grid" data-columns={columns}>
149
+ {products.map((product) => (
150
+ <ProductCard
151
+ key={product.slug}
152
+ product={product}
153
+ showPrices={showPrices}
154
+ showCategory={showCategory}
155
+ showDescription={showDescription}
156
+ ImageComponent={ImageComponent}
157
+ />
158
+ ))}
159
+ </div>
160
+ );
30
161
  }
@@ -9,6 +9,9 @@ import {
9
9
  } from "react";
10
10
  import {
11
11
  productUrl,
12
+ themeCartKey,
13
+ kitCompositionText,
14
+ verifyKitCart,
12
15
  type NuvlerStoreThemeDefinition,
13
16
  type ThemeCartItem,
14
17
  type ThemeCategory,
@@ -245,7 +248,7 @@ function readStoredCart(storageKey: string): PreviewCartItem[] {
245
248
  key?: unknown;
246
249
  quantity?: unknown;
247
250
  };
248
- const key = `${item.productSlug}::${item.variantId || "parent"}`;
251
+ const key = themeCartKey(item);
249
252
  const quantity =
250
253
  typeof item.quantity === "number" && Number.isFinite(item.quantity)
251
254
  ? Math.min(99, Math.max(1, Math.trunc(item.quantity)))
@@ -396,6 +399,7 @@ export function createNuvlerDefaultRuntime(
396
399
  }: NuvlerDefaultThemeProps) {
397
400
  const [cartItems, setCartItems] = useState<PreviewCartItem[]>([]);
398
401
  const [cartOpen, setCartOpen] = useState(false);
402
+ const [kitError, setKitError] = useState("");
399
403
  const [cartToast, setCartToast] = useState<string | null>(null);
400
404
  const [cartViewportHeight, setCartViewportHeight] = useState<number | null>(
401
405
  null,
@@ -507,7 +511,7 @@ export function createNuvlerDefaultRuntime(
507
511
  showPrices && typeof item.unitPrice === "number"
508
512
  ? `\nValor unitário: ${money(item.unitPrice)}`
509
513
  : "";
510
- return `${index + 1}. ${item.quantity}x ${item.title}${item.variantName ? ` (${item.variantName})` : ""}${item.sku ? ` — ref. ${item.sku}` : ""}${price}`;
514
+ return `${index + 1}. ${item.quantity}x ${item.title}${item.variantName ? ` (${item.variantName})` : ""}${item.sku ? ` — ref. ${item.sku}` : ""}${item.kitGroups?.length || item.kitSize ? `\n${kitCompositionText(item)}` : ""}${price}`;
511
515
  });
512
516
  const summary =
513
517
  showPrices && hasCompletePrices
@@ -521,7 +525,7 @@ export function createNuvlerDefaultRuntime(
521
525
  ? `https://wa.me/${whatsapp}?text=${encodeURIComponent(cartMessage)}`
522
526
  : null;
523
527
  const addToCart = (item: ThemeCartItem) => {
524
- const key = `${item.productSlug}::${item.variantId || "parent"}`;
528
+ const key = themeCartKey(item);
525
529
  setCartItems((current) => {
526
530
  const existing = current.find((candidate) => candidate.key === key);
527
531
  if (existing)
@@ -950,6 +954,9 @@ export function createNuvlerDefaultRuntime(
950
954
  <div>
951
955
  <strong>{item.title}</strong>
952
956
  {item.variantName && <small>{item.variantName}</small>}
957
+ {(item.kitGroups?.length || item.kitSize) && (
958
+ <small>{kitCompositionText(item)}</small>
959
+ )}
953
960
  {item.sku && <small>Ref. {item.sku}</small>}
954
961
  {showPrices && typeof item.unitPrice === "number" && (
955
962
  <b>{money(item.unitPrice)}</b>
@@ -1018,6 +1025,7 @@ export function createNuvlerDefaultRuntime(
1018
1025
  </div>
1019
1026
  {cartItems.length > 0 && (
1020
1027
  <footer className="cart-footer">
1028
+ {kitError && <p role="alert">{kitError}</p>}
1021
1029
  {showPrices && hasCompletePrices && (
1022
1030
  <div>
1023
1031
  <span>Total estimado</span>
@@ -1031,6 +1039,24 @@ export function createNuvlerDefaultRuntime(
1031
1039
  <a
1032
1040
  className="button button-primary button-block"
1033
1041
  href={checkoutUrl}
1042
+ onClick={async (event) => {
1043
+ if (
1044
+ !cartItems.some(
1045
+ (item) => item.kitGroups?.length || item.kitSize,
1046
+ )
1047
+ )
1048
+ return;
1049
+ event.preventDefault();
1050
+ try {
1051
+ await verifyKitCart(cartItems);
1052
+ setKitError("");
1053
+ window.location.assign(checkoutUrl);
1054
+ } catch {
1055
+ setKitError(
1056
+ "Não foi possível confirmar os kits. Revise os sabores e quantidades ou tente novamente.",
1057
+ );
1058
+ }
1059
+ }}
1034
1060
  target="_blank"
1035
1061
  rel="noreferrer"
1036
1062
  >