Compare commits
35 Commits
integratio
...
form-with-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f29a16de5c | ||
|
|
6bfd0b1bd9 | ||
|
|
6e85b80158 | ||
|
|
8c5f8d8d2a | ||
|
|
84224f8d7e | ||
|
|
94b85fca6f | ||
|
|
37cb5e38da | ||
|
|
9a4c995bb6 | ||
|
|
dd7449f975 | ||
|
|
cea56f608b | ||
|
|
0ebf071826 | ||
|
|
d7a4f3dc46 | ||
|
|
ec21369389 | ||
|
|
7c1b34abdb | ||
|
|
7fd9ad8a8c | ||
|
|
42d5d8efcf | ||
|
|
a5de9173e8 | ||
|
|
887d437795 | ||
|
|
610b0e8692 | ||
|
|
25ddc5e643 | ||
|
|
3a18f9a098 | ||
|
|
2fe1527bea | ||
|
|
2448f5201c | ||
|
|
80e48001d9 | ||
|
|
1f47796529 | ||
|
|
ece49c4265 | ||
|
|
2035fa0431 | ||
|
|
5cb1245432 | ||
|
|
d9f875b539 | ||
|
|
b0f6e94fba | ||
|
|
d8703e8140 | ||
|
|
18167d22f3 | ||
|
|
4993fca356 | ||
|
|
e9643a546e | ||
|
|
6a153b627c |
23
.eslintrc.js
23
.eslintrc.js
@@ -1,23 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
extends: ['next', 'prettier'],
|
|
||||||
plugins: ['unicorn'],
|
|
||||||
rules: {
|
|
||||||
'no-unused-vars': [
|
|
||||||
'error',
|
|
||||||
{
|
|
||||||
args: 'after-used',
|
|
||||||
caughtErrors: 'none',
|
|
||||||
ignoreRestSiblings: true,
|
|
||||||
vars: 'all'
|
|
||||||
}
|
|
||||||
],
|
|
||||||
'prefer-const': 'error',
|
|
||||||
'react-hooks/exhaustive-deps': 'error',
|
|
||||||
'unicorn/filename-case': [
|
|
||||||
'error',
|
|
||||||
{
|
|
||||||
case: 'kebabCase'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
6
.github/dependabot.yml
vendored
6
.github/dependabot.yml
vendored
@@ -1,6 +0,0 @@
|
|||||||
version: 2
|
|
||||||
updates:
|
|
||||||
- package-ecosystem: 'github-actions'
|
|
||||||
directory: '/'
|
|
||||||
schedule:
|
|
||||||
interval: 'weekly'
|
|
||||||
32
.github/workflows/test.yml
vendored
32
.github/workflows/test.yml
vendored
@@ -1,32 +0,0 @@
|
|||||||
name: test
|
|
||||||
on: pull_request
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Cancel running workflows
|
|
||||||
uses: styfle/cancel-workflow-action@0.11.0
|
|
||||||
with:
|
|
||||||
access_token: ${{ github.token }}
|
|
||||||
- name: Checkout repo
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: Set node version
|
|
||||||
uses: actions/setup-node@v3
|
|
||||||
with:
|
|
||||||
node-version-file: '.nvmrc'
|
|
||||||
- name: Set pnpm version
|
|
||||||
uses: pnpm/action-setup@v2
|
|
||||||
with:
|
|
||||||
run_install: false
|
|
||||||
version: 7
|
|
||||||
- name: Cache node_modules
|
|
||||||
id: node-modules-cache
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: '**/node_modules'
|
|
||||||
key: node-modules-cache-${{ hashFiles('**/pnpm-lock.yaml') }}
|
|
||||||
- name: Install dependencies
|
|
||||||
if: steps.node-modules-cache.outputs.cache-hit != 'true'
|
|
||||||
run: pnpm install
|
|
||||||
- name: Run tests
|
|
||||||
run: pnpm test
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
.vercel
|
|
||||||
.next
|
|
||||||
pnpm-lock.yaml
|
|
||||||
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@@ -2,8 +2,8 @@
|
|||||||
"typescript.tsdk": "node_modules/typescript/lib",
|
"typescript.tsdk": "node_modules/typescript/lib",
|
||||||
"typescript.enablePromptUseWorkspaceTsdk": true,
|
"typescript.enablePromptUseWorkspaceTsdk": true,
|
||||||
"editor.codeActionsOnSave": {
|
"editor.codeActionsOnSave": {
|
||||||
"source.fixAll": true,
|
"source.fixAll": "explicit",
|
||||||
"source.organizeImports": true,
|
"source.organizeImports": "explicit",
|
||||||
"source.sortMembers": true
|
"source.sortMembers": "explicit"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
34
README.md
34
README.md
@@ -2,18 +2,9 @@
|
|||||||
|
|
||||||
# Next.js Commerce
|
# Next.js Commerce
|
||||||
|
|
||||||
A Next.js 13 and App Router-ready ecommerce template featuring:
|
A high-perfomance, server-rendered Next.js App Router ecommerce application.
|
||||||
|
|
||||||
- Next.js App Router
|
This template uses React Server Components, Server Actions, `Suspense`, `useOptimistic`, and more.
|
||||||
- Optimized for SEO using Next.js's Metadata
|
|
||||||
- React Server Components (RSCs) and Suspense
|
|
||||||
- Server Actions for mutations
|
|
||||||
- Edge Runtime
|
|
||||||
- New fetching and caching paradigms
|
|
||||||
- Dynamic OG images
|
|
||||||
- Styling with Tailwind CSS
|
|
||||||
- Checkout and payments with Shopify
|
|
||||||
- Automatic light/dark mode based on system settings
|
|
||||||
|
|
||||||
<h3 id="v1-note"></h3>
|
<h3 id="v1-note"></h3>
|
||||||
|
|
||||||
@@ -23,14 +14,27 @@ A Next.js 13 and App Router-ready ecommerce template featuring:
|
|||||||
|
|
||||||
Vercel will only be actively maintaining a Shopify version [as outlined in our vision and strategy for Next.js Commerce](https://github.com/vercel/commerce/pull/966).
|
Vercel will only be actively maintaining a Shopify version [as outlined in our vision and strategy for Next.js Commerce](https://github.com/vercel/commerce/pull/966).
|
||||||
|
|
||||||
Vercel is more than happy to partner and work with any commerce provider to help them get a similar template up and running and listed below. Alternative providers should be able to fork this repository and swap out the `lib/shopify` file with their own implementation while leaving the rest of the template mostly unchanged.
|
Vercel is happy to partner and work with any commerce provider to help them get a similar template up and running and listed below. Alternative providers should be able to fork this repository and swap out the `lib/shopify` file with their own implementation while leaving the rest of the template mostly unchanged.
|
||||||
|
|
||||||
- Shopify (this repository)
|
- Shopify (this repository)
|
||||||
- [BigCommerce](https://github.com/bigcommerce/nextjs-commerce) ([Demo](https://next-commerce-v2.vercel.app/))
|
- [BigCommerce](https://github.com/bigcommerce/nextjs-commerce) ([Demo](https://next-commerce-v2.vercel.app/))
|
||||||
|
- [Ecwid by Lightspeed](https://github.com/Ecwid/ecwid-nextjs-commerce/) ([Demo](https://ecwid-nextjs-commerce.vercel.app/))
|
||||||
- [Medusa](https://github.com/medusajs/vercel-commerce) ([Demo](https://medusa-nextjs-commerce.vercel.app/))
|
- [Medusa](https://github.com/medusajs/vercel-commerce) ([Demo](https://medusa-nextjs-commerce.vercel.app/))
|
||||||
- [Saleor](https://github.com/saleor/nextjs-commerce) ([Demo](https://saleor-commerce.vercel.app/))
|
- [Saleor](https://github.com/saleor/nextjs-commerce) ([Demo](https://saleor-commerce.vercel.app/))
|
||||||
- [Shopware](https://github.com/shopwareLabs/vercel-commerce) ([Demo](https://shopware-vercel-commerce-react.vercel.app/))
|
- [Shopware](https://github.com/shopwareLabs/vercel-commerce) ([Demo](https://shopware-vercel-commerce-react.vercel.app/))
|
||||||
- [Swell](https://github.com/swellstores/verswell-commerce) ([Demo](https://verswell-commerce.vercel.app/))
|
- [Swell](https://github.com/swellstores/verswell-commerce) ([Demo](https://verswell-commerce.vercel.app/))
|
||||||
|
- [Umbraco](https://github.com/umbraco/Umbraco.VercelCommerce.Demo) ([Demo](https://vercel-commerce-demo.umbraco.com/))
|
||||||
|
- [Wix](https://github.com/wix/nextjs-commerce) ([Demo](https://wix-nextjs-commerce.vercel.app/))
|
||||||
|
|
||||||
|
> Note: Providers, if you are looking to use similar products for your demo, you can [download these assets](https://drive.google.com/file/d/1q_bKerjrwZgHwCw0ovfUMW6He9VtepO_/view?usp=sharing).
|
||||||
|
|
||||||
|
## Integrations
|
||||||
|
|
||||||
|
Integrations enable upgraded or additional functionality for Next.js Commerce
|
||||||
|
|
||||||
|
- [Orama](https://github.com/oramasearch/nextjs-commerce) ([Demo](https://vercel-commerce.oramasearch.com/))
|
||||||
|
- Upgrades search to include typeahead with dynamic re-rendering, vector-based similarity search, and JS-based configuration.
|
||||||
|
- Search runs entirely in the browser for smaller catalogs or on a CDN for larger.
|
||||||
|
|
||||||
## Running locally
|
## Running locally
|
||||||
|
|
||||||
@@ -56,9 +60,9 @@ Your app should now be running on [localhost:3000](http://localhost:3000/).
|
|||||||
1. Select the `Vercel Solutions` scope.
|
1. Select the `Vercel Solutions` scope.
|
||||||
1. Connect to the existing `commerce-shopify` project.
|
1. Connect to the existing `commerce-shopify` project.
|
||||||
1. Run `vc env pull` to get environment variables.
|
1. Run `vc env pull` to get environment variables.
|
||||||
1. Run `pmpm dev` to ensure everything is working correctly.
|
1. Run `pnpm dev` to ensure everything is working correctly.
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
## Vercel, Next.js Commerce, and Shopify integration guide
|
## Vercel, Next.js Commerce, and Shopify Integration Guide
|
||||||
|
|
||||||
You can use this comprehensive [integration guide](http://vercel.com/docs/integrations/shopify) with step-by-step instructions on how to configure Shopify as a headless CMS using Next.js Commerce as your headless Shopify storefront on Vercel.
|
You can use this comprehensive [integration guide](https://vercel.com/docs/integrations/ecommerce/shopify) with step-by-step instructions on how to configure Shopify as a headless CMS using Next.js Commerce as your headless Shopify storefront on Vercel.
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
import Footer from 'components/layout/footer';
|
import Footer from 'components/layout/footer';
|
||||||
import { Suspense } from 'react';
|
|
||||||
|
|
||||||
export default function Layout({ children }: { children: React.ReactNode }) {
|
export default function Layout({ children }: { children: React.ReactNode }) {
|
||||||
return (
|
return (
|
||||||
<Suspense>
|
<>
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
<div className="mx-8 max-w-2xl py-20 sm:mx-auto">
|
<div className="mx-8 max-w-2xl py-20 sm:mx-auto">{children}</div>
|
||||||
<Suspense>{children}</Suspense>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
<Footer />
|
<Footer />
|
||||||
</Suspense>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,10 +4,6 @@ import Prose from 'components/prose';
|
|||||||
import { getPage } from 'lib/shopify';
|
import { getPage } from 'lib/shopify';
|
||||||
import { notFound } from 'next/navigation';
|
import { notFound } from 'next/navigation';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
|
||||||
|
|
||||||
export const revalidate = 43200; // 12 hours in seconds
|
|
||||||
|
|
||||||
export async function generateMetadata({
|
export async function generateMetadata({
|
||||||
params
|
params
|
||||||
}: {
|
}: {
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import { revalidate } from 'lib/shopify';
|
import { revalidate } from 'lib/shopify';
|
||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
|
||||||
|
|
||||||
export async function POST(req: NextRequest): Promise<NextResponse> {
|
export async function POST(req: NextRequest): Promise<NextResponse> {
|
||||||
return revalidate(req);
|
return revalidate(req);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
export default function Error({ reset }: { reset: () => void }) {
|
export default function Error({ reset }: { reset: () => void }) {
|
||||||
return (
|
return (
|
||||||
<div className="mx-auto my-4 flex max-w-xl flex-col rounded-lg border border-neutral-200 bg-white p-8 dark:border-neutral-800 dark:bg-black md:p-12">
|
<div className="mx-auto my-4 flex max-w-xl flex-col rounded-lg border border-neutral-200 bg-white p-8 md:p-12 dark:border-neutral-800 dark:bg-black">
|
||||||
<h2 className="text-xl font-bold">Oh no!</h2>
|
<h2 className="text-xl font-bold">Oh no!</h2>
|
||||||
<p className="my-2">
|
<p className="my-2">
|
||||||
There was an issue with our storefront. This could be a temporary issue, please try your
|
There was an issue with our storefront. This could be a temporary issue, please try your
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
import Navbar from 'components/layout/navbar';
|
import { CartProvider } from 'components/cart/cart-context';
|
||||||
|
import { Navbar } from 'components/layout/navbar';
|
||||||
|
import { WelcomeToast } from 'components/welcome-toast';
|
||||||
|
import { GeistSans } from 'geist/font/sans';
|
||||||
|
import { getCart } from 'lib/shopify';
|
||||||
import { ensureStartsWith } from 'lib/utils';
|
import { ensureStartsWith } from 'lib/utils';
|
||||||
import { Inter } from 'next/font/google';
|
import { cookies } from 'next/headers';
|
||||||
import { ReactNode, Suspense } from 'react';
|
import { ReactNode } from 'react';
|
||||||
|
import { Toaster } from 'sonner';
|
||||||
import './globals.css';
|
import './globals.css';
|
||||||
|
|
||||||
const { TWITTER_CREATOR, TWITTER_SITE, SITE_NAME } = process.env;
|
const { TWITTER_CREATOR, TWITTER_SITE, SITE_NAME } = process.env;
|
||||||
@@ -31,20 +36,22 @@ export const metadata = {
|
|||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
const inter = Inter({
|
|
||||||
subsets: ['latin'],
|
|
||||||
display: 'swap',
|
|
||||||
variable: '--font-inter'
|
|
||||||
});
|
|
||||||
|
|
||||||
export default async function RootLayout({ children }: { children: ReactNode }) {
|
export default async function RootLayout({ children }: { children: ReactNode }) {
|
||||||
|
const cartId = cookies().get('cartId')?.value;
|
||||||
|
// Don't await the fetch, pass the Promise to the context provider
|
||||||
|
const cart = getCart(cartId);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<html lang="en" className={inter.variable}>
|
<html lang="en" className={GeistSans.variable}>
|
||||||
<body className="bg-neutral-50 text-black selection:bg-teal-300 dark:bg-neutral-900 dark:text-white dark:selection:bg-pink-500 dark:selection:text-white">
|
<body className="bg-neutral-50 text-black selection:bg-teal-300 dark:bg-neutral-900 dark:text-white dark:selection:bg-pink-500 dark:selection:text-white">
|
||||||
|
<CartProvider cartPromise={cart}>
|
||||||
<Navbar />
|
<Navbar />
|
||||||
<Suspense>
|
<main>
|
||||||
<main>{children}</main>
|
{children}
|
||||||
</Suspense>
|
<Toaster closeButton />
|
||||||
|
<WelcomeToast />
|
||||||
|
</main>
|
||||||
|
</CartProvider>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
import { Carousel } from 'components/carousel';
|
import { Carousel } from 'components/carousel';
|
||||||
import { ThreeItemGrid } from 'components/grid/three-items';
|
import { ThreeItemGrid } from 'components/grid/three-items';
|
||||||
import Footer from 'components/layout/footer';
|
import Footer from 'components/layout/footer';
|
||||||
import { Suspense } from 'react';
|
|
||||||
|
|
||||||
export const runtime = 'edge';
|
|
||||||
|
|
||||||
export const metadata = {
|
export const metadata = {
|
||||||
description: 'High-performance ecommerce store built with Next.js, Vercel, and Shopify.',
|
description: 'High-performance ecommerce store built with Next.js, Vercel, and Shopify.',
|
||||||
@@ -12,16 +9,12 @@ export const metadata = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default async function HomePage() {
|
export default function HomePage() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<ThreeItemGrid />
|
<ThreeItemGrid />
|
||||||
<Suspense>
|
|
||||||
<Carousel />
|
<Carousel />
|
||||||
<Suspense>
|
|
||||||
<Footer />
|
<Footer />
|
||||||
</Suspense>
|
|
||||||
</Suspense>
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
import type { Metadata } from 'next';
|
import type { Metadata } from 'next';
|
||||||
import { notFound } from 'next/navigation';
|
import { notFound } from 'next/navigation';
|
||||||
import { Suspense } from 'react';
|
|
||||||
|
|
||||||
import { GridTileImage } from 'components/grid/tile';
|
import { GridTileImage } from 'components/grid/tile';
|
||||||
import Footer from 'components/layout/footer';
|
import Footer from 'components/layout/footer';
|
||||||
import { Gallery } from 'components/product/gallery';
|
import { Gallery } from 'components/product/gallery';
|
||||||
|
import { ProductProvider } from 'components/product/product-context';
|
||||||
import { ProductDescription } from 'components/product/product-description';
|
import { ProductDescription } from 'components/product/product-description';
|
||||||
import { HIDDEN_PRODUCT_TAG } from 'lib/constants';
|
import { HIDDEN_PRODUCT_TAG } from 'lib/constants';
|
||||||
import { getProduct, getProductRecommendations } from 'lib/shopify';
|
import { getProduct, getProductRecommendations } from 'lib/shopify';
|
||||||
import { Image } from 'lib/shopify/types';
|
import { Image } from 'lib/shopify/types';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
|
import { Suspense } from 'react';
|
||||||
export const runtime = 'edge';
|
|
||||||
|
|
||||||
export async function generateMetadata({
|
export async function generateMetadata({
|
||||||
params
|
params
|
||||||
@@ -74,7 +73,7 @@ export default async function ProductPage({ params }: { params: { handle: string
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<ProductProvider>
|
||||||
<script
|
<script
|
||||||
type="application/ld+json"
|
type="application/ld+json"
|
||||||
dangerouslySetInnerHTML={{
|
dangerouslySetInnerHTML={{
|
||||||
@@ -82,28 +81,32 @@ export default async function ProductPage({ params }: { params: { handle: string
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<div className="mx-auto max-w-screen-2xl px-4">
|
<div className="mx-auto max-w-screen-2xl px-4">
|
||||||
<div className="flex flex-col rounded-lg border border-neutral-200 bg-white p-8 dark:border-neutral-800 dark:bg-black md:p-12 lg:flex-row lg:gap-8">
|
<div className="flex flex-col rounded-lg border border-neutral-200 bg-white p-8 md:p-12 lg:flex-row lg:gap-8 dark:border-neutral-800 dark:bg-black">
|
||||||
<div className="h-full w-full basis-full lg:basis-4/6">
|
<div className="h-full w-full basis-full lg:basis-4/6">
|
||||||
|
<Suspense
|
||||||
|
fallback={
|
||||||
|
<div className="relative aspect-square h-full max-h-[550px] w-full overflow-hidden" />
|
||||||
|
}
|
||||||
|
>
|
||||||
<Gallery
|
<Gallery
|
||||||
images={product.images.map((image: Image) => ({
|
images={product.images.slice(0, 5).map((image: Image) => ({
|
||||||
src: image.url,
|
src: image.url,
|
||||||
altText: image.altText
|
altText: image.altText
|
||||||
}))}
|
}))}
|
||||||
/>
|
/>
|
||||||
|
</Suspense>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="basis-full lg:basis-2/6">
|
<div className="basis-full lg:basis-2/6">
|
||||||
|
<Suspense fallback={null}>
|
||||||
<ProductDescription product={product} />
|
<ProductDescription product={product} />
|
||||||
|
</Suspense>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<Suspense>
|
|
||||||
<RelatedProducts id={product.id} />
|
<RelatedProducts id={product.id} />
|
||||||
</Suspense>
|
|
||||||
</div>
|
</div>
|
||||||
<Suspense>
|
|
||||||
<Footer />
|
<Footer />
|
||||||
</Suspense>
|
</ProductProvider>
|
||||||
</>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,7 +124,11 @@ async function RelatedProducts({ id }: { id: string }) {
|
|||||||
key={product.handle}
|
key={product.handle}
|
||||||
className="aspect-square w-full flex-none min-[475px]:w-1/2 sm:w-1/3 md:w-1/4 lg:w-1/5"
|
className="aspect-square w-full flex-none min-[475px]:w-1/2 sm:w-1/3 md:w-1/4 lg:w-1/5"
|
||||||
>
|
>
|
||||||
<Link className="relative h-full w-full" href={`/product/${product.handle}`}>
|
<Link
|
||||||
|
className="relative h-full w-full"
|
||||||
|
href={`/product/${product.handle}`}
|
||||||
|
prefetch={true}
|
||||||
|
>
|
||||||
<GridTileImage
|
<GridTileImage
|
||||||
alt={product.title}
|
alt={product.title}
|
||||||
label={{
|
label={{
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ import Grid from 'components/grid';
|
|||||||
import ProductGridItems from 'components/layout/product-grid-items';
|
import ProductGridItems from 'components/layout/product-grid-items';
|
||||||
import { defaultSort, sorting } from 'lib/constants';
|
import { defaultSort, sorting } from 'lib/constants';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
|
||||||
|
|
||||||
export async function generateMetadata({
|
export async function generateMetadata({
|
||||||
params
|
params
|
||||||
}: {
|
}: {
|
||||||
|
|||||||
8
app/search/children-wrapper.tsx
Normal file
8
app/search/children-wrapper.tsx
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
'use client';
|
||||||
|
import { useSearchParams } from 'next/navigation';
|
||||||
|
import { Fragment } from 'react';
|
||||||
|
|
||||||
|
export default function ChildrenWrapper({ children }: { children: React.ReactNode }) {
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
return <Fragment key={searchParams.get('q')}>{children}</Fragment>;
|
||||||
|
}
|
||||||
@@ -2,21 +2,23 @@ import Footer from 'components/layout/footer';
|
|||||||
import Collections from 'components/layout/search/collections';
|
import Collections from 'components/layout/search/collections';
|
||||||
import FilterList from 'components/layout/search/filter';
|
import FilterList from 'components/layout/search/filter';
|
||||||
import { sorting } from 'lib/constants';
|
import { sorting } from 'lib/constants';
|
||||||
import { Suspense } from 'react';
|
import ChildrenWrapper from './children-wrapper';
|
||||||
|
|
||||||
export default function SearchLayout({ children }: { children: React.ReactNode }) {
|
export default function SearchLayout({ children }: { children: React.ReactNode }) {
|
||||||
return (
|
return (
|
||||||
<Suspense>
|
<>
|
||||||
<div className="mx-auto flex max-w-screen-2xl flex-col gap-8 px-4 pb-4 text-black dark:text-white md:flex-row">
|
<div className="mx-auto flex max-w-screen-2xl flex-col gap-8 px-4 pb-4 text-black md:flex-row dark:text-white">
|
||||||
<div className="order-first w-full flex-none md:max-w-[125px]">
|
<div className="order-first w-full flex-none md:max-w-[125px]">
|
||||||
<Collections />
|
<Collections />
|
||||||
</div>
|
</div>
|
||||||
<div className="order-last min-h-screen w-full md:order-none">{children}</div>
|
<div className="order-last min-h-screen w-full md:order-none">
|
||||||
|
<ChildrenWrapper>{children}</ChildrenWrapper>
|
||||||
|
</div>
|
||||||
<div className="order-none flex-none md:order-last md:w-[125px]">
|
<div className="order-none flex-none md:order-last md:w-[125px]">
|
||||||
<FilterList list={sorting} title="Sort by" />
|
<FilterList list={sorting} title="Sort by" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<Footer />
|
<Footer />
|
||||||
</Suspense>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ export default function Loading() {
|
|||||||
.fill(0)
|
.fill(0)
|
||||||
.map((_, index) => {
|
.map((_, index) => {
|
||||||
return (
|
return (
|
||||||
<Grid.Item key={index} className="animate-pulse bg-neutral-100 dark:bg-neutral-900" />
|
<Grid.Item key={index} className="animate-pulse bg-neutral-100 dark:bg-neutral-800" />
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ import ProductGridItems from 'components/layout/product-grid-items';
|
|||||||
import { defaultSort, sorting } from 'lib/constants';
|
import { defaultSort, sorting } from 'lib/constants';
|
||||||
import { getProducts } from 'lib/shopify';
|
import { getProducts } from 'lib/shopify';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
|
||||||
|
|
||||||
export const metadata = {
|
export const metadata = {
|
||||||
title: 'Search',
|
title: 'Search',
|
||||||
description: 'Search for products in the store.'
|
description: 'Search for products in the store.'
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { getCollections, getPages, getProducts } from 'lib/shopify';
|
import { getCollections, getPages, getProducts } from 'lib/shopify';
|
||||||
|
import { validateEnvironmentVariables } from 'lib/utils';
|
||||||
import { MetadataRoute } from 'next';
|
import { MetadataRoute } from 'next';
|
||||||
|
|
||||||
type Route = {
|
type Route = {
|
||||||
@@ -10,7 +11,11 @@ const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL
|
|||||||
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
|
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
|
||||||
: 'http://localhost:3000';
|
: 'http://localhost:3000';
|
||||||
|
|
||||||
|
export const dynamic = 'force-dynamic';
|
||||||
|
|
||||||
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
||||||
|
validateEnvironmentVariables();
|
||||||
|
|
||||||
const routesMap = [''].map((route) => ({
|
const routesMap = [''].map((route) => ({
|
||||||
url: `${baseUrl}${route}`,
|
url: `${baseUrl}${route}`,
|
||||||
lastModified: new Date().toISOString()
|
lastModified: new Date().toISOString()
|
||||||
|
|||||||
@@ -1,69 +1,118 @@
|
|||||||
'use server';
|
'use server';
|
||||||
|
|
||||||
|
import { TAGS } from 'lib/constants';
|
||||||
import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/shopify';
|
import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/shopify';
|
||||||
|
import { revalidateTag } from 'next/cache';
|
||||||
import { cookies } from 'next/headers';
|
import { cookies } from 'next/headers';
|
||||||
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
export const addItem = async (variantId: string | undefined): Promise<String | undefined> => {
|
export async function addItem(prevState: any, selectedVariantId: string | undefined) {
|
||||||
let cartId = cookies().get('cartId')?.value;
|
let cartId = cookies().get('cartId')?.value;
|
||||||
let cart;
|
|
||||||
|
|
||||||
if (cartId) {
|
if (!cartId || !selectedVariantId) {
|
||||||
cart = await getCart(cartId);
|
return 'Error adding item to cart';
|
||||||
}
|
|
||||||
|
|
||||||
if (!cartId || !cart) {
|
|
||||||
cart = await createCart();
|
|
||||||
cartId = cart.id;
|
|
||||||
cookies().set('cartId', cartId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!variantId) {
|
|
||||||
return 'Missing product variant ID';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await addToCart(cartId, [{ merchandiseId: variantId, quantity: 1 }]);
|
await addToCart(cartId, [{ merchandiseId: selectedVariantId, quantity: 1 }]);
|
||||||
|
revalidateTag(TAGS.cart);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return 'Error adding item to cart';
|
return 'Error adding item to cart';
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
export const removeItem = async (lineId: string): Promise<String | undefined> => {
|
export async function removeItem(prevState: any, merchandiseId: string) {
|
||||||
const cartId = cookies().get('cartId')?.value;
|
let cartId = cookies().get('cartId')?.value;
|
||||||
|
|
||||||
if (!cartId) {
|
if (!cartId) {
|
||||||
return 'Missing cart ID';
|
return 'Missing cart ID';
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await removeFromCart(cartId, [lineId]);
|
const cart = await getCart(cartId);
|
||||||
|
|
||||||
|
if (!cart) {
|
||||||
|
return 'Error fetching cart';
|
||||||
|
}
|
||||||
|
|
||||||
|
const lineItem = cart.lines.find((line) => line.merchandise.id === merchandiseId);
|
||||||
|
|
||||||
|
if (lineItem && lineItem.id) {
|
||||||
|
await removeFromCart(cartId, [lineItem.id]);
|
||||||
|
revalidateTag(TAGS.cart);
|
||||||
|
} else {
|
||||||
|
return 'Item not found in cart';
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return 'Error removing item from cart';
|
return 'Error removing item from cart';
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
export const updateItemQuantity = async ({
|
export async function updateItemQuantity(
|
||||||
lineId,
|
prevState: any,
|
||||||
variantId,
|
payload: {
|
||||||
quantity
|
merchandiseId: string;
|
||||||
}: {
|
|
||||||
lineId: string;
|
|
||||||
variantId: string;
|
|
||||||
quantity: number;
|
quantity: number;
|
||||||
}): Promise<String | undefined> => {
|
}
|
||||||
const cartId = cookies().get('cartId')?.value;
|
) {
|
||||||
|
let cartId = cookies().get('cartId')?.value;
|
||||||
|
|
||||||
if (!cartId) {
|
if (!cartId) {
|
||||||
return 'Missing cart ID';
|
return 'Missing cart ID';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { merchandiseId, quantity } = payload;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const cart = await getCart(cartId);
|
||||||
|
|
||||||
|
if (!cart) {
|
||||||
|
return 'Error fetching cart';
|
||||||
|
}
|
||||||
|
|
||||||
|
const lineItem = cart.lines.find((line) => line.merchandise.id === merchandiseId);
|
||||||
|
|
||||||
|
if (lineItem && lineItem.id) {
|
||||||
|
if (quantity === 0) {
|
||||||
|
await removeFromCart(cartId, [lineItem.id]);
|
||||||
|
} else {
|
||||||
await updateCart(cartId, [
|
await updateCart(cartId, [
|
||||||
{
|
{
|
||||||
id: lineId,
|
id: lineItem.id,
|
||||||
merchandiseId: variantId,
|
merchandiseId,
|
||||||
quantity
|
quantity
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
}
|
||||||
|
} else if (quantity > 0) {
|
||||||
|
// If the item doesn't exist in the cart and quantity > 0, add it
|
||||||
|
await addToCart(cartId, [{ merchandiseId, quantity }]);
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidateTag(TAGS.cart);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
return 'Error updating item quantity';
|
return 'Error updating item quantity';
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
export async function redirectToCheckout() {
|
||||||
|
let cartId = cookies().get('cartId')?.value;
|
||||||
|
|
||||||
|
if (!cartId) {
|
||||||
|
return 'Missing cart ID';
|
||||||
|
}
|
||||||
|
|
||||||
|
let cart = await getCart(cartId);
|
||||||
|
|
||||||
|
if (!cart) {
|
||||||
|
return 'Error fetching cart';
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect(cart.checkoutUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createCartAndSetCookie() {
|
||||||
|
let cart = await createCart();
|
||||||
|
cookies().set('cartId', cart.id!);
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,66 +3,86 @@
|
|||||||
import { PlusIcon } from '@heroicons/react/24/outline';
|
import { PlusIcon } from '@heroicons/react/24/outline';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import { addItem } from 'components/cart/actions';
|
import { addItem } from 'components/cart/actions';
|
||||||
import LoadingDots from 'components/loading-dots';
|
import { useProduct } from 'components/product/product-context';
|
||||||
import { ProductVariant } from 'lib/shopify/types';
|
import { Product, ProductVariant } from 'lib/shopify/types';
|
||||||
import { useRouter, useSearchParams } from 'next/navigation';
|
import { useFormState } from 'react-dom';
|
||||||
import { useTransition } from 'react';
|
import { useCart } from './cart-context';
|
||||||
|
|
||||||
export function AddToCart({
|
function SubmitButton({
|
||||||
variants,
|
availableForSale,
|
||||||
availableForSale
|
selectedVariantId
|
||||||
}: {
|
}: {
|
||||||
variants: ProductVariant[];
|
|
||||||
availableForSale: boolean;
|
availableForSale: boolean;
|
||||||
|
selectedVariantId: string | undefined;
|
||||||
}) {
|
}) {
|
||||||
const router = useRouter();
|
const buttonClasses =
|
||||||
const searchParams = useSearchParams();
|
'relative flex w-full items-center justify-center rounded-full bg-blue-600 p-4 tracking-wide text-white';
|
||||||
const [isPending, startTransition] = useTransition();
|
const disabledClasses = 'cursor-not-allowed opacity-60 hover:opacity-60';
|
||||||
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;
|
|
||||||
|
|
||||||
|
if (!availableForSale) {
|
||||||
return (
|
return (
|
||||||
<button
|
<button disabled className={clsx(buttonClasses, disabledClasses)}>
|
||||||
aria-label="Add item to cart"
|
Out Of Stock
|
||||||
disabled={isPending || !availableForSale || !selectedVariantId}
|
|
||||||
title={title}
|
|
||||||
onClick={() => {
|
|
||||||
// Safeguard in case someone messes with `disabled` in devtools.
|
|
||||||
if (!availableForSale || !selectedVariantId) return;
|
|
||||||
|
|
||||||
startTransition(async () => {
|
|
||||||
const error = await addItem(selectedVariantId);
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
// Trigger the error boundary in the root error.js
|
|
||||||
throw new Error(error.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
router.refresh();
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
className={clsx(
|
|
||||||
'relative flex w-full items-center justify-center rounded-full bg-blue-600 p-4 tracking-wide text-white hover:opacity-90',
|
|
||||||
{
|
|
||||||
'cursor-not-allowed opacity-60 hover:opacity-60': !availableForSale || !selectedVariantId,
|
|
||||||
'cursor-not-allowed': isPending
|
|
||||||
}
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<div className="absolute left-0 ml-4">
|
|
||||||
{!isPending ? <PlusIcon className="h-5" /> : <LoadingDots className="mb-3 bg-white" />}
|
|
||||||
</div>
|
|
||||||
<span>{availableForSale ? 'Add To Cart' : 'Out Of Stock'}</span>
|
|
||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(selectedVariantId);
|
||||||
|
if (!selectedVariantId) {
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
aria-label="Please select an option"
|
||||||
|
disabled
|
||||||
|
className={clsx(buttonClasses, disabledClasses)}
|
||||||
|
>
|
||||||
|
<div className="absolute left-0 ml-4">
|
||||||
|
<PlusIcon className="h-5" />
|
||||||
|
</div>
|
||||||
|
Add To Cart
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
aria-label="Add to cart"
|
||||||
|
className={clsx(buttonClasses, {
|
||||||
|
'hover:opacity-90': true
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
<div className="absolute left-0 ml-4">
|
||||||
|
<PlusIcon className="h-5" />
|
||||||
|
</div>
|
||||||
|
Add To Cart
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function AddToCart({ product }: { product: Product }) {
|
||||||
|
const { variants, availableForSale } = product;
|
||||||
|
const { addCartItem } = useCart();
|
||||||
|
const { state } = useProduct();
|
||||||
|
const [message, formAction] = useFormState(addItem, null);
|
||||||
|
|
||||||
|
const variant = variants.find((variant: ProductVariant) =>
|
||||||
|
variant.selectedOptions.every((option) => option.value === state[option.name.toLowerCase()])
|
||||||
|
);
|
||||||
|
const defaultVariantId = variants.length === 1 ? variants[0]?.id : undefined;
|
||||||
|
const selectedVariantId = variant?.id || defaultVariantId;
|
||||||
|
const actionWithVariant = formAction.bind(null, selectedVariantId);
|
||||||
|
const finalVariant = variants.find((variant) => variant.id === selectedVariantId)!;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<form
|
||||||
|
action={async () => {
|
||||||
|
addCartItem(finalVariant, product);
|
||||||
|
await actionWithVariant();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<SubmitButton availableForSale={availableForSale} selectedVariantId={selectedVariantId} />
|
||||||
|
<p aria-live="polite" className="sr-only" role="status">
|
||||||
|
{message}
|
||||||
|
</p>
|
||||||
|
</form>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
184
components/cart/cart-context.tsx
Normal file
184
components/cart/cart-context.tsx
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import type { Cart, CartItem, Product, ProductVariant } from 'lib/shopify/types';
|
||||||
|
import React, { createContext, use, useContext, useMemo, useOptimistic } from 'react';
|
||||||
|
|
||||||
|
type UpdateType = 'plus' | 'minus' | 'delete';
|
||||||
|
|
||||||
|
type CartAction =
|
||||||
|
| { type: 'UPDATE_ITEM'; payload: { merchandiseId: string; updateType: UpdateType } }
|
||||||
|
| { type: 'ADD_ITEM'; payload: { variant: ProductVariant; product: Product } };
|
||||||
|
|
||||||
|
type CartContextType = {
|
||||||
|
cart: Cart | undefined;
|
||||||
|
updateCartItem: (merchandiseId: string, updateType: UpdateType) => void;
|
||||||
|
addCartItem: (variant: ProductVariant, product: Product) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
const CartContext = createContext<CartContextType | undefined>(undefined);
|
||||||
|
|
||||||
|
function calculateItemCost(quantity: number, price: string): string {
|
||||||
|
return (Number(price) * quantity).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateCartItem(item: CartItem, updateType: UpdateType): CartItem | null {
|
||||||
|
if (updateType === 'delete') return null;
|
||||||
|
|
||||||
|
const newQuantity = updateType === 'plus' ? item.quantity + 1 : item.quantity - 1;
|
||||||
|
if (newQuantity === 0) return null;
|
||||||
|
|
||||||
|
const singleItemAmount = Number(item.cost.totalAmount.amount) / item.quantity;
|
||||||
|
const newTotalAmount = calculateItemCost(newQuantity, singleItemAmount.toString());
|
||||||
|
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
quantity: newQuantity,
|
||||||
|
cost: {
|
||||||
|
...item.cost,
|
||||||
|
totalAmount: {
|
||||||
|
...item.cost.totalAmount,
|
||||||
|
amount: newTotalAmount
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createOrUpdateCartItem(
|
||||||
|
existingItem: CartItem | undefined,
|
||||||
|
variant: ProductVariant,
|
||||||
|
product: Product
|
||||||
|
): CartItem {
|
||||||
|
const quantity = existingItem ? existingItem.quantity + 1 : 1;
|
||||||
|
const totalAmount = calculateItemCost(quantity, variant.price.amount);
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: existingItem?.id,
|
||||||
|
quantity,
|
||||||
|
cost: {
|
||||||
|
totalAmount: {
|
||||||
|
amount: totalAmount,
|
||||||
|
currencyCode: variant.price.currencyCode
|
||||||
|
}
|
||||||
|
},
|
||||||
|
merchandise: {
|
||||||
|
id: variant.id,
|
||||||
|
title: variant.title,
|
||||||
|
selectedOptions: variant.selectedOptions,
|
||||||
|
product: {
|
||||||
|
id: product.id,
|
||||||
|
handle: product.handle,
|
||||||
|
title: product.title,
|
||||||
|
featuredImage: product.featuredImage
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateCartTotals(lines: CartItem[]): Pick<Cart, 'totalQuantity' | 'cost'> {
|
||||||
|
const totalQuantity = lines.reduce((sum, item) => sum + item.quantity, 0);
|
||||||
|
const totalAmount = lines.reduce((sum, item) => sum + Number(item.cost.totalAmount.amount), 0);
|
||||||
|
const currencyCode = lines[0]?.cost.totalAmount.currencyCode ?? 'USD';
|
||||||
|
|
||||||
|
return {
|
||||||
|
totalQuantity,
|
||||||
|
cost: {
|
||||||
|
subtotalAmount: { amount: totalAmount.toString(), currencyCode },
|
||||||
|
totalAmount: { amount: totalAmount.toString(), currencyCode },
|
||||||
|
totalTaxAmount: { amount: '0', currencyCode }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createEmptyCart(): Cart {
|
||||||
|
return {
|
||||||
|
id: undefined,
|
||||||
|
checkoutUrl: '',
|
||||||
|
totalQuantity: 0,
|
||||||
|
lines: [],
|
||||||
|
cost: {
|
||||||
|
subtotalAmount: { amount: '0', currencyCode: 'USD' },
|
||||||
|
totalAmount: { amount: '0', currencyCode: 'USD' },
|
||||||
|
totalTaxAmount: { amount: '0', currencyCode: 'USD' }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function cartReducer(state: Cart | undefined, action: CartAction): Cart {
|
||||||
|
const currentCart = state || createEmptyCart();
|
||||||
|
|
||||||
|
switch (action.type) {
|
||||||
|
case 'UPDATE_ITEM': {
|
||||||
|
const { merchandiseId, updateType } = action.payload;
|
||||||
|
const updatedLines = currentCart.lines
|
||||||
|
.map((item) =>
|
||||||
|
item.merchandise.id === merchandiseId ? updateCartItem(item, updateType) : item
|
||||||
|
)
|
||||||
|
.filter(Boolean) as CartItem[];
|
||||||
|
|
||||||
|
if (updatedLines.length === 0) {
|
||||||
|
return {
|
||||||
|
...currentCart,
|
||||||
|
lines: [],
|
||||||
|
totalQuantity: 0,
|
||||||
|
cost: {
|
||||||
|
...currentCart.cost,
|
||||||
|
totalAmount: { ...currentCart.cost.totalAmount, amount: '0' }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ...currentCart, ...updateCartTotals(updatedLines), lines: updatedLines };
|
||||||
|
}
|
||||||
|
case 'ADD_ITEM': {
|
||||||
|
const { variant, product } = action.payload;
|
||||||
|
const existingItem = currentCart.lines.find((item) => item.merchandise.id === variant.id);
|
||||||
|
const updatedItem = createOrUpdateCartItem(existingItem, variant, product);
|
||||||
|
|
||||||
|
const updatedLines = existingItem
|
||||||
|
? currentCart.lines.map((item) => (item.merchandise.id === variant.id ? updatedItem : item))
|
||||||
|
: [...currentCart.lines, updatedItem];
|
||||||
|
|
||||||
|
return { ...currentCart, ...updateCartTotals(updatedLines), lines: updatedLines };
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return currentCart;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function CartProvider({
|
||||||
|
children,
|
||||||
|
cartPromise
|
||||||
|
}: {
|
||||||
|
children: React.ReactNode;
|
||||||
|
cartPromise: Promise<Cart | undefined>;
|
||||||
|
}) {
|
||||||
|
const initialCart = use(cartPromise);
|
||||||
|
const [optimisticCart, updateOptimisticCart] = useOptimistic(initialCart, cartReducer);
|
||||||
|
|
||||||
|
const updateCartItem = (merchandiseId: string, updateType: UpdateType) => {
|
||||||
|
updateOptimisticCart({ type: 'UPDATE_ITEM', payload: { merchandiseId, updateType } });
|
||||||
|
};
|
||||||
|
|
||||||
|
const addCartItem = (variant: ProductVariant, product: Product) => {
|
||||||
|
updateOptimisticCart({ type: 'ADD_ITEM', payload: { variant, product } });
|
||||||
|
};
|
||||||
|
|
||||||
|
const value = useMemo(
|
||||||
|
() => ({
|
||||||
|
cart: optimisticCart,
|
||||||
|
updateCartItem,
|
||||||
|
addCartItem
|
||||||
|
}),
|
||||||
|
[optimisticCart]
|
||||||
|
);
|
||||||
|
|
||||||
|
return <CartContext.Provider value={value}>{children}</CartContext.Provider>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useCart() {
|
||||||
|
const context = useContext(CartContext);
|
||||||
|
if (context === undefined) {
|
||||||
|
throw new Error('useCart must be used within a CartProvider');
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
}
|
||||||
@@ -1,44 +1,38 @@
|
|||||||
import { XMarkIcon } from '@heroicons/react/24/outline';
|
'use client';
|
||||||
import LoadingDots from 'components/loading-dots';
|
|
||||||
import { useRouter } from 'next/navigation';
|
|
||||||
|
|
||||||
import clsx from 'clsx';
|
import { XMarkIcon } from '@heroicons/react/24/outline';
|
||||||
import { removeItem } from 'components/cart/actions';
|
import { removeItem } from 'components/cart/actions';
|
||||||
import type { CartItem } from 'lib/shopify/types';
|
import type { CartItem } from 'lib/shopify/types';
|
||||||
import { useTransition } from 'react';
|
import { useFormState } from 'react-dom';
|
||||||
|
|
||||||
export default function DeleteItemButton({ item }: { item: CartItem }) {
|
export function DeleteItemButton({
|
||||||
const router = useRouter();
|
item,
|
||||||
const [isPending, startTransition] = useTransition();
|
optimisticUpdate
|
||||||
|
}: {
|
||||||
|
item: CartItem;
|
||||||
|
optimisticUpdate: any;
|
||||||
|
}) {
|
||||||
|
const [message, formAction] = useFormState(removeItem, null);
|
||||||
|
const merchandiseId = item.merchandise.id;
|
||||||
|
const actionWithVariant = formAction.bind(null, merchandiseId);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<form
|
||||||
aria-label="Remove cart item"
|
action={async () => {
|
||||||
onClick={() => {
|
optimisticUpdate(merchandiseId, 'delete');
|
||||||
startTransition(async () => {
|
await actionWithVariant();
|
||||||
const error = await removeItem(item.id);
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
// Trigger the error boundary in the root error.js
|
|
||||||
throw new Error(error.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
router.refresh();
|
|
||||||
});
|
|
||||||
}}
|
}}
|
||||||
disabled={isPending}
|
|
||||||
className={clsx(
|
|
||||||
'ease flex h-[17px] w-[17px] items-center justify-center rounded-full bg-neutral-500 transition-all duration-200',
|
|
||||||
{
|
|
||||||
'cursor-not-allowed px-0': isPending
|
|
||||||
}
|
|
||||||
)}
|
|
||||||
>
|
>
|
||||||
{isPending ? (
|
<button
|
||||||
<LoadingDots className="bg-white" />
|
type="submit"
|
||||||
) : (
|
aria-label="Remove cart item"
|
||||||
<XMarkIcon className="hover:text-accent-3 mx-[1px] h-4 w-4 text-white dark:text-black" />
|
className="flex h-[24px] w-[24px] items-center justify-center rounded-full bg-neutral-500"
|
||||||
)}
|
>
|
||||||
|
<XMarkIcon className="mx-[1px] h-4 w-4 text-white dark:text-black" />
|
||||||
</button>
|
</button>
|
||||||
|
<p aria-live="polite" className="sr-only" role="status">
|
||||||
|
{message}
|
||||||
|
</p>
|
||||||
|
</form>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,56 +1,24 @@
|
|||||||
import { useRouter } from 'next/navigation';
|
'use client';
|
||||||
import { useTransition } from 'react';
|
|
||||||
|
|
||||||
import { MinusIcon, PlusIcon } from '@heroicons/react/24/outline';
|
import { MinusIcon, PlusIcon } from '@heroicons/react/24/outline';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import { removeItem, updateItemQuantity } from 'components/cart/actions';
|
import { updateItemQuantity } from 'components/cart/actions';
|
||||||
import LoadingDots from 'components/loading-dots';
|
|
||||||
import type { CartItem } from 'lib/shopify/types';
|
import type { CartItem } from 'lib/shopify/types';
|
||||||
|
import { useFormState } from 'react-dom';
|
||||||
|
|
||||||
export default function EditItemQuantityButton({
|
function SubmitButton({ type }: { type: 'plus' | 'minus' }) {
|
||||||
item,
|
|
||||||
type
|
|
||||||
}: {
|
|
||||||
item: CartItem;
|
|
||||||
type: 'plus' | 'minus';
|
|
||||||
}) {
|
|
||||||
const router = useRouter();
|
|
||||||
const [isPending, startTransition] = useTransition();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
|
type="submit"
|
||||||
aria-label={type === 'plus' ? 'Increase item quantity' : 'Reduce item quantity'}
|
aria-label={type === 'plus' ? 'Increase item quantity' : 'Reduce item quantity'}
|
||||||
onClick={() => {
|
|
||||||
startTransition(async () => {
|
|
||||||
const error =
|
|
||||||
type === 'minus' && item.quantity - 1 === 0
|
|
||||||
? await removeItem(item.id)
|
|
||||||
: await updateItemQuantity({
|
|
||||||
lineId: item.id,
|
|
||||||
variantId: item.merchandise.id,
|
|
||||||
quantity: type === 'plus' ? item.quantity + 1 : item.quantity - 1
|
|
||||||
});
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
// Trigger the error boundary in the root error.js
|
|
||||||
throw new Error(error.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
router.refresh();
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
disabled={isPending}
|
|
||||||
className={clsx(
|
className={clsx(
|
||||||
'ease flex h-full min-w-[36px] max-w-[36px] flex-none items-center justify-center rounded-full px-2 transition-all duration-200 hover:border-neutral-800 hover:opacity-80',
|
'ease flex h-full min-w-[36px] max-w-[36px] flex-none items-center justify-center rounded-full p-2 transition-all duration-200 hover:border-neutral-800 hover:opacity-80',
|
||||||
{
|
{
|
||||||
'cursor-not-allowed': isPending,
|
|
||||||
'ml-auto': type === 'minus'
|
'ml-auto': type === 'minus'
|
||||||
}
|
}
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{isPending ? (
|
{type === 'plus' ? (
|
||||||
<LoadingDots className="bg-black dark:bg-white" />
|
|
||||||
) : type === 'plus' ? (
|
|
||||||
<PlusIcon className="h-4 w-4 dark:text-neutral-500" />
|
<PlusIcon className="h-4 w-4 dark:text-neutral-500" />
|
||||||
) : (
|
) : (
|
||||||
<MinusIcon className="h-4 w-4 dark:text-neutral-500" />
|
<MinusIcon className="h-4 w-4 dark:text-neutral-500" />
|
||||||
@@ -58,3 +26,34 @@ export default function EditItemQuantityButton({
|
|||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function EditItemQuantityButton({
|
||||||
|
item,
|
||||||
|
type,
|
||||||
|
optimisticUpdate
|
||||||
|
}: {
|
||||||
|
item: CartItem;
|
||||||
|
type: 'plus' | 'minus';
|
||||||
|
optimisticUpdate: any;
|
||||||
|
}) {
|
||||||
|
const [message, formAction] = useFormState(updateItemQuantity, null);
|
||||||
|
const payload = {
|
||||||
|
merchandiseId: item.merchandise.id,
|
||||||
|
quantity: type === 'plus' ? item.quantity + 1 : item.quantity - 1
|
||||||
|
};
|
||||||
|
const actionWithVariant = formAction.bind(null, payload);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<form
|
||||||
|
action={async () => {
|
||||||
|
optimisticUpdate(payload.merchandiseId, type);
|
||||||
|
await actionWithVariant();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<SubmitButton type={type} />
|
||||||
|
<p aria-live="polite" className="sr-only" role="status">
|
||||||
|
{message}
|
||||||
|
</p>
|
||||||
|
</form>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
import { getCart } from 'lib/shopify';
|
|
||||||
import { cookies } from 'next/headers';
|
|
||||||
import CartModal from './modal';
|
|
||||||
|
|
||||||
export default async function Cart() {
|
|
||||||
const cartId = cookies().get('cartId')?.value;
|
|
||||||
let cart;
|
|
||||||
|
|
||||||
if (cartId) {
|
|
||||||
cart = await getCart(cartId);
|
|
||||||
}
|
|
||||||
|
|
||||||
return <CartModal cart={cart} />;
|
|
||||||
}
|
|
||||||
@@ -2,37 +2,47 @@
|
|||||||
|
|
||||||
import { Dialog, Transition } from '@headlessui/react';
|
import { Dialog, Transition } from '@headlessui/react';
|
||||||
import { ShoppingCartIcon } from '@heroicons/react/24/outline';
|
import { ShoppingCartIcon } from '@heroicons/react/24/outline';
|
||||||
|
import LoadingDots from 'components/loading-dots';
|
||||||
import Price from 'components/price';
|
import Price from 'components/price';
|
||||||
import { DEFAULT_OPTION } from 'lib/constants';
|
import { DEFAULT_OPTION } from 'lib/constants';
|
||||||
import type { Cart } from 'lib/shopify/types';
|
|
||||||
import { createUrl } from 'lib/utils';
|
import { createUrl } from 'lib/utils';
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { Fragment, useEffect, useRef, useState } from 'react';
|
import { Fragment, useEffect, useRef, useState } from 'react';
|
||||||
|
import { useFormStatus } from 'react-dom';
|
||||||
|
import { createCartAndSetCookie, redirectToCheckout } from './actions';
|
||||||
|
import { useCart } from './cart-context';
|
||||||
import CloseCart from './close-cart';
|
import CloseCart from './close-cart';
|
||||||
import DeleteItemButton from './delete-item-button';
|
import { DeleteItemButton } from './delete-item-button';
|
||||||
import EditItemQuantityButton from './edit-item-quantity-button';
|
import { EditItemQuantityButton } from './edit-item-quantity-button';
|
||||||
import OpenCart from './open-cart';
|
import OpenCart from './open-cart';
|
||||||
|
|
||||||
type MerchandiseSearchParams = {
|
type MerchandiseSearchParams = {
|
||||||
[key: string]: string;
|
[key: string]: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function CartModal({ cart }: { cart: Cart | undefined }) {
|
export default function CartModal() {
|
||||||
|
const { cart, updateCartItem } = useCart();
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const quantityRef = useRef(cart?.totalQuantity);
|
const quantityRef = useRef(cart?.totalQuantity);
|
||||||
const openCart = () => setIsOpen(true);
|
const openCart = () => setIsOpen(true);
|
||||||
const closeCart = () => setIsOpen(false);
|
const closeCart = () => setIsOpen(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Open cart modal when quantity changes.
|
if (!cart) {
|
||||||
if (cart?.totalQuantity !== quantityRef.current) {
|
createCartAndSetCookie();
|
||||||
// But only if it's not already open (quantity also changes when editing items in cart).
|
}
|
||||||
|
}, [cart]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (
|
||||||
|
cart?.totalQuantity &&
|
||||||
|
cart?.totalQuantity !== quantityRef.current &&
|
||||||
|
cart?.totalQuantity > 0
|
||||||
|
) {
|
||||||
if (!isOpen) {
|
if (!isOpen) {
|
||||||
setIsOpen(true);
|
setIsOpen(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always update the quantity reference
|
|
||||||
quantityRef.current = cart?.totalQuantity;
|
quantityRef.current = cart?.totalQuantity;
|
||||||
}
|
}
|
||||||
}, [isOpen, cart?.totalQuantity, quantityRef]);
|
}, [isOpen, cart?.totalQuantity, quantityRef]);
|
||||||
@@ -64,10 +74,9 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
|
|||||||
leaveFrom="translate-x-0"
|
leaveFrom="translate-x-0"
|
||||||
leaveTo="translate-x-full"
|
leaveTo="translate-x-full"
|
||||||
>
|
>
|
||||||
<Dialog.Panel className="fixed bottom-0 right-0 top-0 flex h-full w-full flex-col border-l border-neutral-200 bg-white/80 p-6 text-black backdrop-blur-xl dark:border-neutral-700 dark:bg-black/80 dark:text-white md:w-[390px]">
|
<Dialog.Panel className="fixed bottom-0 right-0 top-0 flex h-full w-full flex-col border-l border-neutral-200 bg-white/80 p-6 text-black backdrop-blur-xl md:w-[390px] dark:border-neutral-700 dark:bg-black/80 dark:text-white">
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between">
|
||||||
<p className="text-lg font-semibold">My Cart</p>
|
<p className="text-lg font-semibold">My Cart</p>
|
||||||
|
|
||||||
<button aria-label="Close cart" onClick={closeCart}>
|
<button aria-label="Close cart" onClick={closeCart}>
|
||||||
<CloseCart />
|
<CloseCart />
|
||||||
</button>
|
</button>
|
||||||
@@ -81,7 +90,11 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
|
|||||||
) : (
|
) : (
|
||||||
<div className="flex h-full flex-col justify-between overflow-hidden p-1">
|
<div className="flex h-full flex-col justify-between overflow-hidden p-1">
|
||||||
<ul className="flex-grow overflow-auto py-4">
|
<ul className="flex-grow overflow-auto py-4">
|
||||||
{cart.lines.map((item, i) => {
|
{cart.lines
|
||||||
|
.sort((a, b) =>
|
||||||
|
a.merchandise.product.title.localeCompare(b.merchandise.product.title)
|
||||||
|
)
|
||||||
|
.map((item, i) => {
|
||||||
const merchandiseSearchParams = {} as MerchandiseSearchParams;
|
const merchandiseSearchParams = {} as MerchandiseSearchParams;
|
||||||
|
|
||||||
item.merchandise.selectedOptions.forEach(({ name, value }) => {
|
item.merchandise.selectedOptions.forEach(({ name, value }) => {
|
||||||
@@ -101,15 +114,11 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
|
|||||||
className="flex w-full flex-col border-b border-neutral-300 dark:border-neutral-700"
|
className="flex w-full flex-col border-b border-neutral-300 dark:border-neutral-700"
|
||||||
>
|
>
|
||||||
<div className="relative flex w-full flex-row justify-between px-1 py-4">
|
<div className="relative flex w-full flex-row justify-between px-1 py-4">
|
||||||
<div className="absolute z-40 -mt-2 ml-[55px]">
|
<div className="absolute z-40 -ml-1 -mt-2">
|
||||||
<DeleteItemButton item={item} />
|
<DeleteItemButton item={item} optimisticUpdate={updateCartItem} />
|
||||||
</div>
|
</div>
|
||||||
<Link
|
<div className="flex flex-row">
|
||||||
href={merchandiseUrl}
|
<div className="relative h-16 w-16 overflow-hidden rounded-md border border-neutral-300 bg-neutral-300 dark:border-neutral-700 dark:bg-neutral-900 dark:hover:bg-neutral-800">
|
||||||
onClick={closeCart}
|
|
||||||
className="z-30 flex flex-row space-x-4"
|
|
||||||
>
|
|
||||||
<div className="relative h-16 w-16 cursor-pointer overflow-hidden rounded-md border border-neutral-300 bg-neutral-300 dark:border-neutral-700 dark:bg-neutral-900 dark:hover:bg-neutral-800">
|
|
||||||
<Image
|
<Image
|
||||||
className="h-full w-full object-cover"
|
className="h-full w-full object-cover"
|
||||||
width={64}
|
width={64}
|
||||||
@@ -121,7 +130,11 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
|
|||||||
src={item.merchandise.product.featuredImage.url}
|
src={item.merchandise.product.featuredImage.url}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
<Link
|
||||||
|
href={merchandiseUrl}
|
||||||
|
onClick={closeCart}
|
||||||
|
className="z-30 ml-2 flex flex-row space-x-4"
|
||||||
|
>
|
||||||
<div className="flex flex-1 flex-col text-base">
|
<div className="flex flex-1 flex-col text-base">
|
||||||
<span className="leading-tight">
|
<span className="leading-tight">
|
||||||
{item.merchandise.product.title}
|
{item.merchandise.product.title}
|
||||||
@@ -133,6 +146,7 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
|
|||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
</Link>
|
</Link>
|
||||||
|
</div>
|
||||||
<div className="flex h-16 flex-col justify-between">
|
<div className="flex h-16 flex-col justify-between">
|
||||||
<Price
|
<Price
|
||||||
className="flex justify-end space-y-2 text-right text-sm"
|
className="flex justify-end space-y-2 text-right text-sm"
|
||||||
@@ -140,11 +154,19 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
|
|||||||
currencyCode={item.cost.totalAmount.currencyCode}
|
currencyCode={item.cost.totalAmount.currencyCode}
|
||||||
/>
|
/>
|
||||||
<div className="ml-auto flex h-9 flex-row items-center rounded-full border border-neutral-200 dark:border-neutral-700">
|
<div className="ml-auto flex h-9 flex-row items-center rounded-full border border-neutral-200 dark:border-neutral-700">
|
||||||
<EditItemQuantityButton item={item} type="minus" />
|
<EditItemQuantityButton
|
||||||
|
item={item}
|
||||||
|
type="minus"
|
||||||
|
optimisticUpdate={updateCartItem}
|
||||||
|
/>
|
||||||
<p className="w-6 text-center">
|
<p className="w-6 text-center">
|
||||||
<span className="w-full text-sm">{item.quantity}</span>
|
<span className="w-full text-sm">{item.quantity}</span>
|
||||||
</p>
|
</p>
|
||||||
<EditItemQuantityButton item={item} type="plus" />
|
<EditItemQuantityButton
|
||||||
|
item={item}
|
||||||
|
type="plus"
|
||||||
|
optimisticUpdate={updateCartItem}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -174,12 +196,9 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<a
|
<form action={redirectToCheckout}>
|
||||||
href={cart.checkoutUrl}
|
<CheckoutButton />
|
||||||
className="block w-full rounded-full bg-blue-600 p-3 text-center text-sm font-medium text-white opacity-90 hover:opacity-100"
|
</form>
|
||||||
>
|
|
||||||
Proceed to Checkout
|
|
||||||
</a>
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</Dialog.Panel>
|
</Dialog.Panel>
|
||||||
@@ -189,3 +208,17 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
|
|||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function CheckoutButton() {
|
||||||
|
const { pending } = useFormStatus();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
className="block w-full rounded-full bg-blue-600 p-3 text-center text-sm font-medium text-white opacity-90 hover:opacity-100"
|
||||||
|
type="submit"
|
||||||
|
disabled={pending}
|
||||||
|
>
|
||||||
|
{pending ? <LoadingDots className="bg-white" /> : 'Proceed to Checkout'}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,7 +16,11 @@ function ThreeItemGridItem({
|
|||||||
<div
|
<div
|
||||||
className={size === 'full' ? 'md:col-span-4 md:row-span-2' : 'md:col-span-2 md:row-span-1'}
|
className={size === 'full' ? 'md:col-span-4 md:row-span-2' : 'md:col-span-2 md:row-span-1'}
|
||||||
>
|
>
|
||||||
<Link className="relative block aspect-square h-full w-full" href={`/product/${item.handle}`}>
|
<Link
|
||||||
|
className="relative block aspect-square h-full w-full"
|
||||||
|
href={`/product/${item.handle}`}
|
||||||
|
prefetch={true}
|
||||||
|
>
|
||||||
<GridTileImage
|
<GridTileImage
|
||||||
src={item.featuredImage.url}
|
src={item.featuredImage.url}
|
||||||
fill
|
fill
|
||||||
@@ -48,7 +52,7 @@ export async function ThreeItemGrid() {
|
|||||||
const [firstProduct, secondProduct, thirdProduct] = homepageItems;
|
const [firstProduct, secondProduct, thirdProduct] = homepageItems;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="mx-auto grid max-w-screen-2xl gap-4 px-4 pb-4 md:grid-cols-6 md:grid-rows-2">
|
<section className="mx-auto grid max-w-screen-2xl gap-4 px-4 pb-4 md:grid-cols-6 md:grid-rows-2 lg:max-h-[calc(100vh-200px)]">
|
||||||
<ThreeItemGridItem size="full" item={firstProduct} priority={true} />
|
<ThreeItemGridItem size="full" item={firstProduct} priority={true} />
|
||||||
<ThreeItemGridItem size="half" item={secondProduct} priority={true} />
|
<ThreeItemGridItem size="half" item={secondProduct} priority={true} />
|
||||||
<ThreeItemGridItem size="half" item={thirdProduct} />
|
<ThreeItemGridItem size="half" item={thirdProduct} />
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ export function GridTileImage({
|
|||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{props.src ? (
|
{props.src ? (
|
||||||
// eslint-disable-next-line jsx-a11y/alt-text -- `alt` is inherited from `props`, which is being enforced with TypeScript
|
|
||||||
<Image
|
<Image
|
||||||
className={clsx('relative h-full w-full object-contain', {
|
className={clsx('relative h-full w-full object-contain', {
|
||||||
'transition duration-300 ease-in-out group-hover:scale-105': isInteractive
|
'transition duration-300 ease-in-out group-hover:scale-105': isInteractive
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import Link from 'next/link';
|
|||||||
import { usePathname } from 'next/navigation';
|
import { usePathname } from 'next/navigation';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
const FooterMenuItem = ({ item }: { item: Menu }) => {
|
export function FooterMenuItem({ item }: { item: Menu }) {
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
const [active, setActive] = useState(pathname === item.path);
|
const [active, setActive] = useState(pathname === item.path);
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ const FooterMenuItem = ({ item }: { item: Menu }) => {
|
|||||||
<Link
|
<Link
|
||||||
href={item.path}
|
href={item.path}
|
||||||
className={clsx(
|
className={clsx(
|
||||||
'block p-2 text-lg underline-offset-4 hover:text-black hover:underline dark:hover:text-neutral-300 md:inline-block md:text-sm',
|
'block p-2 text-lg underline-offset-4 hover:text-black hover:underline md:inline-block md:text-sm dark:hover:text-neutral-300',
|
||||||
{
|
{
|
||||||
'text-black dark:text-neutral-300': active
|
'text-black dark:text-neutral-300': active
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ const FooterMenuItem = ({ item }: { item: Menu }) => {
|
|||||||
</Link>
|
</Link>
|
||||||
</li>
|
</li>
|
||||||
);
|
);
|
||||||
};
|
}
|
||||||
|
|
||||||
export default function FooterMenu({ menu }: { menu: Menu[] }) {
|
export default function FooterMenu({ menu }: { menu: Menu[] }) {
|
||||||
if (!menu.length) return null;
|
if (!menu.length) return null;
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ export default async function Footer() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<footer className="text-sm text-neutral-500 dark:text-neutral-400">
|
<footer className="text-sm text-neutral-500 dark:text-neutral-400">
|
||||||
<div className="mx-auto flex w-full max-w-7xl flex-col gap-6 border-t border-neutral-200 px-6 py-12 text-sm dark:border-neutral-700 md:flex-row md:gap-12 md:px-4 xl:px-0">
|
<div className="mx-auto flex w-full max-w-7xl flex-col gap-6 border-t border-neutral-200 px-6 py-12 text-sm md:flex-row md:gap-12 md:px-4 min-[1320px]:px-0 dark:border-neutral-700">
|
||||||
<div>
|
<div>
|
||||||
<Link className="flex items-center gap-2 text-black dark:text-white md:pt-1" href="/">
|
<Link className="flex items-center gap-2 text-black md:pt-1 dark:text-white" href="/">
|
||||||
<LogoSquare size="sm" />
|
<LogoSquare size="sm" />
|
||||||
<span className="uppercase">{SITE_NAME}</span>
|
<span className="uppercase">{SITE_NAME}</span>
|
||||||
</Link>
|
</Link>
|
||||||
@@ -50,17 +50,18 @@ export default async function Footer() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="border-t border-neutral-200 py-6 text-sm dark:border-neutral-700">
|
<div className="border-t border-neutral-200 py-6 text-sm dark:border-neutral-700">
|
||||||
<div className="mx-auto flex w-full max-w-7xl flex-col items-center gap-1 px-4 md:flex-row md:gap-0 md:px-4 xl:px-0">
|
<div className="mx-auto flex w-full max-w-7xl flex-col items-center gap-1 px-4 md:flex-row md:gap-0 md:px-4 min-[1320px]:px-0">
|
||||||
<p>
|
<p>
|
||||||
© {copyrightDate} {copyrightName}
|
© {copyrightDate} {copyrightName}
|
||||||
{copyrightName.length && !copyrightName.endsWith('.') ? '.' : ''} All rights reserved.
|
{copyrightName.length && !copyrightName.endsWith('.') ? '.' : ''} All rights reserved.
|
||||||
</p>
|
</p>
|
||||||
<hr className="mx-4 hidden h-4 w-[1px] border-l border-neutral-400 md:inline-block" />
|
<hr className="mx-4 hidden h-4 w-[1px] border-l border-neutral-400 md:inline-block" />
|
||||||
<p>Designed in California</p>
|
<p>
|
||||||
|
<a href="https://github.com/vercel/commerce">View the source</a>
|
||||||
|
</p>
|
||||||
<p className="md:ml-auto">
|
<p className="md:ml-auto">
|
||||||
Crafted by{' '}
|
|
||||||
<a href="https://vercel.com" className="text-black dark:text-white">
|
<a href="https://vercel.com" className="text-black dark:text-white">
|
||||||
▲ Vercel
|
Created by ▲ Vercel
|
||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,25 +1,31 @@
|
|||||||
import Cart from 'components/cart';
|
import CartModal from 'components/cart/modal';
|
||||||
import OpenCart from 'components/cart/open-cart';
|
|
||||||
import LogoSquare from 'components/logo-square';
|
import LogoSquare from 'components/logo-square';
|
||||||
import { getMenu } from 'lib/shopify';
|
import { getMenu } from 'lib/shopify';
|
||||||
import { Menu } from 'lib/shopify/types';
|
import { Menu } from 'lib/shopify/types';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { Suspense } from 'react';
|
import { Suspense } from 'react';
|
||||||
import MobileMenu from './mobile-menu';
|
import MobileMenu from './mobile-menu';
|
||||||
import Search from './search';
|
import Search, { SearchSkeleton } from './search';
|
||||||
|
|
||||||
const { SITE_NAME } = process.env;
|
const { SITE_NAME } = process.env;
|
||||||
|
|
||||||
export default async function Navbar() {
|
export async function Navbar() {
|
||||||
const menu = await getMenu('next-js-frontend-header-menu');
|
const menu = await getMenu('next-js-frontend-header-menu');
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<nav className="relative flex items-center justify-between p-4 lg:px-6">
|
<nav className="relative flex items-center justify-between p-4 lg:px-6">
|
||||||
<div className="block flex-none md:hidden">
|
<div className="block flex-none md:hidden">
|
||||||
|
<Suspense fallback={null}>
|
||||||
<MobileMenu menu={menu} />
|
<MobileMenu menu={menu} />
|
||||||
|
</Suspense>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex w-full items-center">
|
<div className="flex w-full items-center">
|
||||||
<div className="flex w-full md:w-1/3">
|
<div className="flex w-full md:w-1/3">
|
||||||
<Link href="/" className="mr-2 flex w-full items-center justify-center md:w-auto lg:mr-6">
|
<Link
|
||||||
|
href="/"
|
||||||
|
prefetch={true}
|
||||||
|
className="mr-2 flex w-full items-center justify-center md:w-auto lg:mr-6"
|
||||||
|
>
|
||||||
<LogoSquare />
|
<LogoSquare />
|
||||||
<div className="ml-2 flex-none text-sm font-medium uppercase md:hidden lg:block">
|
<div className="ml-2 flex-none text-sm font-medium uppercase md:hidden lg:block">
|
||||||
{SITE_NAME}
|
{SITE_NAME}
|
||||||
@@ -31,6 +37,7 @@ export default async function Navbar() {
|
|||||||
<li key={item.title}>
|
<li key={item.title}>
|
||||||
<Link
|
<Link
|
||||||
href={item.path}
|
href={item.path}
|
||||||
|
prefetch={item.path === '/search' ? undefined : true}
|
||||||
className="text-neutral-500 underline-offset-4 hover:text-black hover:underline dark:text-neutral-400 dark:hover:text-neutral-300"
|
className="text-neutral-500 underline-offset-4 hover:text-black hover:underline dark:text-neutral-400 dark:hover:text-neutral-300"
|
||||||
>
|
>
|
||||||
{item.title}
|
{item.title}
|
||||||
@@ -41,12 +48,12 @@ export default async function Navbar() {
|
|||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
<div className="hidden justify-center md:flex md:w-1/3">
|
<div className="hidden justify-center md:flex md:w-1/3">
|
||||||
|
<Suspense fallback={<SearchSkeleton />}>
|
||||||
<Search />
|
<Search />
|
||||||
|
</Suspense>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex justify-end md:w-1/3">
|
<div className="flex justify-end md:w-1/3">
|
||||||
<Suspense fallback={<OpenCart />}>
|
<CartModal />
|
||||||
<Cart />
|
|
||||||
</Suspense>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
import { Dialog, Transition } from '@headlessui/react';
|
import { Dialog, Transition } from '@headlessui/react';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { usePathname, useSearchParams } from 'next/navigation';
|
import { usePathname, useSearchParams } from 'next/navigation';
|
||||||
import { Fragment, useEffect, useState } from 'react';
|
import { Fragment, Suspense, useEffect, useState } from 'react';
|
||||||
|
|
||||||
import { Bars3Icon, XMarkIcon } from '@heroicons/react/24/outline';
|
import { Bars3Icon, XMarkIcon } from '@heroicons/react/24/outline';
|
||||||
import { Menu } from 'lib/shopify/types';
|
import { Menu } from 'lib/shopify/types';
|
||||||
import Search from './search';
|
import Search, { SearchSkeleton } from './search';
|
||||||
|
|
||||||
export default function MobileMenu({ menu }: { menu: Menu[] }) {
|
export default function MobileMenu({ menu }: { menu: Menu[] }) {
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
@@ -35,7 +35,7 @@ export default function MobileMenu({ menu }: { menu: Menu[] }) {
|
|||||||
<button
|
<button
|
||||||
onClick={openMobileMenu}
|
onClick={openMobileMenu}
|
||||||
aria-label="Open mobile menu"
|
aria-label="Open mobile menu"
|
||||||
className="flex h-11 w-11 items-center justify-center rounded-md border border-neutral-200 text-black transition-colors dark:border-neutral-700 dark:text-white md:hidden"
|
className="flex h-11 w-11 items-center justify-center rounded-md border border-neutral-200 text-black transition-colors md:hidden dark:border-neutral-700 dark:text-white"
|
||||||
>
|
>
|
||||||
<Bars3Icon className="h-4" />
|
<Bars3Icon className="h-4" />
|
||||||
</button>
|
</button>
|
||||||
@@ -72,7 +72,9 @@ export default function MobileMenu({ menu }: { menu: Menu[] }) {
|
|||||||
</button>
|
</button>
|
||||||
|
|
||||||
<div className="mb-4 w-full">
|
<div className="mb-4 w-full">
|
||||||
|
<Suspense fallback={<SearchSkeleton />}>
|
||||||
<Search />
|
<Search />
|
||||||
|
</Suspense>
|
||||||
</div>
|
</div>
|
||||||
{menu.length ? (
|
{menu.length ? (
|
||||||
<ul className="flex w-full flex-col">
|
<ul className="flex w-full flex-col">
|
||||||
@@ -81,7 +83,7 @@ export default function MobileMenu({ menu }: { menu: Menu[] }) {
|
|||||||
className="py-2 text-xl text-black transition-colors hover:text-neutral-500 dark:text-white"
|
className="py-2 text-xl text-black transition-colors hover:text-neutral-500 dark:text-white"
|
||||||
key={item.title}
|
key={item.title}
|
||||||
>
|
>
|
||||||
<Link href={item.path} onClick={closeMobileMenu}>
|
<Link href={item.path} prefetch={true} onClick={closeMobileMenu}>
|
||||||
{item.title}
|
{item.title}
|
||||||
</Link>
|
</Link>
|
||||||
</li>
|
</li>
|
||||||
|
|||||||
@@ -1,45 +1,35 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useRouter, useSearchParams } from 'next/navigation';
|
|
||||||
import { useEffect, useState } from 'react';
|
|
||||||
|
|
||||||
import { MagnifyingGlassIcon } from '@heroicons/react/24/outline';
|
import { MagnifyingGlassIcon } from '@heroicons/react/24/outline';
|
||||||
import { createUrl } from 'lib/utils';
|
import Form from 'next/form';
|
||||||
|
import { useSearchParams } from 'next/navigation';
|
||||||
|
|
||||||
export default function Search() {
|
export default function Search() {
|
||||||
const router = useRouter();
|
|
||||||
const searchParams = useSearchParams();
|
const searchParams = useSearchParams();
|
||||||
const [searchValue, setSearchValue] = useState('');
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setSearchValue(searchParams?.get('q') || '');
|
|
||||||
}, [searchParams, setSearchValue]);
|
|
||||||
|
|
||||||
function onSubmit(e: React.FormEvent<HTMLFormElement>) {
|
|
||||||
e.preventDefault();
|
|
||||||
|
|
||||||
const val = e.target as HTMLFormElement;
|
|
||||||
const search = val.search as HTMLInputElement;
|
|
||||||
const newParams = new URLSearchParams(searchParams.toString());
|
|
||||||
|
|
||||||
if (search.value) {
|
|
||||||
newParams.set('q', search.value);
|
|
||||||
} else {
|
|
||||||
newParams.delete('q');
|
|
||||||
}
|
|
||||||
|
|
||||||
router.push(createUrl('/search', newParams));
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form onSubmit={onSubmit} className="w-max-[550px] relative w-full lg:w-80 xl:w-full">
|
<Form action="/search" className="w-max-[550px] relative w-full lg:w-80 xl:w-full">
|
||||||
<input
|
<input
|
||||||
|
key={searchParams?.get('q')}
|
||||||
type="text"
|
type="text"
|
||||||
name="search"
|
name="q"
|
||||||
placeholder="Search for products..."
|
placeholder="Search for products..."
|
||||||
autoComplete="off"
|
autoComplete="off"
|
||||||
value={searchValue}
|
defaultValue={searchParams?.get('q') || ''}
|
||||||
onChange={(e) => setSearchValue(e.target.value)}
|
className="text-md w-full rounded-lg border bg-white px-4 py-2 text-black placeholder:text-neutral-500 md:text-sm dark:border-neutral-800 dark:bg-transparent dark:text-white dark:placeholder:text-neutral-400"
|
||||||
|
/>
|
||||||
|
<div className="absolute right-0 top-0 mr-3 flex h-full items-center">
|
||||||
|
<MagnifyingGlassIcon className="h-4" />
|
||||||
|
</div>
|
||||||
|
</Form>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SearchSkeleton() {
|
||||||
|
return (
|
||||||
|
<form className="w-max-[550px] relative w-full lg:w-80 xl:w-full">
|
||||||
|
<input
|
||||||
|
placeholder="Search for products..."
|
||||||
className="w-full rounded-lg border bg-white px-4 py-2 text-sm text-black placeholder:text-neutral-500 dark:border-neutral-800 dark:bg-transparent dark:text-white dark:placeholder:text-neutral-400"
|
className="w-full rounded-lg border bg-white px-4 py-2 text-sm text-black placeholder:text-neutral-500 dark:border-neutral-800 dark:bg-transparent dark:text-white dark:placeholder:text-neutral-400"
|
||||||
/>
|
/>
|
||||||
<div className="absolute right-0 top-0 mr-3 flex h-full items-center">
|
<div className="absolute right-0 top-0 mr-3 flex h-full items-center">
|
||||||
|
|||||||
@@ -8,7 +8,11 @@ export default function ProductGridItems({ products }: { products: Product[] })
|
|||||||
<>
|
<>
|
||||||
{products.map((product) => (
|
{products.map((product) => (
|
||||||
<Grid.Item key={product.handle} className="animate-fadeIn">
|
<Grid.Item key={product.handle} className="animate-fadeIn">
|
||||||
<Link className="relative inline-block h-full w-full" href={`/product/${product.handle}`}>
|
<Link
|
||||||
|
className="relative inline-block h-full w-full"
|
||||||
|
href={`/product/${product.handle}`}
|
||||||
|
prefetch={true}
|
||||||
|
>
|
||||||
<GridTileImage
|
<GridTileImage
|
||||||
alt={product.title}
|
alt={product.title}
|
||||||
label={{
|
label={{
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { SortFilterItem } from 'lib/constants';
|
import { SortFilterItem } from 'lib/constants';
|
||||||
|
import { Suspense } from 'react';
|
||||||
import FilterItemDropdown from './dropdown';
|
import FilterItemDropdown from './dropdown';
|
||||||
import { FilterItem } from './item';
|
import { FilterItem } from './item';
|
||||||
|
|
||||||
@@ -19,12 +20,20 @@ export default function FilterList({ list, title }: { list: ListItem[]; title?:
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<nav>
|
<nav>
|
||||||
{title ? <h3 className="hidden text-xs text-neutral-500 md:block">{title}</h3> : null}
|
{title ? (
|
||||||
|
<h3 className="hidden text-xs text-neutral-500 md:block dark:text-neutral-400">
|
||||||
|
{title}
|
||||||
|
</h3>
|
||||||
|
) : null}
|
||||||
<ul className="hidden md:block">
|
<ul className="hidden md:block">
|
||||||
|
<Suspense fallback={null}>
|
||||||
<FilterItemList list={list} />
|
<FilterItemList list={list} />
|
||||||
|
</Suspense>
|
||||||
</ul>
|
</ul>
|
||||||
<ul className="md:hidden">
|
<ul className="md:hidden">
|
||||||
|
<Suspense fallback={null}>
|
||||||
<FilterItemDropdown list={list} />
|
<FilterItemDropdown list={list} />
|
||||||
|
</Suspense>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import { SortFilterItem } from 'lib/constants';
|
import type { SortFilterItem } from 'lib/constants';
|
||||||
import { createUrl } from 'lib/utils';
|
import { createUrl } from 'lib/utils';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { usePathname, useSearchParams } from 'next/navigation';
|
import { usePathname, useSearchParams } from 'next/navigation';
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { ImageResponse } from 'next/server';
|
import { ImageResponse } from 'next/og';
|
||||||
import LogoIcon from './icons/logo';
|
import LogoIcon from './icons/logo';
|
||||||
|
|
||||||
export type Props = {
|
export type Props = {
|
||||||
|
|||||||
@@ -2,32 +2,22 @@
|
|||||||
|
|
||||||
import { ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/24/outline';
|
import { ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/24/outline';
|
||||||
import { GridTileImage } from 'components/grid/tile';
|
import { GridTileImage } from 'components/grid/tile';
|
||||||
import { createUrl } from 'lib/utils';
|
import { useProduct, useUpdateURL } from 'components/product/product-context';
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import Link from 'next/link';
|
|
||||||
import { usePathname, useSearchParams } from 'next/navigation';
|
|
||||||
|
|
||||||
export function Gallery({ images }: { images: { src: string; altText: string }[] }) {
|
export function Gallery({ images }: { images: { src: string; altText: string }[] }) {
|
||||||
const pathname = usePathname();
|
const { state, updateImage } = useProduct();
|
||||||
const searchParams = useSearchParams();
|
const updateURL = useUpdateURL();
|
||||||
const imageSearchParam = searchParams.get('image');
|
const imageIndex = state.image ? parseInt(state.image) : 0;
|
||||||
const imageIndex = imageSearchParam ? parseInt(imageSearchParam) : 0;
|
|
||||||
|
|
||||||
const nextSearchParams = new URLSearchParams(searchParams.toString());
|
|
||||||
const nextImageIndex = imageIndex + 1 < images.length ? imageIndex + 1 : 0;
|
const nextImageIndex = imageIndex + 1 < images.length ? imageIndex + 1 : 0;
|
||||||
nextSearchParams.set('image', nextImageIndex.toString());
|
|
||||||
const nextUrl = createUrl(pathname, nextSearchParams);
|
|
||||||
|
|
||||||
const previousSearchParams = new URLSearchParams(searchParams.toString());
|
|
||||||
const previousImageIndex = imageIndex === 0 ? images.length - 1 : imageIndex - 1;
|
const previousImageIndex = imageIndex === 0 ? images.length - 1 : imageIndex - 1;
|
||||||
previousSearchParams.set('image', previousImageIndex.toString());
|
|
||||||
const previousUrl = createUrl(pathname, previousSearchParams);
|
|
||||||
|
|
||||||
const buttonClassName =
|
const buttonClassName =
|
||||||
'h-full px-6 transition-all ease-in-out hover:scale-110 hover:text-black dark:hover:text-white flex items-center justify-center';
|
'h-full px-6 transition-all ease-in-out hover:scale-110 hover:text-black dark:hover:text-white flex items-center justify-center';
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<form>
|
||||||
<div className="relative aspect-square h-full max-h-[550px] w-full overflow-hidden">
|
<div className="relative aspect-square h-full max-h-[550px] w-full overflow-hidden">
|
||||||
{images[imageIndex] && (
|
{images[imageIndex] && (
|
||||||
<Image
|
<Image
|
||||||
@@ -43,23 +33,27 @@ export function Gallery({ images }: { images: { src: string; altText: string }[]
|
|||||||
{images.length > 1 ? (
|
{images.length > 1 ? (
|
||||||
<div className="absolute bottom-[15%] flex w-full justify-center">
|
<div className="absolute bottom-[15%] flex w-full justify-center">
|
||||||
<div className="mx-auto flex h-11 items-center rounded-full border border-white bg-neutral-50/80 text-neutral-500 backdrop-blur dark:border-black dark:bg-neutral-900/80">
|
<div className="mx-auto flex h-11 items-center rounded-full border border-white bg-neutral-50/80 text-neutral-500 backdrop-blur dark:border-black dark:bg-neutral-900/80">
|
||||||
<Link
|
<button
|
||||||
|
formAction={() => {
|
||||||
|
const newState = updateImage(previousImageIndex.toString());
|
||||||
|
updateURL(newState);
|
||||||
|
}}
|
||||||
aria-label="Previous product image"
|
aria-label="Previous product image"
|
||||||
href={previousUrl}
|
|
||||||
className={buttonClassName}
|
className={buttonClassName}
|
||||||
scroll={false}
|
|
||||||
>
|
>
|
||||||
<ArrowLeftIcon className="h-5" />
|
<ArrowLeftIcon className="h-5" />
|
||||||
</Link>
|
</button>
|
||||||
<div className="mx-1 h-6 w-px bg-neutral-500"></div>
|
<div className="mx-1 h-6 w-px bg-neutral-500"></div>
|
||||||
<Link
|
<button
|
||||||
|
formAction={() => {
|
||||||
|
const newState = updateImage(nextImageIndex.toString());
|
||||||
|
updateURL(newState);
|
||||||
|
}}
|
||||||
aria-label="Next product image"
|
aria-label="Next product image"
|
||||||
href={nextUrl}
|
|
||||||
className={buttonClassName}
|
className={buttonClassName}
|
||||||
scroll={false}
|
|
||||||
>
|
>
|
||||||
<ArrowRightIcon className="h-5" />
|
<ArrowRightIcon className="h-5" />
|
||||||
</Link>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
@@ -69,16 +63,15 @@ export function Gallery({ images }: { images: { src: string; altText: string }[]
|
|||||||
<ul className="my-12 flex items-center justify-center gap-2 overflow-auto py-1 lg:mb-0">
|
<ul className="my-12 flex items-center justify-center gap-2 overflow-auto py-1 lg:mb-0">
|
||||||
{images.map((image, index) => {
|
{images.map((image, index) => {
|
||||||
const isActive = index === imageIndex;
|
const isActive = index === imageIndex;
|
||||||
const imageSearchParams = new URLSearchParams(searchParams.toString());
|
|
||||||
|
|
||||||
imageSearchParams.set('image', index.toString());
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li key={image.src} className="h-20 w-20">
|
<li key={image.src} className="h-20 w-20">
|
||||||
<Link
|
<button
|
||||||
aria-label="Enlarge product image"
|
formAction={() => {
|
||||||
href={createUrl(pathname, imageSearchParams)}
|
const newState = updateImage(index.toString());
|
||||||
scroll={false}
|
updateURL(newState);
|
||||||
|
}}
|
||||||
|
aria-label="Select product image"
|
||||||
className="h-full w-full"
|
className="h-full w-full"
|
||||||
>
|
>
|
||||||
<GridTileImage
|
<GridTileImage
|
||||||
@@ -88,12 +81,12 @@ export function Gallery({ images }: { images: { src: string; altText: string }[]
|
|||||||
height={80}
|
height={80}
|
||||||
active={isActive}
|
active={isActive}
|
||||||
/>
|
/>
|
||||||
</Link>
|
</button>
|
||||||
</li>
|
</li>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</ul>
|
</ul>
|
||||||
) : null}
|
) : null}
|
||||||
</>
|
</form>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
81
components/product/product-context.tsx
Normal file
81
components/product/product-context.tsx
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
|
import React, { createContext, useContext, useMemo, useOptimistic } from 'react';
|
||||||
|
|
||||||
|
type ProductState = {
|
||||||
|
[key: string]: string;
|
||||||
|
} & {
|
||||||
|
image?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type ProductContextType = {
|
||||||
|
state: ProductState;
|
||||||
|
updateOption: (name: string, value: string) => ProductState;
|
||||||
|
updateImage: (index: string) => ProductState;
|
||||||
|
};
|
||||||
|
|
||||||
|
const ProductContext = createContext<ProductContextType | undefined>(undefined);
|
||||||
|
|
||||||
|
export function ProductProvider({ children }: { children: React.ReactNode }) {
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
|
const getInitialState = () => {
|
||||||
|
const params: ProductState = {};
|
||||||
|
for (const [key, value] of searchParams.entries()) {
|
||||||
|
params[key] = value;
|
||||||
|
}
|
||||||
|
return params;
|
||||||
|
};
|
||||||
|
|
||||||
|
const [state, setOptimisticState] = useOptimistic(
|
||||||
|
getInitialState(),
|
||||||
|
(prevState: ProductState, update: ProductState) => ({
|
||||||
|
...prevState,
|
||||||
|
...update
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const updateOption = (name: string, value: string) => {
|
||||||
|
const newState = { [name]: value };
|
||||||
|
setOptimisticState(newState);
|
||||||
|
return { ...state, ...newState };
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateImage = (index: string) => {
|
||||||
|
const newState = { image: index };
|
||||||
|
setOptimisticState(newState);
|
||||||
|
return { ...state, ...newState };
|
||||||
|
};
|
||||||
|
|
||||||
|
const value = useMemo(
|
||||||
|
() => ({
|
||||||
|
state,
|
||||||
|
updateOption,
|
||||||
|
updateImage
|
||||||
|
}),
|
||||||
|
[state]
|
||||||
|
);
|
||||||
|
|
||||||
|
return <ProductContext.Provider value={value}>{children}</ProductContext.Provider>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useProduct() {
|
||||||
|
const context = useContext(ProductContext);
|
||||||
|
if (context === undefined) {
|
||||||
|
throw new Error('useProduct must be used within a ProductProvider');
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useUpdateURL() {
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
return (state: ProductState) => {
|
||||||
|
const newParams = new URLSearchParams(window.location.search);
|
||||||
|
Object.entries(state).forEach(([key, value]) => {
|
||||||
|
newParams.set(key, value);
|
||||||
|
});
|
||||||
|
router.push(`?${newParams.toString()}`, { scroll: false });
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -17,15 +17,13 @@ export function ProductDescription({ product }: { product: Product }) {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<VariantSelector options={product.options} variants={product.variants} />
|
<VariantSelector options={product.options} variants={product.variants} />
|
||||||
|
|
||||||
{product.descriptionHtml ? (
|
{product.descriptionHtml ? (
|
||||||
<Prose
|
<Prose
|
||||||
className="mb-6 text-sm leading-tight dark:text-white/[60%]"
|
className="mb-6 text-sm leading-tight dark:text-white/[60%]"
|
||||||
html={product.descriptionHtml}
|
html={product.descriptionHtml}
|
||||||
/>
|
/>
|
||||||
) : null}
|
) : null}
|
||||||
|
<AddToCart product={product} />
|
||||||
<AddToCart variants={product.variants} availableForSale={product.availableForSale} />
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
|
import { useProduct, useUpdateURL } from 'components/product/product-context';
|
||||||
import { ProductOption, ProductVariant } from 'lib/shopify/types';
|
import { ProductOption, ProductVariant } from 'lib/shopify/types';
|
||||||
import { createUrl } from 'lib/utils';
|
|
||||||
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
|
|
||||||
|
|
||||||
type Combination = {
|
type Combination = {
|
||||||
id: string;
|
id: string;
|
||||||
availableForSale: boolean;
|
availableForSale: boolean;
|
||||||
[key: string]: string | boolean; // ie. { color: 'Red', size: 'Large', ... }
|
[key: string]: string | boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function VariantSelector({
|
export function VariantSelector({
|
||||||
@@ -18,9 +17,8 @@ export function VariantSelector({
|
|||||||
options: ProductOption[];
|
options: ProductOption[];
|
||||||
variants: ProductVariant[];
|
variants: ProductVariant[];
|
||||||
}) {
|
}) {
|
||||||
const router = useRouter();
|
const { state, updateOption } = useProduct();
|
||||||
const pathname = usePathname();
|
const updateURL = useUpdateURL();
|
||||||
const searchParams = useSearchParams();
|
|
||||||
const hasNoOptionsOrJustOneOption =
|
const hasNoOptionsOrJustOneOption =
|
||||||
!options.length || (options.length === 1 && options[0]?.values.length === 1);
|
!options.length || (options.length === 1 && options[0]?.values.length === 1);
|
||||||
|
|
||||||
@@ -31,7 +29,6 @@ export function VariantSelector({
|
|||||||
const combinations: Combination[] = variants.map((variant) => ({
|
const combinations: Combination[] = variants.map((variant) => ({
|
||||||
id: variant.id,
|
id: variant.id,
|
||||||
availableForSale: variant.availableForSale,
|
availableForSale: variant.availableForSale,
|
||||||
// Adds key / value pairs for each variant (ie. "color": "Black" and "size": 'M").
|
|
||||||
...variant.selectedOptions.reduce(
|
...variant.selectedOptions.reduce(
|
||||||
(accumulator, option) => ({ ...accumulator, [option.name.toLowerCase()]: option.value }),
|
(accumulator, option) => ({ ...accumulator, [option.name.toLowerCase()]: option.value }),
|
||||||
{}
|
{}
|
||||||
@@ -39,30 +36,18 @@ export function VariantSelector({
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
return options.map((option) => (
|
return options.map((option) => (
|
||||||
<dl className="mb-8" key={option.id}>
|
<form key={option.id}>
|
||||||
|
<dl className="mb-8">
|
||||||
<dt className="mb-4 text-sm uppercase tracking-wide">{option.name}</dt>
|
<dt className="mb-4 text-sm uppercase tracking-wide">{option.name}</dt>
|
||||||
<dd className="flex flex-wrap gap-3">
|
<dd className="flex flex-wrap gap-3">
|
||||||
{option.values.map((value) => {
|
{option.values.map((value) => {
|
||||||
const optionNameLowerCase = option.name.toLowerCase();
|
const optionNameLowerCase = option.name.toLowerCase();
|
||||||
|
|
||||||
// Base option params on current params so we can preserve any other param state in the url.
|
// Base option params on current selectedOptions so we can preserve any other param state.
|
||||||
const optionSearchParams = new URLSearchParams(searchParams.toString());
|
const optionParams = { ...state, [optionNameLowerCase]: value };
|
||||||
|
|
||||||
// Update the option params using the current option to reflect how the url *would* change,
|
// Filter out invalid options and check if the option combination is available for sale.
|
||||||
// if the option was clicked.
|
const filtered = Object.entries(optionParams).filter(([key, value]) =>
|
||||||
optionSearchParams.set(optionNameLowerCase, value);
|
|
||||||
const optionUrl = createUrl(pathname, optionSearchParams);
|
|
||||||
|
|
||||||
// In order to determine if an option is available for sale, we need to:
|
|
||||||
//
|
|
||||||
// 1. Filter out all other param state
|
|
||||||
// 2. Filter out invalid options
|
|
||||||
// 3. Check if the option combination is available for sale
|
|
||||||
//
|
|
||||||
// This is the "magic" that will cross check possible variant combinations and preemptively
|
|
||||||
// disable combinations that are not available. For example, if the color gray is only available in size medium,
|
|
||||||
// then all other sizes should be disabled.
|
|
||||||
const filtered = Array.from(optionSearchParams.entries()).filter(([key, value]) =>
|
|
||||||
options.find(
|
options.find(
|
||||||
(option) => option.name.toLowerCase() === key && option.values.includes(value)
|
(option) => option.name.toLowerCase() === key && option.values.includes(value)
|
||||||
)
|
)
|
||||||
@@ -73,23 +58,24 @@ export function VariantSelector({
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
// The option is active if it's in the url params.
|
// The option is active if it's in the selected options.
|
||||||
const isActive = searchParams.get(optionNameLowerCase) === value;
|
const isActive = state[optionNameLowerCase] === value;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
|
formAction={() => {
|
||||||
|
const newState = updateOption(optionNameLowerCase, value);
|
||||||
|
updateURL(newState);
|
||||||
|
}}
|
||||||
key={value}
|
key={value}
|
||||||
aria-disabled={!isAvailableForSale}
|
aria-disabled={!isAvailableForSale}
|
||||||
disabled={!isAvailableForSale}
|
disabled={!isAvailableForSale}
|
||||||
onClick={() => {
|
|
||||||
router.replace(optionUrl, { scroll: false });
|
|
||||||
}}
|
|
||||||
title={`${option.name} ${value}${!isAvailableForSale ? ' (Out of Stock)' : ''}`}
|
title={`${option.name} ${value}${!isAvailableForSale ? ' (Out of Stock)' : ''}`}
|
||||||
className={clsx(
|
className={clsx(
|
||||||
'flex min-w-[48px] items-center justify-center rounded-full border bg-neutral-100 px-2 py-1 text-sm dark:border-neutral-800 dark:bg-neutral-900',
|
'flex min-w-[48px] items-center justify-center rounded-full border bg-neutral-100 px-2 py-1 text-sm dark:border-neutral-800 dark:bg-neutral-900',
|
||||||
{
|
{
|
||||||
'cursor-default ring-2 ring-blue-600': isActive,
|
'cursor-default ring-2 ring-blue-600': isActive,
|
||||||
'ring-1 ring-transparent transition duration-300 ease-in-out hover:scale-110 hover:ring-blue-600 ':
|
'ring-1 ring-transparent transition duration-300 ease-in-out hover:ring-blue-600':
|
||||||
!isActive && isAvailableForSale,
|
!isActive && isAvailableForSale,
|
||||||
'relative z-10 cursor-not-allowed overflow-hidden bg-neutral-100 text-neutral-500 ring-1 ring-neutral-300 before:absolute before:inset-x-0 before:-z-10 before:h-px before:-rotate-45 before:bg-neutral-300 before:transition-transform dark:bg-neutral-900 dark:text-neutral-400 dark:ring-neutral-700 before:dark:bg-neutral-700':
|
'relative z-10 cursor-not-allowed overflow-hidden bg-neutral-100 text-neutral-500 ring-1 ring-neutral-300 before:absolute before:inset-x-0 before:-z-10 before:h-px before:-rotate-45 before:bg-neutral-300 before:transition-transform dark:bg-neutral-900 dark:text-neutral-400 dark:ring-neutral-700 before:dark:bg-neutral-700':
|
||||||
!isAvailableForSale
|
!isAvailableForSale
|
||||||
@@ -102,5 +88,6 @@ export function VariantSelector({
|
|||||||
})}
|
})}
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
|
</form>
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|||||||
35
components/welcome-toast.tsx
Normal file
35
components/welcome-toast.tsx
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
import { toast } from 'sonner';
|
||||||
|
|
||||||
|
export function WelcomeToast() {
|
||||||
|
useEffect(() => {
|
||||||
|
// ignore if screen height is too small
|
||||||
|
if (window.innerHeight < 650) return;
|
||||||
|
if (!document.cookie.includes('welcome-toast=2')) {
|
||||||
|
toast('🛍️ Welcome to Next.js Commerce!', {
|
||||||
|
id: 'welcome-toast',
|
||||||
|
duration: Infinity,
|
||||||
|
onDismiss: () => {
|
||||||
|
document.cookie = 'welcome-toast=2; max-age=31536000; path=/';
|
||||||
|
},
|
||||||
|
description: (
|
||||||
|
<>
|
||||||
|
This is a high-performance, SSR storefront powered by Shopify, Next.js, and Vercel.{' '}
|
||||||
|
<a
|
||||||
|
href="https://vercel.com/templates/next.js/nextjs-commerce"
|
||||||
|
className="text-blue-600 hover:underline"
|
||||||
|
target="_blank"
|
||||||
|
>
|
||||||
|
Deploy your own
|
||||||
|
</a>
|
||||||
|
.
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
@@ -22,7 +22,8 @@ export const sorting: SortFilterItem[] = [
|
|||||||
|
|
||||||
export const TAGS = {
|
export const TAGS = {
|
||||||
collections: 'collections',
|
collections: 'collections',
|
||||||
products: 'products'
|
products: 'products',
|
||||||
|
cart: 'cart'
|
||||||
};
|
};
|
||||||
|
|
||||||
export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden';
|
export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden';
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ export async function shopifyFetch<T>({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const removeEdgesAndNodes = (array: Connection<any>) => {
|
const removeEdgesAndNodes = <T>(array: Connection<T>): T[] => {
|
||||||
return array.edges.map((edge) => edge?.node);
|
return array.edges.map((edge) => edge?.node);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -163,7 +163,7 @@ const reshapeImages = (images: Connection<Image>, productTitle: string) => {
|
|||||||
const flattened = removeEdgesAndNodes(images);
|
const flattened = removeEdgesAndNodes(images);
|
||||||
|
|
||||||
return flattened.map((image) => {
|
return flattened.map((image) => {
|
||||||
const filename = image.url.match(/.*\/(.*)\..*/)[1];
|
const filename = image.url.match(/.*\/(.*)\..*/)?.[1];
|
||||||
return {
|
return {
|
||||||
...image,
|
...image,
|
||||||
altText: image.altText || `${productTitle} - ${filename}`
|
altText: image.altText || `${productTitle} - ${filename}`
|
||||||
@@ -254,11 +254,15 @@ export async function updateCart(
|
|||||||
return reshapeCart(res.body.data.cartLinesUpdate.cart);
|
return reshapeCart(res.body.data.cartLinesUpdate.cart);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCart(cartId: string): Promise<Cart | undefined> {
|
export async function getCart(cartId: string | undefined): Promise<Cart | undefined> {
|
||||||
|
if (!cartId) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
const res = await shopifyFetch<ShopifyCartOperation>({
|
const res = await shopifyFetch<ShopifyCartOperation>({
|
||||||
query: getCartQuery,
|
query: getCartQuery,
|
||||||
variables: { cartId },
|
variables: { cartId },
|
||||||
cache: 'no-store'
|
tags: [TAGS.cart]
|
||||||
});
|
});
|
||||||
|
|
||||||
// Old carts becomes `null` when you checkout.
|
// Old carts becomes `null` when you checkout.
|
||||||
@@ -356,6 +360,7 @@ export async function getMenu(handle: string): Promise<Menu[]> {
|
|||||||
export async function getPage(handle: string): Promise<Page> {
|
export async function getPage(handle: string): Promise<Page> {
|
||||||
const res = await shopifyFetch<ShopifyPageOperation>({
|
const res = await shopifyFetch<ShopifyPageOperation>({
|
||||||
query: getPageQuery,
|
query: getPageQuery,
|
||||||
|
cache: 'no-store',
|
||||||
variables: { handle }
|
variables: { handle }
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -364,7 +369,8 @@ export async function getPage(handle: string): Promise<Page> {
|
|||||||
|
|
||||||
export async function getPages(): Promise<Page[]> {
|
export async function getPages(): Promise<Page[]> {
|
||||||
const res = await shopifyFetch<ShopifyPagesOperation>({
|
const res = await shopifyFetch<ShopifyPagesOperation>({
|
||||||
query: getPagesQuery
|
query: getPagesQuery,
|
||||||
|
cache: 'no-store'
|
||||||
});
|
});
|
||||||
|
|
||||||
return removeEdgesAndNodes(res.body.data.pages);
|
return removeEdgesAndNodes(res.body.data.pages);
|
||||||
|
|||||||
@@ -12,8 +12,15 @@ export type Cart = Omit<ShopifyCart, 'lines'> & {
|
|||||||
lines: CartItem[];
|
lines: CartItem[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CartItem = {
|
export type CartProduct = {
|
||||||
id: string;
|
id: string;
|
||||||
|
handle: string;
|
||||||
|
title: string;
|
||||||
|
featuredImage: Image;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CartItem = {
|
||||||
|
id: string | undefined;
|
||||||
quantity: number;
|
quantity: number;
|
||||||
cost: {
|
cost: {
|
||||||
totalAmount: Money;
|
totalAmount: Money;
|
||||||
@@ -25,7 +32,7 @@ export type CartItem = {
|
|||||||
name: string;
|
name: string;
|
||||||
value: string;
|
value: string;
|
||||||
}[];
|
}[];
|
||||||
product: Product;
|
product: CartProduct;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -89,7 +96,7 @@ export type SEO = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type ShopifyCart = {
|
export type ShopifyCart = {
|
||||||
id: string;
|
id: string | undefined;
|
||||||
checkoutUrl: string;
|
checkoutUrl: string;
|
||||||
cost: {
|
cost: {
|
||||||
subtotalAmount: Money;
|
subtotalAmount: Money;
|
||||||
|
|||||||
28
lib/utils.ts
28
lib/utils.ts
@@ -9,3 +9,31 @@ export const createUrl = (pathname: string, params: URLSearchParams | ReadonlyUR
|
|||||||
|
|
||||||
export const ensureStartsWith = (stringToCheck: string, startsWith: string) =>
|
export const ensureStartsWith = (stringToCheck: string, startsWith: string) =>
|
||||||
stringToCheck.startsWith(startsWith) ? stringToCheck : `${startsWith}${stringToCheck}`;
|
stringToCheck.startsWith(startsWith) ? stringToCheck : `${startsWith}${stringToCheck}`;
|
||||||
|
|
||||||
|
export const validateEnvironmentVariables = () => {
|
||||||
|
const requiredEnvironmentVariables = ['SHOPIFY_STORE_DOMAIN', 'SHOPIFY_STOREFRONT_ACCESS_TOKEN'];
|
||||||
|
const missingEnvironmentVariables = [] as string[];
|
||||||
|
|
||||||
|
requiredEnvironmentVariables.forEach((envVar) => {
|
||||||
|
if (!process.env[envVar]) {
|
||||||
|
missingEnvironmentVariables.push(envVar);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (missingEnvironmentVariables.length) {
|
||||||
|
throw new Error(
|
||||||
|
`The following environment variables are missing. Your site will not work without them. Read more: https://vercel.com/docs/integrations/shopify#configure-environment-variables\n\n${missingEnvironmentVariables.join(
|
||||||
|
'\n'
|
||||||
|
)}\n`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
process.env.SHOPIFY_STORE_DOMAIN?.includes('[') ||
|
||||||
|
process.env.SHOPIFY_STORE_DOMAIN?.includes(']')
|
||||||
|
) {
|
||||||
|
throw new Error(
|
||||||
|
'Your `SHOPIFY_STORE_DOMAIN` environment variable includes brackets (ie. `[` and / or `]`). Your site will not work with them there. Please remove them.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,12 +1,5 @@
|
|||||||
/** @type {import('next').NextConfig} */
|
/** @type {import('next').NextConfig} */
|
||||||
module.exports = {
|
module.exports = {
|
||||||
eslint: {
|
|
||||||
// Disabling on production builds because we're running checks on PRs via GitHub Actions.
|
|
||||||
ignoreDuringBuilds: true
|
|
||||||
},
|
|
||||||
experimental: {
|
|
||||||
serverActions: true
|
|
||||||
},
|
|
||||||
images: {
|
images: {
|
||||||
formats: ['image/avif', 'image/webp'],
|
formats: ['image/avif', 'image/webp'],
|
||||||
remotePatterns: [
|
remotePatterns: [
|
||||||
@@ -16,14 +9,5 @@ module.exports = {
|
|||||||
pathname: '/s/files/**'
|
pathname: '/s/files/**'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
|
||||||
async redirects() {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
source: '/password',
|
|
||||||
destination: '/',
|
|
||||||
permanent: true
|
|
||||||
}
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
57
package.json
57
package.json
@@ -1,51 +1,38 @@
|
|||||||
{
|
{
|
||||||
"private": true,
|
"private": true,
|
||||||
"packageManager": "pnpm@8.2.0",
|
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=18",
|
"node": ">=20",
|
||||||
"pnpm": ">=7"
|
"pnpm": ">=9"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "next dev",
|
"dev": "next dev --turbo",
|
||||||
"build": "next build",
|
"build": "next build",
|
||||||
"start": "next start",
|
"start": "next start",
|
||||||
"lint": "next lint",
|
|
||||||
"lint-staged": "lint-staged",
|
|
||||||
"prettier": "prettier --write --ignore-unknown .",
|
"prettier": "prettier --write --ignore-unknown .",
|
||||||
"prettier:check": "prettier --check --ignore-unknown .",
|
"prettier:check": "prettier --check --ignore-unknown .",
|
||||||
"test": "pnpm lint && pnpm prettier:check"
|
"test": "pnpm prettier:check"
|
||||||
},
|
|
||||||
"git": {
|
|
||||||
"pre-commit": "lint-staged"
|
|
||||||
},
|
|
||||||
"lint-staged": {
|
|
||||||
"*": "prettier --write --ignore-unknown"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@headlessui/react": "^1.7.15",
|
"@headlessui/react": "^2.1.2",
|
||||||
"@heroicons/react": "^2.0.18",
|
"@heroicons/react": "^2.1.5",
|
||||||
"clsx": "^2.0.0",
|
"clsx": "^2.1.1",
|
||||||
"next": "13.4.13-canary.15",
|
"geist": "^1.3.1",
|
||||||
"react": "18.2.0",
|
"next": "https://vercel-packages.vercel.app/next/prs/68340/next",
|
||||||
"react-dom": "18.2.0"
|
"react": "19.0.0-rc-3208e73e-20240730",
|
||||||
|
"react-dom": "19.0.0-rc-3208e73e-20240730",
|
||||||
|
"sonner": "^1.5.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@tailwindcss/container-queries": "^0.1.1",
|
"@tailwindcss/container-queries": "^0.1.1",
|
||||||
"@tailwindcss/typography": "^0.5.9",
|
"@tailwindcss/typography": "^0.5.13",
|
||||||
"@types/node": "20.4.4",
|
"@types/node": "20.14.12",
|
||||||
"@types/react": "18.2.16",
|
"@types/react": "18.3.3",
|
||||||
"@types/react-dom": "18.2.7",
|
"@types/react-dom": "18.3.0",
|
||||||
"@vercel/git-hooks": "^1.0.0",
|
"autoprefixer": "^10.4.19",
|
||||||
"autoprefixer": "^10.4.14",
|
"postcss": "^8.4.39",
|
||||||
"eslint": "^8.45.0",
|
"prettier": "3.3.3",
|
||||||
"eslint-config-next": "^13.4.12",
|
"prettier-plugin-tailwindcss": "^0.6.5",
|
||||||
"eslint-config-prettier": "^8.8.0",
|
"tailwindcss": "^3.4.6",
|
||||||
"eslint-plugin-unicorn": "^48.0.0",
|
"typescript": "5.5.4"
|
||||||
"lint-staged": "^13.2.3",
|
|
||||||
"postcss": "^8.4.27",
|
|
||||||
"prettier": "3.0.1",
|
|
||||||
"prettier-plugin-tailwindcss": "^0.4.1",
|
|
||||||
"tailwindcss": "^3.3.3",
|
|
||||||
"typescript": "5.1.6"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
4292
pnpm-lock.yaml
generated
4292
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,9 @@
|
|||||||
|
/** @type {import('prettier').Config} */
|
||||||
module.exports = {
|
module.exports = {
|
||||||
singleQuote: true,
|
singleQuote: true,
|
||||||
arrowParens: 'always',
|
arrowParens: 'always',
|
||||||
trailingComma: 'none',
|
trailingComma: 'none',
|
||||||
printWidth: 100,
|
printWidth: 100,
|
||||||
tabWidth: 2,
|
tabWidth: 2,
|
||||||
// pnpm doesn't support plugin autoloading
|
plugins: ['prettier-plugin-tailwindcss']
|
||||||
// https://github.com/tailwindlabs/prettier-plugin-tailwindcss#installation
|
|
||||||
plugins: [require('prettier-plugin-tailwindcss')]
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ module.exports = {
|
|||||||
theme: {
|
theme: {
|
||||||
extend: {
|
extend: {
|
||||||
fontFamily: {
|
fontFamily: {
|
||||||
sans: ['var(--font-inter)']
|
sans: ['var(--font-geist-sans)']
|
||||||
},
|
},
|
||||||
keyframes: {
|
keyframes: {
|
||||||
fadeIn: {
|
fadeIn: {
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "es5",
|
"target": "es2015",
|
||||||
"lib": ["dom", "dom.iterable", "esnext"],
|
"lib": ["dom", "dom.iterable", "esnext"],
|
||||||
|
"downlevelIteration": true,
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
|
|||||||
Reference in New Issue
Block a user