@pradip1995/segment-powerhouse-cart 0.1.1 → 0.1.2

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-cart",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -26,6 +26,7 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@pradip1995/segment-analytics": "^0.2.4",
29
+ "@pradip1995/segment-login-popup": "^0.1.3",
29
30
  "@pradip1995/segment-primitives": "^0.4.0"
30
31
  },
31
32
  "devDependencies": {
package/src/cart.css CHANGED
@@ -411,3 +411,80 @@
411
411
  text-align: center;
412
412
  }
413
413
  }
414
+
415
+ .powerhouse-cart-confirm {
416
+ position: fixed;
417
+ inset: 0;
418
+ z-index: 80;
419
+ display: flex;
420
+ align-items: center;
421
+ justify-content: center;
422
+ padding: 1rem;
423
+ }
424
+
425
+ .powerhouse-cart-confirm__backdrop {
426
+ position: absolute;
427
+ inset: 0;
428
+ border: 0;
429
+ background: rgba(10, 10, 10, 0.45);
430
+ }
431
+
432
+ .powerhouse-cart-confirm__panel {
433
+ position: relative;
434
+ width: min(26rem, 100%);
435
+ padding: 1.5rem;
436
+ background: #ffffff;
437
+ border: 1px solid #e8e8e8;
438
+ }
439
+
440
+ .powerhouse-cart-confirm__title {
441
+ margin: 0 0 0.5rem;
442
+ font-family: var(--font-heading, Georgia, serif);
443
+ font-size: 1.35rem;
444
+ color: #1d1d1d;
445
+ }
446
+
447
+ .powerhouse-cart-confirm__text {
448
+ margin: 0 0 1.25rem;
449
+ font-size: 0.875rem;
450
+ color: #6b6b6b;
451
+ }
452
+
453
+ .powerhouse-cart-confirm__actions {
454
+ display: flex;
455
+ flex-direction: column;
456
+ gap: 0.5rem;
457
+ }
458
+
459
+ .powerhouse-cart-confirm__btn {
460
+ min-height: 2.6rem;
461
+ padding: 0.65rem 1rem;
462
+ font-size: 0.75rem;
463
+ font-weight: 600;
464
+ letter-spacing: 0.06em;
465
+ text-transform: uppercase;
466
+ cursor: pointer;
467
+ }
468
+
469
+ .powerhouse-cart-confirm__btn--primary {
470
+ border: 0;
471
+ background: #1d1d1d;
472
+ color: #ffffff;
473
+ }
474
+
475
+ .powerhouse-cart-confirm__btn--danger {
476
+ border: 1px solid #c1272d;
477
+ background: #ffffff;
478
+ color: #c1272d;
479
+ }
480
+
481
+ .powerhouse-cart-confirm__btn--ghost {
482
+ border: 0;
483
+ background: transparent;
484
+ color: #6b6b6b;
485
+ }
486
+
487
+ .powerhouse-cart-confirm__btn:disabled {
488
+ opacity: 0.6;
489
+ cursor: not-allowed;
490
+ }
package/src/segment.tsx CHANGED
@@ -2,9 +2,10 @@
2
2
 
3
3
  import Image from "next/image"
4
4
  import { useRouter } from "next/navigation"
5
- import { useTransition } from "react"
5
+ import { useState, useTransition } from "react"
6
6
  import type { HttpTypes } from "@medusajs/types"
7
7
  import { deleteLineItem, updateLineItem } from "@pradip1995/commerce-core/client/actions/cart"
8
+ import { addToWishlist } from "@pradip1995/commerce-core/client/actions/wishlist"
8
9
  import {
9
10
  trackCartLineItemQuantityChange,
10
11
  trackCartLineItemRemoved,
@@ -12,6 +13,7 @@ import {
12
13
  import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
13
14
  import { formatPrice } from "@pradip1995/segment-primitives/format-price"
14
15
  import { resolveMediaUrl } from "@pradip1995/segment-primitives/resolve-media-url"
16
+ import LoginRequiredModal from "@pradip1995/segment-login-popup/required"
15
17
  import {
16
18
  DEFAULT_CHECKOUT_LABEL,
17
19
  DEFAULT_CONTINUE_HREF,
@@ -131,6 +133,8 @@ function LineItemRow({
131
133
  }) {
132
134
  const router = useRouter()
133
135
  const [pending, startTransition] = useTransition()
136
+ const [confirmOpen, setConfirmOpen] = useState(false)
137
+ const [loginRequired, setLoginRequired] = useState(false)
134
138
  const thumbnail = lineItemImage(item)
135
139
  const variantLabel = item.variant?.title
136
140
  const showVariant = Boolean(variantLabel && !isPlaceholderVariant(variantLabel))
@@ -145,11 +149,36 @@ function LineItemRow({
145
149
  })
146
150
  }
147
151
 
148
- function remove() {
152
+ function removeFromCart() {
149
153
  if (!item.id) return
150
154
  startTransition(async () => {
151
155
  trackCartLineItemRemoved(item, currency)
152
156
  await deleteLineItem(item.id)
157
+ setConfirmOpen(false)
158
+ router.refresh()
159
+ })
160
+ }
161
+
162
+ function moveToWishlist() {
163
+ const productId = item.product_id
164
+ if (!productId) {
165
+ removeFromCart()
166
+ return
167
+ }
168
+ startTransition(async () => {
169
+ const result = await addToWishlist(productId)
170
+ if (!result.success && result.error?.toLowerCase().includes("login")) {
171
+ setConfirmOpen(false)
172
+ setLoginRequired(true)
173
+ return
174
+ }
175
+ if (!result.success) {
176
+ setConfirmOpen(false)
177
+ return
178
+ }
179
+ trackCartLineItemRemoved(item, currency)
180
+ if (item.id) await deleteLineItem(item.id)
181
+ setConfirmOpen(false)
153
182
  router.refresh()
154
183
  })
155
184
  }
@@ -201,11 +230,66 @@ function LineItemRow({
201
230
  +
202
231
  </button>
203
232
  </div>
204
- <button type="button" className="powerhouse-cart__remove" onClick={remove} disabled={pending}>
233
+ <button
234
+ type="button"
235
+ className="powerhouse-cart__remove"
236
+ onClick={() => setConfirmOpen(true)}
237
+ disabled={pending}
238
+ >
205
239
  Remove
206
240
  </button>
207
241
  </div>
208
242
  <p className="powerhouse-cart__total">{formatPrice(lineTotal(item), currency)}</p>
243
+
244
+ {confirmOpen ? (
245
+ <div className="powerhouse-cart-confirm">
246
+ <button
247
+ type="button"
248
+ className="powerhouse-cart-confirm__backdrop"
249
+ aria-label="Close"
250
+ onClick={() => setConfirmOpen(false)}
251
+ />
252
+ <div className="powerhouse-cart-confirm__panel" role="dialog" aria-modal="true">
253
+ <h3 className="powerhouse-cart-confirm__title">Remove this item?</h3>
254
+ <p className="powerhouse-cart-confirm__text">
255
+ Move it to your wishlist to save it for later, or remove it from your cart.
256
+ </p>
257
+ <div className="powerhouse-cart-confirm__actions">
258
+ <button
259
+ type="button"
260
+ className="powerhouse-cart-confirm__btn powerhouse-cart-confirm__btn--primary"
261
+ disabled={pending}
262
+ onClick={moveToWishlist}
263
+ >
264
+ {pending ? "Saving…" : "Move to Wishlist"}
265
+ </button>
266
+ <button
267
+ type="button"
268
+ className="powerhouse-cart-confirm__btn powerhouse-cart-confirm__btn--danger"
269
+ disabled={pending}
270
+ onClick={removeFromCart}
271
+ >
272
+ Remove the Product
273
+ </button>
274
+ <button
275
+ type="button"
276
+ className="powerhouse-cart-confirm__btn powerhouse-cart-confirm__btn--ghost"
277
+ disabled={pending}
278
+ onClick={() => setConfirmOpen(false)}
279
+ >
280
+ Keep in cart
281
+ </button>
282
+ </div>
283
+ </div>
284
+ </div>
285
+ ) : null}
286
+
287
+ <LoginRequiredModal
288
+ isOpen={loginRequired}
289
+ onClose={() => setLoginRequired(false)}
290
+ countryCode={countryCode}
291
+ message="Please log in to move this item to your wishlist."
292
+ />
209
293
  </article>
210
294
  )
211
295
  }