Skip to content

Commit

Permalink
chore: refactor ProductCard to streamline product variant handling an…
Browse files Browse the repository at this point in the history
…d remove unused imports
  • Loading branch information
hta218 committed Oct 16, 2024
1 parent 3b64b20 commit ea25493
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions app/modules/product-card/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Image, flattenConnection } from "@shopify/hydrogen";
import type { MoneyV2, Product } from "@shopify/hydrogen/storefront-api-types";
import type { MoneyV2 } from "@shopify/hydrogen/storefront-api-types";
import clsx from "clsx";
import type { ProductCardFragment } from "storefrontapi.generated";
import { Link } from "~/components/link";
import { ProductTag } from "~/components/product-tag";
import { VariantPrices } from "~/components/variant-prices";
import { getProductPlaceholder } from "~/lib/placeholders";
import { isDiscounted, isNewArrival } from "~/lib/utils";
import { QuickViewTrigger } from "./quick-view";

Expand All @@ -18,18 +17,15 @@ export function ProductCard({
className?: string;
loading?: HTMLImageElement["loading"];
}) {
let cardLabel = "";
let cardProduct: Product = product?.variants
? (product as Product)
: getProductPlaceholder();
if (!cardProduct?.variants?.nodes?.length) return null;
if (!product?.variants?.nodes?.length) return null;

let variants = flattenConnection(cardProduct.variants);
let variants = flattenConnection(product.variants);
let firstVariant = variants[0];

if (!firstVariant) return null;
let { image, price, compareAtPrice } = firstVariant;

let { image, price, compareAtPrice } = firstVariant;
let cardLabel = "";
if (isDiscounted(price as MoneyV2, compareAtPrice as MoneyV2)) {
cardLabel = "Sale";
} else if (isNewArrival(product.publishedAt)) {
Expand Down

0 comments on commit ea25493

Please sign in to comment.