@pradip1995/segment-powerhouse-deals 0.1.3 → 0.1.4
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 +1 -1
- package/src/deal-card.tsx +37 -7
- package/src/deals.css +7 -1
package/package.json
CHANGED
package/src/deal-card.tsx
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { useState } from "react"
|
|
4
4
|
import { useRouter } from "next/navigation"
|
|
5
|
-
import { addToCart } from "@pradip1995/commerce-core/client/actions/cart"
|
|
5
|
+
import { addToCart, buyNow } from "@pradip1995/commerce-core/client/actions/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"
|
|
@@ -19,6 +19,7 @@ type Props = {
|
|
|
19
19
|
export default function DealCard({ product, region, countryCode, wishlistProductIds }: Props) {
|
|
20
20
|
const router = useRouter()
|
|
21
21
|
const [adding, setAdding] = useState(false)
|
|
22
|
+
const [buying, setBuying] = useState(false)
|
|
22
23
|
const variant = product.variants?.[0]
|
|
23
24
|
const price = variant?.calculated_price?.calculated_amount
|
|
24
25
|
const originalPrice = variant?.calculated_price?.original_amount
|
|
@@ -32,17 +33,19 @@ export default function DealCard({ product, region, countryCode, wishlistProduct
|
|
|
32
33
|
(inventory ?? 0) > 0
|
|
33
34
|
const href = `/products/${product.handle}`
|
|
34
35
|
const hasOptions = (product.variants?.length ?? 0) > 1
|
|
36
|
+
const busy = adding || buying
|
|
37
|
+
const code = countryCode.toLowerCase()
|
|
35
38
|
|
|
36
39
|
const handleAdd = async (event: React.MouseEvent) => {
|
|
37
40
|
event.preventDefault()
|
|
38
41
|
event.stopPropagation()
|
|
39
|
-
if (!variant?.id ||
|
|
42
|
+
if (!variant?.id || busy || !inStock || hasOptions) return
|
|
40
43
|
setAdding(true)
|
|
41
44
|
try {
|
|
42
45
|
await addToCart({
|
|
43
46
|
variantId: variant.id,
|
|
44
47
|
quantity: 1,
|
|
45
|
-
countryCode:
|
|
48
|
+
countryCode: code,
|
|
46
49
|
})
|
|
47
50
|
window.dispatchEvent(new CustomEvent("cart:item-added", { detail: { message: "Added to cart" } }))
|
|
48
51
|
router.refresh()
|
|
@@ -55,6 +58,26 @@ export default function DealCard({ product, region, countryCode, wishlistProduct
|
|
|
55
58
|
}
|
|
56
59
|
}
|
|
57
60
|
|
|
61
|
+
const handleBuyNow = async (event: React.MouseEvent) => {
|
|
62
|
+
event.preventDefault()
|
|
63
|
+
event.stopPropagation()
|
|
64
|
+
if (!variant?.id || busy || !inStock) return
|
|
65
|
+
setBuying(true)
|
|
66
|
+
try {
|
|
67
|
+
await buyNow({
|
|
68
|
+
variantId: variant.id,
|
|
69
|
+
quantity: 1,
|
|
70
|
+
countryCode: code,
|
|
71
|
+
})
|
|
72
|
+
router.refresh()
|
|
73
|
+
} catch (err) {
|
|
74
|
+
if (isNextRedirect(err)) throw err
|
|
75
|
+
const message = err instanceof Error ? err.message : "Buy now failed"
|
|
76
|
+
window.dispatchEvent(new CustomEvent("cart:item-added", { detail: { message, variant: "error" } }))
|
|
77
|
+
setBuying(false)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
58
81
|
return (
|
|
59
82
|
<article className={`powerhouse-deals__card${onSale ? " is-sale" : ""}`}>
|
|
60
83
|
<div className="powerhouse-deals__media-wrap">
|
|
@@ -101,9 +124,16 @@ export default function DealCard({ product, region, countryCode, wishlistProduct
|
|
|
101
124
|
)}
|
|
102
125
|
|
|
103
126
|
<div className="powerhouse-deals__actions">
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
127
|
+
{inStock ? (
|
|
128
|
+
<button
|
|
129
|
+
type="button"
|
|
130
|
+
className="powerhouse-deals__quickshop"
|
|
131
|
+
onClick={handleBuyNow}
|
|
132
|
+
disabled={busy || !variant?.id}
|
|
133
|
+
>
|
|
134
|
+
{buying ? "Processing…" : "Buy now"}
|
|
135
|
+
</button>
|
|
136
|
+
) : null}
|
|
107
137
|
{hasOptions ? (
|
|
108
138
|
<LocalizedLink href={href} countryCode={countryCode} className="powerhouse-deals__atc">
|
|
109
139
|
Choose options
|
|
@@ -113,7 +143,7 @@ export default function DealCard({ product, region, countryCode, wishlistProduct
|
|
|
113
143
|
type="button"
|
|
114
144
|
className="powerhouse-deals__atc"
|
|
115
145
|
onClick={handleAdd}
|
|
116
|
-
disabled={
|
|
146
|
+
disabled={busy || !variant?.id || !inStock}
|
|
117
147
|
>
|
|
118
148
|
{adding ? "Adding…" : !inStock ? "Sold out" : "Add to cart"}
|
|
119
149
|
</button>
|
package/src/deals.css
CHANGED
|
@@ -324,9 +324,15 @@
|
|
|
324
324
|
background: #f1eeae;
|
|
325
325
|
border: 1px solid #f2f682;
|
|
326
326
|
color: #1d1d1d;
|
|
327
|
+
cursor: pointer;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
.powerhouse-deals__quickshop:disabled {
|
|
331
|
+
opacity: 0.55;
|
|
332
|
+
cursor: not-allowed;
|
|
327
333
|
}
|
|
328
334
|
|
|
329
|
-
.powerhouse-deals__quickshop:hover {
|
|
335
|
+
.powerhouse-deals__quickshop:hover:not(:disabled) {
|
|
330
336
|
background: #f4f1be;
|
|
331
337
|
}
|
|
332
338
|
|