@pradip1995/segment-powerhouse-shop 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 +1 -1
- package/src/product-card.tsx +37 -7
- package/src/shop.css +6 -0
- package/src/wishlist-button.tsx +11 -4
package/package.json
CHANGED
package/src/product-card.tsx
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { useState, type MouseEvent } from "react"
|
|
4
4
|
import { useRouter } from "next/navigation"
|
|
5
|
+
import { buyNow } from "@pradip1995/commerce-core/client/actions/cart"
|
|
5
6
|
import { addToCart } from "./add-to-cart"
|
|
6
7
|
import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
|
|
7
8
|
import { formatPrice } from "@pradip1995/segment-primitives/format-price"
|
|
@@ -33,6 +34,7 @@ export default function ShopProductCard({
|
|
|
33
34
|
}) {
|
|
34
35
|
const router = useRouter()
|
|
35
36
|
const [adding, setAdding] = useState(false)
|
|
37
|
+
const [buying, setBuying] = useState(false)
|
|
36
38
|
const variants = product.variants ?? []
|
|
37
39
|
const variant = variants[0]
|
|
38
40
|
const price = variant?.calculated_price?.calculated_amount
|
|
@@ -50,11 +52,12 @@ export default function ShopProductCard({
|
|
|
50
52
|
const hasOptions = variants.length > 1
|
|
51
53
|
const copy = snippet(product.description, layout === "list" ? 220 : 140)
|
|
52
54
|
const isList = layout === "list"
|
|
55
|
+
const busy = adding || buying
|
|
53
56
|
|
|
54
57
|
const handleAdd = async (event: MouseEvent) => {
|
|
55
58
|
event.preventDefault()
|
|
56
59
|
event.stopPropagation()
|
|
57
|
-
if (!variant?.id ||
|
|
60
|
+
if (!variant?.id || busy || !inStock || hasOptions) return
|
|
58
61
|
setAdding(true)
|
|
59
62
|
try {
|
|
60
63
|
await addToCart({
|
|
@@ -73,6 +76,26 @@ export default function ShopProductCard({
|
|
|
73
76
|
}
|
|
74
77
|
}
|
|
75
78
|
|
|
79
|
+
const handleBuyNow = async (event: MouseEvent) => {
|
|
80
|
+
event.preventDefault()
|
|
81
|
+
event.stopPropagation()
|
|
82
|
+
if (!variant?.id || busy || !inStock) return
|
|
83
|
+
setBuying(true)
|
|
84
|
+
try {
|
|
85
|
+
await buyNow({
|
|
86
|
+
variantId: variant.id,
|
|
87
|
+
quantity: 1,
|
|
88
|
+
countryCode,
|
|
89
|
+
})
|
|
90
|
+
router.refresh()
|
|
91
|
+
} catch (err) {
|
|
92
|
+
if (isNextRedirect(err)) throw err
|
|
93
|
+
const message = err instanceof Error ? err.message : "Buy now failed"
|
|
94
|
+
window.dispatchEvent(new CustomEvent("cart:item-added", { detail: { message, variant: "error" } }))
|
|
95
|
+
setBuying(false)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
76
99
|
const priceBlock =
|
|
77
100
|
price != null ? (
|
|
78
101
|
<div className="powerhouse-shop__price">
|
|
@@ -83,13 +106,20 @@ export default function ShopProductCard({
|
|
|
83
106
|
</div>
|
|
84
107
|
) : null
|
|
85
108
|
|
|
109
|
+
const buyNowControl = (
|
|
110
|
+
<button
|
|
111
|
+
type="button"
|
|
112
|
+
className="powerhouse-shop__quickshop"
|
|
113
|
+
onClick={handleBuyNow}
|
|
114
|
+
disabled={busy || !variant?.id}
|
|
115
|
+
>
|
|
116
|
+
{buying ? "Processing…" : "Buy now"}
|
|
117
|
+
</button>
|
|
118
|
+
)
|
|
119
|
+
|
|
86
120
|
const actionBlock = (
|
|
87
121
|
<div className="powerhouse-shop__actions">
|
|
88
|
-
{inStock ?
|
|
89
|
-
<LocalizedLink href={href} countryCode={countryCode} className="powerhouse-shop__quickshop">
|
|
90
|
-
Quick shop
|
|
91
|
-
</LocalizedLink>
|
|
92
|
-
) : null}
|
|
122
|
+
{inStock ? buyNowControl : null}
|
|
93
123
|
{!inStock ? (
|
|
94
124
|
<button type="button" className="powerhouse-shop__atc" disabled>
|
|
95
125
|
Sold out
|
|
@@ -103,7 +133,7 @@ export default function ShopProductCard({
|
|
|
103
133
|
type="button"
|
|
104
134
|
className="powerhouse-shop__atc"
|
|
105
135
|
onClick={handleAdd}
|
|
106
|
-
disabled={
|
|
136
|
+
disabled={busy || !variant?.id}
|
|
107
137
|
>
|
|
108
138
|
{adding ? "Adding…" : "Add to cart"}
|
|
109
139
|
</button>
|
package/src/shop.css
CHANGED
|
@@ -750,6 +750,12 @@ html.powerhouse-filters-open body {
|
|
|
750
750
|
border: 1px solid #f2f682;
|
|
751
751
|
background: #f1eeae;
|
|
752
752
|
color: #1d1d1d;
|
|
753
|
+
cursor: pointer;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
.powerhouse-shop__quickshop:disabled {
|
|
757
|
+
opacity: 0.55;
|
|
758
|
+
cursor: not-allowed;
|
|
753
759
|
}
|
|
754
760
|
|
|
755
761
|
.powerhouse-shop__atc {
|
package/src/wishlist-button.tsx
CHANGED
|
@@ -7,7 +7,10 @@ import {
|
|
|
7
7
|
getWishlistProductIds,
|
|
8
8
|
removeFromWishlist,
|
|
9
9
|
} from "@pradip1995/commerce-core/client/actions/wishlist"
|
|
10
|
-
import LoginRequiredModal
|
|
10
|
+
import LoginRequiredModal, {
|
|
11
|
+
isAuthRequiredError,
|
|
12
|
+
promptLoginForWishlist,
|
|
13
|
+
} from "@pradip1995/segment-login-popup/required"
|
|
11
14
|
|
|
12
15
|
type WishlistButtonProps = {
|
|
13
16
|
productId?: string | null
|
|
@@ -60,9 +63,13 @@ export default function WishlistButton({
|
|
|
60
63
|
return
|
|
61
64
|
}
|
|
62
65
|
|
|
63
|
-
if (result.error
|
|
64
|
-
setLoginRequired(true)
|
|
65
|
-
|
|
66
|
+
if (isAuthRequiredError(result.error)) {
|
|
67
|
+
promptLoginForWishlist(() => setLoginRequired(true))
|
|
68
|
+
}
|
|
69
|
+
} catch (error) {
|
|
70
|
+
const message = error instanceof Error ? error.message : ""
|
|
71
|
+
if (isAuthRequiredError(message) || !message) {
|
|
72
|
+
promptLoginForWishlist(() => setLoginRequired(true))
|
|
66
73
|
}
|
|
67
74
|
} finally {
|
|
68
75
|
setLoading(false)
|