From d9838f8807e6ee9b86a81fbbb367540a3bf1a9f8 Mon Sep 17 00:00:00 2001 From: Sol Irvine Date: Wed, 23 Aug 2023 23:24:03 -0700 Subject: [PATCH] fix: Add to cart buttons on homepage --- app/[locale]/globals.css | 2 +- app/[locale]/page.tsx | 4 +- components/cart/inline-add-to-cart.tsx | 75 +++++++++++++++++++ ...{three-items.tsx => homepage-products.tsx} | 22 ++++-- components/label.tsx | 4 +- components/layout/shoplist.tsx | 6 +- messages/en.json | 6 +- 7 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 components/cart/inline-add-to-cart.tsx rename components/grid/{three-items.tsx => homepage-products.tsx} (68%) diff --git a/app/[locale]/globals.css b/app/[locale]/globals.css index dc3410eaf..155116088 100644 --- a/app/[locale]/globals.css +++ b/app/[locale]/globals.css @@ -25,5 +25,5 @@ button { } [lang='ja'] .font-multilingual { - @apply font-japan; + @apply font-japan tracking-widest; } diff --git a/app/[locale]/page.tsx b/app/[locale]/page.tsx index 4ec31feac..318b6768d 100644 --- a/app/[locale]/page.tsx +++ b/app/[locale]/page.tsx @@ -1,4 +1,3 @@ -import { ThreeItemGrid } from 'components/grid/three-items'; import Footer from 'components/layout/footer'; import { SupportedLocale } from 'components/layout/navbar/language-control'; @@ -11,6 +10,7 @@ import HomeImage006 from '@images/home-images/home-image-006.webp'; import HomeImage007 from '@images/home-images/home-image-007.webp'; import HomeImage008 from '@images/home-images/home-image-008.webp'; import clsx from 'clsx'; +import { HomepageProducts } from 'components/grid/homepage-products'; import AboutNaraiPreview from 'components/layout/about-narai-preview'; import ConceptPreview from 'components/layout/concept-preview'; import LocationPreview from 'components/layout/location-preview'; @@ -52,7 +52,7 @@ export default async function HomePage({
- +
diff --git a/components/cart/inline-add-to-cart.tsx b/components/cart/inline-add-to-cart.tsx new file mode 100644 index 000000000..573e68c67 --- /dev/null +++ b/components/cart/inline-add-to-cart.tsx @@ -0,0 +1,75 @@ +'use client'; + +import clsx from 'clsx'; +import { addItem } from 'components/cart/actions'; +import LoadingDots from 'components/loading-dots'; +import { ProductVariant } from 'lib/shopify/types'; +import { useTranslations } from 'next-intl'; +import { useRouter, useSearchParams } from 'next/navigation'; +import { useTransition } from 'react'; + +export function InlineAddToCart({ + variants, + availableForSale +}: { + variants: ProductVariant[]; + availableForSale: boolean; +}) { + const router = useRouter(); + const searchParams = useSearchParams(); + const t = useTranslations('Index'); + + const [isPending, startTransition] = useTransition(); + const defaultVariantId = variants.length === 1 ? variants[0]?.id : undefined; + const variant = variants.find((variant: ProductVariant) => + variant.selectedOptions.every( + (option) => option.value === searchParams.get(option.name.toLowerCase()) + ) + ); + const selectedVariantId = variant?.id || defaultVariantId; + const title = !availableForSale + ? 'Out of stock' + : !selectedVariantId + ? 'Please select options' + : undefined; + + return ( + + ); +} diff --git a/components/grid/three-items.tsx b/components/grid/homepage-products.tsx similarity index 68% rename from components/grid/three-items.tsx rename to components/grid/homepage-products.tsx index 02671f646..84391b945 100644 --- a/components/grid/three-items.tsx +++ b/components/grid/homepage-products.tsx @@ -1,4 +1,5 @@ import clsx from 'clsx'; +import { InlineAddToCart } from 'components/cart/inline-add-to-cart'; import { SupportedLocale } from 'components/layout/navbar/language-control'; import { getCollectionProducts } from 'lib/shopify'; import type { Product } from 'lib/shopify/types'; @@ -6,13 +7,17 @@ import Link from 'next/link'; import Label from '../label'; import { GridTileImage } from './tile'; -function ThreeItemGridItem({ item, priority }: { item: Product; priority?: boolean }) { +function HomepageProductsItem({ item, priority }: { item: Product; priority?: boolean }) { const size = item?.variants?.[0]?.selectedOptions?.find((option) => option.name === 'Size'); const image = item?.variants?.[0]?.image; return !!image ? ( -
- +
+
{item?.summary?.value}
+
) : null; } -export async function ThreeItemGrid({ lang }: { lang?: SupportedLocale }) { +export async function HomepageProducts({ lang }: { lang?: SupportedLocale }) { // Collections that start with `hidden-*` are hidden from the search page. const homepageItems = await getCollectionProducts({ collection: 'hidden-homepage-featured-items', @@ -50,14 +56,14 @@ export async function ThreeItemGrid({ lang }: { lang?: SupportedLocale }) { return (
- - - + + +
); } diff --git a/components/label.tsx b/components/label.tsx index 3fbcf3c33..e5af536cd 100644 --- a/components/label.tsx +++ b/components/label.tsx @@ -15,7 +15,9 @@ const Label = ({ return (
-

{title}

+

+ {title} +

+

shop list

-

- {t('shops.subtitle')} -

+

{t('shops.subtitle')}