import type {CartLineUpdateInput} from '@shopify/hydrogen/storefront-api-types'; import type {CartLayout, LineItemChildrenMap} from '~/components/CartMain'; import {CartForm, Image, type OptimisticCartLine} from '@shopify/hydrogen'; import {useVariantUrl} from '~/lib/variants'; import {Link} from 'react-router'; import {ProductPrice} from './ProductPrice'; import {useAside} from './Aside'; import type { CartApiQueryFragment, CartLineFragment, } from 'storefrontapi.generated'; export type CartLine = OptimisticCartLine; /** * A single line item in the cart. It displays the product image, title, price. * It also provides controls to update the quantity or remove the line item. * If the line is a parent line that has child components (like warranties or gift wrapping), they are * rendered nested below the parent line. */ export function CartLineItem({ layout, line, childrenMap, }: { layout: CartLayout; line: CartLine; childrenMap: LineItemChildrenMap; }) { const {id, merchandise} = line; const {product, title, image, selectedOptions} = merchandise; const lineItemUrl = useVariantUrl(product.handle, selectedOptions); const {close} = useAside(); const lineItemChildren = childrenMap[id]; const childrenLabelId = `cart-line-children-${id}`; return (
  • {image && ( {title} )}
    { if (layout === 'aside') { close(); } }} >

    {product.title}

      {selectedOptions.map((option) => (
    • {option.name}: {option.value}
    • ))}
    {lineItemChildren ? (

    Line items with {product.title}

      {lineItemChildren.map((childLine) => ( ))}
    ) : null}
  • ); } /** * Provides the controls to update the quantity of a line item in the cart. * These controls are disabled when the line item is new, and the server * hasn't yet responded that it was successfully added to the cart. */ function CartLineQuantity({line}: {line: CartLine}) { if (!line || typeof line?.quantity === 'undefined') return null; const {id: lineId, quantity, isOptimistic} = line; const prevQuantity = Number(Math.max(0, quantity - 1).toFixed(0)); const nextQuantity = Number((quantity + 1).toFixed(0)); return (
    Quantity: {quantity}       
    ); } /** * A button that removes a line item from the cart. It is disabled * when the line item is new, and the server hasn't yet responded * that it was successfully added to the cart. */ function CartLineRemoveButton({ lineIds, disabled, }: { lineIds: string[]; disabled: boolean; }) { return ( ); } function CartLineUpdateButton({ children, lines, }: { children: React.ReactNode; lines: CartLineUpdateInput[]; }) { const lineIds = lines.map((line) => line.id); return ( {children} ); } /** * Returns a unique key for the update action. This is used to make sure actions modifying the same line * items are not run concurrently, but cancel each other. For example, if the user clicks "Increase quantity" * and "Decrease quantity" in rapid succession, the actions will cancel each other and only the last one will run. * @param lineIds - line ids affected by the update * @returns */ function getUpdateKey(lineIds: string[]) { return [CartForm.ACTIONS.LinesUpdate, ...lineIds].join('-'); }