@pradip1995/segment-powerhouse-shop 0.1.1 → 0.1.3

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": "@pradip1995/segment-powerhouse-shop",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -0,0 +1,24 @@
1
+ "use server"
2
+
3
+ import { addToCart as addToCartCore } from "@pradip1995/commerce-core/data/cart"
4
+ import { removeAuthToken, removeCartId } from "@pradip1995/commerce-core/data/cookies"
5
+
6
+ function isStaleCustomerError(err: unknown) {
7
+ const message = err instanceof Error ? err.message : String(err)
8
+ return /customer with id/i.test(message) && /not found/i.test(message)
9
+ }
10
+
11
+ export async function addToCart(input: {
12
+ variantId: string
13
+ quantity: number
14
+ countryCode: string
15
+ }) {
16
+ try {
17
+ await addToCartCore(input)
18
+ } catch (err) {
19
+ if (!isStaleCustomerError(err)) throw err
20
+ await removeAuthToken()
21
+ await removeCartId()
22
+ await addToCartCore(input)
23
+ }
24
+ }
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { useState, type MouseEvent } from "react"
4
4
  import { useRouter } from "next/navigation"
5
- import { addToCart } from "@pradip1995/commerce-core/client/actions/cart"
5
+ import { addToCart } from "./add-to-cart"
6
6
  import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
7
7
  import { formatPrice } from "@pradip1995/segment-primitives/format-price"
8
8
  import { isNextRedirect } from "@pradip1995/segment-primitives/is-next-redirect"
@@ -22,9 +22,11 @@ function snippet(value?: string | null, length = 140) {
22
22
  export default function ShopProductCard({
23
23
  product,
24
24
  countryCode,
25
+ layout = "grid",
25
26
  }: {
26
27
  product: HttpTypes.StoreProduct
27
28
  countryCode: string
29
+ layout?: "grid" | "list"
28
30
  }) {
29
31
  const router = useRouter()
30
32
  const [adding, setAdding] = useState(false)
@@ -43,7 +45,8 @@ export default function ShopProductCard({
43
45
  const lowStock = inventory != null && inventory > 0 && inventory <= 5
44
46
  const href = `/products/${product.handle}`
45
47
  const hasOptions = variants.length > 1
46
- const copy = snippet(product.description)
48
+ const copy = snippet(product.description, layout === "list" ? 220 : 140)
49
+ const isList = layout === "list"
47
50
 
48
51
  const handleAdd = async (event: MouseEvent) => {
49
52
  event.preventDefault()
@@ -67,8 +70,46 @@ export default function ShopProductCard({
67
70
  }
68
71
  }
69
72
 
73
+ const priceBlock =
74
+ price != null ? (
75
+ <div className="powerhouse-shop__price">
76
+ {onSale && originalPrice != null ? (
77
+ <span className="powerhouse-shop__compare">{formatPrice(originalPrice, currency)}</span>
78
+ ) : null}
79
+ <span className="powerhouse-shop__current">{formatPrice(price, currency)}</span>
80
+ </div>
81
+ ) : null
82
+
83
+ const actionBlock = (
84
+ <div className="powerhouse-shop__actions">
85
+ {inStock ? (
86
+ <LocalizedLink href={href} countryCode={countryCode} className="powerhouse-shop__quickshop">
87
+ Quick shop
88
+ </LocalizedLink>
89
+ ) : null}
90
+ {!inStock ? (
91
+ <button type="button" className="powerhouse-shop__atc" disabled>
92
+ Sold out
93
+ </button>
94
+ ) : hasOptions ? (
95
+ <LocalizedLink href={href} countryCode={countryCode} className="powerhouse-shop__atc">
96
+ Choose options
97
+ </LocalizedLink>
98
+ ) : (
99
+ <button
100
+ type="button"
101
+ className="powerhouse-shop__atc"
102
+ onClick={handleAdd}
103
+ disabled={adding || !variant?.id}
104
+ >
105
+ {adding ? "Adding…" : "Add to cart"}
106
+ </button>
107
+ )}
108
+ </div>
109
+ )
110
+
70
111
  return (
71
- <article className={`powerhouse-shop__card${onSale ? " is-sale" : ""}`}>
112
+ <article className={`powerhouse-shop__card${onSale ? " is-sale" : ""}${isList ? " is-list-card" : ""}`}>
72
113
  <LocalizedLink href={href} countryCode={countryCode} className="powerhouse-shop__media" tabIndex={-1}>
73
114
  {product.thumbnail?.trim() ? (
74
115
  <img src={product.thumbnail.trim()} alt={product.title || ""} />
@@ -78,55 +119,50 @@ export default function ShopProductCard({
78
119
  {onSale && saved != null ? (
79
120
  <span className="powerhouse-shop__badge">Save {formatPrice(saved, currency)}</span>
80
121
  ) : null}
81
- {!inStock ? <span className="powerhouse-shop__sold">Sold out</span> : null}
122
+ {!inStock && !isList ? <span className="powerhouse-shop__sold">Sold out</span> : null}
82
123
  </LocalizedLink>
83
124
 
84
- <div className="powerhouse-shop__info">
85
- <h2 className="powerhouse-shop__name">
86
- <LocalizedLink href={href} countryCode={countryCode}>
87
- {product.title}
88
- </LocalizedLink>
89
- </h2>
90
-
91
- {lowStock ? (
92
- <p className="powerhouse-shop__stock">Only {inventory} left!</p>
93
- ) : inStock ? (
94
- <p className="powerhouse-shop__stock is-ok">in stock</p>
95
- ) : (
96
- <p className="powerhouse-shop__stock is-out">Out of stock</p>
97
- )}
98
-
99
- {copy ? <p className="powerhouse-shop__excerpt">{copy}</p> : null}
100
-
101
- {price != null ? (
102
- <div className="powerhouse-shop__price">
103
- {onSale && originalPrice != null ? (
104
- <span className="powerhouse-shop__compare">{formatPrice(originalPrice, currency)}</span>
125
+ {isList ? (
126
+ <>
127
+ <div className="powerhouse-shop__copy">
128
+ <h2 className="powerhouse-shop__name">
129
+ <LocalizedLink href={href} countryCode={countryCode}>
130
+ {product.title}
131
+ </LocalizedLink>
132
+ </h2>
133
+ {lowStock ? (
134
+ <p className="powerhouse-shop__stock">Only {inventory} left!</p>
135
+ ) : !inStock ? (
136
+ <p className="powerhouse-shop__stock is-out">Out of stock</p>
105
137
  ) : null}
106
- <span className="powerhouse-shop__current">{formatPrice(price, currency)}</span>
138
+ {copy ? <p className="powerhouse-shop__excerpt">{copy}</p> : null}
139
+ <LocalizedLink href={href} countryCode={countryCode} className="powerhouse-shop__details-link">
140
+ View full details
141
+ </LocalizedLink>
107
142
  </div>
108
- ) : null}
109
-
110
- <div className="powerhouse-shop__actions">
111
- <LocalizedLink href={href} countryCode={countryCode} className="powerhouse-shop__quickshop">
112
- Quick shop
113
- </LocalizedLink>
114
- {hasOptions ? (
115
- <LocalizedLink href={href} countryCode={countryCode} className="powerhouse-shop__atc">
116
- Choose options
143
+ <div className="powerhouse-shop__aside">
144
+ {priceBlock}
145
+ {actionBlock}
146
+ </div>
147
+ </>
148
+ ) : (
149
+ <div className="powerhouse-shop__info">
150
+ {priceBlock}
151
+ <h2 className="powerhouse-shop__name">
152
+ <LocalizedLink href={href} countryCode={countryCode}>
153
+ {product.title}
117
154
  </LocalizedLink>
155
+ </h2>
156
+ {lowStock ? (
157
+ <p className="powerhouse-shop__stock">Only {inventory} left!</p>
158
+ ) : inStock ? (
159
+ <p className="powerhouse-shop__stock is-ok">in stock</p>
118
160
  ) : (
119
- <button
120
- type="button"
121
- className="powerhouse-shop__atc"
122
- onClick={handleAdd}
123
- disabled={adding || !variant?.id || !inStock}
124
- >
125
- {adding ? "Adding…" : !inStock ? "Sold out" : "Add to cart"}
126
- </button>
161
+ <p className="powerhouse-shop__stock is-out">Out of stock</p>
127
162
  )}
163
+ {actionBlock}
128
164
  </div>
129
- </div>
165
+ )}
130
166
  </article>
131
167
  )
132
168
  }