design upgrade
This commit is contained in:
@@ -1,139 +1,143 @@
|
||||
"use client"
|
||||
|
||||
import { Table } from "@modules/common/components/ui"
|
||||
import { updateLineItem } from "@lib/data/cart"
|
||||
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 CartItemSelect from "@modules/cart/components/cart-item-select"
|
||||
import ErrorMessage from "@modules/checkout/components/error-message"
|
||||
import DeleteButton from "@modules/common/components/delete-button"
|
||||
import LineItemOptions from "@modules/common/components/line-item-options"
|
||||
import LineItemPrice from "@modules/common/components/line-item-price"
|
||||
import LineItemUnitPrice from "@modules/common/components/line-item-unit-price"
|
||||
import LocalizedClientLink from "@modules/common/components/localized-client-link"
|
||||
import Thumbnail from "@modules/products/components/thumbnail"
|
||||
import { useState } from "react"
|
||||
import { Loader2 } from "lucide-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))
|
||||
}
|
||||
|
||||
await updateLineItem({
|
||||
lineId: item.id,
|
||||
quantity,
|
||||
})
|
||||
const remove = async () => {
|
||||
setRemoving(true)
|
||||
await deleteLineItem(item.id)
|
||||
.then(() => notifyCartUpdated())
|
||||
.catch((err) => {
|
||||
setError(err.message)
|
||||
})
|
||||
.finally(() => {
|
||||
setUpdating(false)
|
||||
setRemoving(false)
|
||||
})
|
||||
}
|
||||
|
||||
const maxQtyFromInventory = 10
|
||||
const maxQuantity = item.variant?.manage_inventory ? 10 : maxQtyFromInventory
|
||||
|
||||
return (
|
||||
<Table.Row className="w-full border-b border-border" data-testid="product-row">
|
||||
<Table.Cell className="p-3 w-20">
|
||||
<LocalizedClientLink
|
||||
href={`/products/${item.product_handle}`}
|
||||
className={cn("flex rounded-md overflow-hidden border border-border", {
|
||||
"w-14 h-14": type === "preview",
|
||||
"sm:w-20 sm:h-20 w-14 h-14": type === "full",
|
||||
})}
|
||||
>
|
||||
<Thumbnail
|
||||
thumbnail={item.thumbnail}
|
||||
images={item.variant?.product?.images}
|
||||
size="square"
|
||||
/>
|
||||
</LocalizedClientLink>
|
||||
</Table.Cell>
|
||||
<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>
|
||||
|
||||
<Table.Cell className="text-left py-3">
|
||||
<LocalizedClientLink
|
||||
href={`/products/${item.product_handle}`}
|
||||
className="font-medium text-foreground hover:underline line-clamp-2 text-sm sm:text-base"
|
||||
data-testid="product-title"
|
||||
>
|
||||
{item.product_title}
|
||||
</LocalizedClientLink>
|
||||
<LineItemOptions variant={item.variant} data-testid="product-variant" />
|
||||
</Table.Cell>
|
||||
|
||||
{type === "full" && (
|
||||
<Table.Cell className="py-3">
|
||||
<div className="flex gap-2 items-center w-28">
|
||||
<DeleteButton id={item.id} data-testid="product-delete-button" />
|
||||
<CartItemSelect
|
||||
value={item.quantity}
|
||||
onChange={(value) => changeQuantity(parseInt(value.target.value))}
|
||||
className="h-9 rounded-md border border-input bg-background px-2 text-sm"
|
||||
data-testid="product-select-button"
|
||||
>
|
||||
{Array.from(
|
||||
{
|
||||
length: Math.min(maxQuantity, 10),
|
||||
},
|
||||
(_, i) => (
|
||||
<option value={i + 1} key={i}>
|
||||
{i + 1}
|
||||
</option>
|
||||
)
|
||||
<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"
|
||||
)}
|
||||
</CartItemSelect>
|
||||
{updating && <Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />}
|
||||
data-testid="product-title"
|
||||
>
|
||||
{item.product_title}
|
||||
</LocalizedClientLink>
|
||||
<LineItemOptions variant={item.variant} metadata={item.metadata} data-testid="product-variant" />
|
||||
</div>
|
||||
<ErrorMessage error={error} data-testid="product-error-message" />
|
||||
</Table.Cell>
|
||||
)}
|
||||
<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>
|
||||
|
||||
{type === "full" && (
|
||||
<Table.Cell className="hidden sm:table-cell py-3 text-muted-foreground text-sm">
|
||||
<LineItemUnitPrice
|
||||
item={item}
|
||||
style="tight"
|
||||
currencyCode={currencyCode}
|
||||
/>
|
||||
</Table.Cell>
|
||||
)}
|
||||
|
||||
<Table.Cell className="text-right py-3 pr-4 font-semibold text-foreground text-sm">
|
||||
<span
|
||||
className={cn({
|
||||
"flex flex-col items-end h-full justify-center": type === "preview",
|
||||
})}
|
||||
>
|
||||
{type === "preview" && (
|
||||
<span className="flex gap-x-1 text-xs text-muted-foreground font-normal">
|
||||
<span>{item.quantity}x </span>
|
||||
<LineItemUnitPrice
|
||||
item={item}
|
||||
style="tight"
|
||||
currencyCode={currencyCode}
|
||||
/>
|
||||
</span>
|
||||
)}
|
||||
<LineItemPrice
|
||||
item={item}
|
||||
style="tight"
|
||||
currencyCode={currencyCode}
|
||||
/>
|
||||
</span>
|
||||
</Table.Cell>
|
||||
</Table.Row>
|
||||
{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>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user