145 lines
5.5 KiB
TypeScript
145 lines
5.5 KiB
TypeScript
"use client"
|
||
|
||
import Image from "next/image"
|
||
import { updateLineItem, deleteLineItem } from "@lib/data/cart"
|
||
import { notifyCartUpdated } from "@lib/util/cart-events"
|
||
import { convertToLocale } from "@lib/util/money"
|
||
import { getProductCardImage } from "@lib/util/sale-scenario"
|
||
import { HttpTypes } from "@medusajs/types"
|
||
import ErrorMessage from "@modules/checkout/components/error-message"
|
||
import LineItemOptions from "@modules/common/components/line-item-options"
|
||
import LocalizedClientLink from "@modules/common/components/localized-client-link"
|
||
import { useState } from "react"
|
||
import { Loader2, Minus, Plus } from "lucide-react"
|
||
import { cn } from "@/lib/utils"
|
||
|
||
type ItemProps = {
|
||
item: HttpTypes.StoreCartLineItem
|
||
/** full: cart page with quantity controls; preview: checkout summary */
|
||
type?: "full" | "preview"
|
||
currencyCode: string
|
||
}
|
||
|
||
const MAX_QUANTITY = 99
|
||
|
||
/** Cart line as a list row (cart page and checkout summary) */
|
||
const Item = ({ item, type = "full", currencyCode }: ItemProps) => {
|
||
const [updating, setUpdating] = useState(false)
|
||
const [removing, setRemoving] = useState(false)
|
||
const [error, setError] = useState<string | null>(null)
|
||
const isFull = type === "full"
|
||
const money = (amount?: number | null) =>
|
||
convertToLocale({ amount: amount ?? 0, currency_code: currencyCode })
|
||
|
||
const changeQuantity = async (quantity: number) => {
|
||
if (quantity < 1 || quantity > MAX_QUANTITY) return
|
||
setError(null)
|
||
setUpdating(true)
|
||
await updateLineItem({ lineId: item.id, quantity })
|
||
.then(() => notifyCartUpdated())
|
||
.catch((err) => setError(err.message))
|
||
.finally(() => setUpdating(false))
|
||
}
|
||
|
||
const remove = async () => {
|
||
setRemoving(true)
|
||
await deleteLineItem(item.id)
|
||
.then(() => notifyCartUpdated())
|
||
.catch((err) => {
|
||
setError(err.message)
|
||
setRemoving(false)
|
||
})
|
||
}
|
||
|
||
return (
|
||
<li
|
||
className={cn("flex gap-4 sm:gap-6", isFull ? "py-6" : "py-4", removing && "opacity-40")}
|
||
data-testid="product-row"
|
||
>
|
||
<LocalizedClientLink
|
||
href={`/products/${item.product_handle}`}
|
||
className={cn(
|
||
"relative aspect-[4/5] shrink-0 overflow-hidden rounded-lg bg-muted",
|
||
isFull ? "w-24 sm:w-32" : "w-14"
|
||
)}
|
||
>
|
||
<Image
|
||
src={getProductCardImage(item.product_title)}
|
||
alt={item.product_title ?? ""}
|
||
fill
|
||
sizes={isFull ? "128px" : "56px"}
|
||
className="object-cover"
|
||
/>
|
||
</LocalizedClientLink>
|
||
|
||
<div className="flex min-w-0 flex-1 flex-col">
|
||
<div className="flex items-start justify-between gap-4">
|
||
<div className="min-w-0">
|
||
<LocalizedClientLink
|
||
href={`/products/${item.product_handle}`}
|
||
className={cn(
|
||
"text-foreground transition-colors hover:text-primary line-clamp-2",
|
||
isFull ? "text-base" : "text-sm"
|
||
)}
|
||
data-testid="product-title"
|
||
>
|
||
{item.product_title}
|
||
</LocalizedClientLink>
|
||
<LineItemOptions variant={item.variant} metadata={item.metadata} data-testid="product-variant" />
|
||
</div>
|
||
<div className="shrink-0 text-right">
|
||
<p className={cn("text-foreground tabular-nums", isFull ? "text-base" : "text-sm")} data-testid="product-price">
|
||
{money(item.total)}
|
||
</p>
|
||
{(isFull ? item.quantity > 1 : true) && (
|
||
<p className="mt-0.5 text-xs text-muted-foreground tabular-nums">
|
||
{item.quantity} × {money(item.unit_price)}
|
||
</p>
|
||
)}
|
||
</div>
|
||
</div>
|
||
|
||
{isFull && (
|
||
<div className="mt-auto flex items-center justify-between pt-4">
|
||
<div className="inline-flex items-center rounded-full border border-border" data-testid="product-select-button">
|
||
<button
|
||
type="button"
|
||
onClick={() => changeQuantity(item.quantity - 1)}
|
||
disabled={updating || item.quantity <= 1}
|
||
className="flex h-9 w-9 items-center justify-center rounded-full text-muted-foreground transition-colors hover:text-foreground disabled:opacity-30"
|
||
aria-label="Smanji količinu"
|
||
>
|
||
<Minus className="h-3.5 w-3.5" />
|
||
</button>
|
||
<span className="w-8 text-center text-sm tabular-nums" aria-live="polite">
|
||
{updating ? <Loader2 className="mx-auto h-3.5 w-3.5 animate-spin text-primary" /> : item.quantity}
|
||
</span>
|
||
<button
|
||
type="button"
|
||
onClick={() => changeQuantity(item.quantity + 1)}
|
||
disabled={updating || item.quantity >= MAX_QUANTITY}
|
||
className="flex h-9 w-9 items-center justify-center rounded-full text-muted-foreground transition-colors hover:text-foreground disabled:opacity-30"
|
||
aria-label="Povećaj količinu"
|
||
>
|
||
<Plus className="h-3.5 w-3.5" />
|
||
</button>
|
||
</div>
|
||
<button
|
||
type="button"
|
||
onClick={remove}
|
||
disabled={removing || updating}
|
||
className="text-sm text-muted-foreground underline-offset-4 transition-colors hover:text-destructive hover:underline disabled:opacity-40"
|
||
data-testid="product-delete-button"
|
||
>
|
||
Ukloni
|
||
</button>
|
||
</div>
|
||
)}
|
||
<ErrorMessage error={error} data-testid="product-error-message" />
|
||
</div>
|
||
</li>
|
||
)
|
||
}
|
||
|
||
export default Item
|