diff --git a/.env.example b/.env.example index 088014635..d97ac1819 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,4 @@ -TWITTER_CREATOR="@vercel" -TWITTER_SITE="https://nextjs.org/commerce" -SITE_NAME="Next.js Commerce" -SHOPIFY_STOREFRONT_ACCESS_TOKEN= -SHOPIFY_STORE_DOMAIN= +TWITTER_CREATOR="@medusajs" +TWITTER_SITE="https://medusajs.com/" +SITE_NAME="Next.js Commerce x Medusa" +MEDUSA_BACKEND_API="http://localhost:9000/store" diff --git a/README.md b/README.md index e7ec645ad..a6abf2a69 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,12 @@ [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fcommerce&project-name=commerce&repo-name=commerce&demo-title=Next.js%20Commerce&demo-url=https%3A%2F%2Fdemo.vercel.store&demo-image=https%3A%2F%2Fbigcommerce-demo-asset-ksvtgfvnd.vercel.app%2Fbigcommerce.png&env=SHOPIFY_STOREFRONT_ACCESS_TOKEN,SHOPIFY_STORE_DOMAIN,SITE_NAME,TWITTER_CREATOR,TWITTER_SITE) -# Next.js Commerce +_Todo: update deploy url_ + +# Next.js Commerce x Medusa > Note: Looking for Next.js Commerce v1? View the [code](https://github.com/vercel/commerce/tree/v1), [demo](https://commerce-v1.vercel.store), and [release notes](https://github.com/vercel/commerce/releases/tag/v1) -A Next.js 13 and App Router-ready ecommerce template, built with Shopify, featuring: +A Next.js 13 and App Router-ready ecommerce template, built with [Medusa](https://github.com/medusajs/medusa), featuring: - Next.js App Router - Optimized for SEO using Next.js's Metadata @@ -14,14 +16,19 @@ A Next.js 13 and App Router-ready ecommerce template, built with Shopify, featur - 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 +## What is Medusa? + +Medusa is a set of commerce modules and tools that allow you to build rich, reliable, and performant commerce applications without reinventing core commerce logic. The modules can be customized and used to build advanced ecommerce stores, marketplaces, or any product that needs foundational commerce primitives. All modules are open-source and freely available on npm. + +Learn more about [Medusa’s architecture](https://docs.medusajs.com/development/fundamentals/architecture-overview) and [commerce modules](https://docs.medusajs.com/modules/overview) in the Docs. + ## Running locally You will need to use the environment variables [defined in `.env.example`](.env.example) to run Next.js Commerce. It's recommended you use [Vercel Environment Variables](https://vercel.com/docs/concepts/projects/environment-variables) for this, but a `.env` file is all that is necessary. -> Note: You should not commit your `.env` file or it will expose secrets that will allow others to control your Shopify store. +> Note: You should not commit your `.env` file or it will expose secrets. 1. Install Vercel CLI: `npm i -g vercel` 2. Link local instance with Vercel and GitHub accounts (creates `.vercel` directory): `vercel link` @@ -34,179 +41,37 @@ pnpm dev Your app should now be running on [localhost:3000](http://localhost:3000/). -## How to configure your Shopify store for Next.js Commerce +## How to configure your Medusa server for Next.js Commerce -Next.js Commerce requires a [paid Shopify plan](https://www.shopify.com/pricing). It will not work with a Shopify Starter plan. +Next.js Commerce x Medusa requires a running [Medusa](https://github.com/medusajs/medusa) server. -### Add Shopify domain to an environment variable +### Create a Medusa Backend[​](https://docs.medusajs.com/development/backend/install#create-a-medusa-backend 'Direct link to Create a Medusa Backend') -Create a `SHOPIFY_STORE_DOMAIN` environment variable and use your Shopify domain as the the value (ie. `SHOPIFY_STORE_SUBDOMAIN.myshopify.com`). +It is recommended to use [Yarn](https://yarnpkg.com/getting-started/install) for the installation process as it's much faster than using NPM. -> Note: Do not include the `https://`. +#### 1. Install Medusa CLI[​](https://docs.medusajs.com/development/backend/install#1-install-medusa-cli 'Direct link to 1. Install Medusa CLI') -### Accessing the Shopify Storefront API +- npm +- Yarn -Next.js Commerce utilizes [Shopify's Storefront API](https://shopify.dev/docs/api/storefront) to create unique customer experiences. The API offers a full range of commerce options making it possible for customers to control products, collections, menus, pages, cart, checkout, and more. +``` +yarn global add @medusajs/medusa-cli +``` -In order to use the Shopify's Storefront API, you need to install the [Headless app](https://apps.shopify.com/headless) in your Shopify store. +If you run into any errors while installing the CLI tool, check out the [troubleshooting guide](https://docs.medusajs.com/troubleshooting/cli-installation-errors). -Once installed, you'll need to create a `SHOPIFY_STOREFRONT_ACCESS_TOKEN` environment variable and use the public access token as the value +#### 2. Create a new Medusa project[​](https://docs.medusajs.com/development/backend/install#2-create-a-new-medusa-project 'Direct link to 2. Create a new Medusa project') -> Note: Shopify does offer a Node.js Storefront API SDK. We use the Storefront API via GraphQL directly instead of the Node.js SDK so we have more control over fetching and caching. +``` +medusa new my-medusa-store --seed +``` -
- Expand to view detailed walkthrough +#### 3. Start your Medusa backend[​](https://docs.medusajs.com/development/backend/install#3-start-your-medusa-backend 'Direct link to 3. Start your Medusa backend') -1. Navigate to `https://SHOPIFY_STORE_SUBDOMAIN.myshopify.com/admin/settings/apps`. -1. Click the green `Shopify App Store` button. - ![Shopify App Store](https://user-images.githubusercontent.com/446260/233220545-cb4c1461-ebc5-424e-a421-bf0d32044027.jpg) -1. Search for `Headless` and click on the `Headless` app. - ![Headless](https://user-images.githubusercontent.com/446260/233220547-6d93b5ef-16c7-45db-99e7-13ae7e18eb39.jpg) -1. Click the black `Add app` button. - ![Add app](https://user-images.githubusercontent.com/446260/233220550-a34c8bda-75a8-437a-9673-125f3794ff35.jpg) -1. Click the green `Add sales channel` button. - ![Add sales channel](https://user-images.githubusercontent.com/446260/233220553-42d94a74-421d-4f8a-99ab-a95936b707a3.jpg) -1. Click the green `Create storefront` button. - ![Create storefront](https://user-images.githubusercontent.com/446260/233220556-1eee15c4-a45d-446e-9f73-2e7c9f56b29c.jpg) -1. Copy and paste the public access token and assign it to a `SHOPIFY_STOREFRONT_ACCESS_TOKEN` environment variable. - ![Pubic access token](https://user-images.githubusercontent.com/446260/233220558-5db04ff9-b894-40fe-bfba-0e92f26b8e1f.jpg) -1. If you ever need to reference the public access token again, you can navigate to `https://SHOPIFY_STORE_SUBDOMAIN.myshopify.com/admin/headless_storefronts`. -
+``` +cd my-medusa-store medusa develop +``` -### Install a headless theme +#### 4. Add Medusa backend domain to an environment variable -When using a headless Shopify setup, you normally don't want customers to access any of the theme pages except for checkout. However, you can't totally disable the theme and a lot of links will still point to the theme (e.g. links in emails, order details, plugins, checkout, etc.). - -To enable a seamless flow between your headless site and Shopify, you can install the [Shopify Headless Theme](https://github.com/instantcommerce/shopify-headless-theme). - -Follow the installation instructions and configure the theme with your headless site's values. - -
- Expand to view detailed walkthrough - -1. Download [Shopify Headless Theme](https://github.com/instantcommerce/shopify-headless-theme). - ![Download Shoify Headless Theme](https://user-images.githubusercontent.com/446260/233220560-9f3f5ab0-ffb4-4305-b4ee-2c9d33eea90f.jpg) -1. Navigate to `https://SHOPIFY_STORE_SUBDOMAIN.myshopify.com/admin/themes`. -1. Click `Add theme`, then `Upload zip file`. - ![Upload zip file](https://user-images.githubusercontent.com/446260/233220561-7a53809e-0d95-45eb-b52f-3a52e3663a9c.jpg) -1. Select the downloaded zip file from above, and click the green `Upload file` button. - ![Select and upload file](https://user-images.githubusercontent.com/446260/233220563-135fb9f7-2921-4189-8f17-3b1cc15c0ea6.jpg) -1. Click `Customize`. - ![Customize theme](https://user-images.githubusercontent.com/446260/233220565-24b9c954-c18a-46f1-9db5-3d2a00040e48.jpg) -1. Click `Theme settings` (ie. the paintbrush icon), expand the `STOREFRONT` section, enter your headless store domain, click the gray `Publish` button. - ![Set headless domain in theme settings](https://user-images.githubusercontent.com/446260/233220566-acaee14d-03f8-400d-a2a2-28e85eb5ecdc.jpg) -1. Confirm the theme change by clicking the green `Save and publish` button. - ![Confirm save and publish](https://user-images.githubusercontent.com/446260/233220567-504d5bde-cfb9-426d-a264-f9a12d02af13.jpg) -1. The headless theme should now be your current active theme. -![Headless theme is current and active](https://user-images.githubusercontent.com/446260/233220569-63cab2b4-241b-4bf1-9b5b-451daaeceb91.jpg) -
- -### Branding & Design - -Since you're creating a headless Shopify store, you'll be in full control of your brand and design. However, there are still a few aspects we're leaving within Shopify's control. - -- Checkout -- Emails -- Order status -- Order history -- Favicon (for any Shopify controlled pages) - -You can use Shopify's admin to customize these pages to match your brand and design. - -
- Expand to view detailed walkthrough - -#### Checkout, order status, and order history - -1. Navigate to `https://SHOPIFY_STORE_SUBDOMAIN.myshopify.com/admin/settings/checkout`. -1. Click the green `Customize` button. - ![Customize](https://user-images.githubusercontent.com/446260/233220530-9beda4b4-5008-440a-b923-9d196b722539.jpg) -1. Click `Branding` (ie. the paintbrush icon) and customize your brand. Please note, there are three steps / pages to the checkout flow. Use the dropdown to change pages and adjust branding as needed on each page. Click `Save` when you are done. - ![Branding](https://user-images.githubusercontent.com/446260/233220534-e884d9fd-1a39-4f4d-9d09-163dde47c2e8.jpg) -1. Navigate to `https://SHOPIFY_STORE_SUBDOMAIN.myshopify.com/admin/settings/branding`. -1. Customize settings to match your brand. - ![Branding](https://user-images.githubusercontent.com/446260/233220536-452b8802-9a1e-40f0-9a12-52b3dace84a5.jpg) - -#### Emails - -1. Navigate to `https://SHOPIFY_STORE_SUBDOMAIN.myshopify.com/admin/settings/email_settings`. -1. Customize settings to match your brand. - ![Branding](https://user-images.githubusercontent.com/446260/233220538-13c83a9e-55f8-41e6-9b34-a39ee0848a8a.jpg) - -#### Favicon - -1. Navigate to `https://SHOPIFY_STORE_SUBDOMAIN.myshopify.com/admin/themes`. -1. Click the green `Customize` button. - ![Customize theme](https://user-images.githubusercontent.com/446260/233220539-4869a6cd-f59f-4de6-8091-95ed81d2302d.jpg) -1. Click `Theme settings` (ie. the paintbrush icon), expand the `FAVICON` section, upload favicon, then click the `Save` button. - ![Favicon](https://user-images.githubusercontent.com/446260/233220542-ac81b674-d86e-4172-ab38-c79d1ad1ff36.jpg) - -
- -### Configure webhooks for on-demand incremental static regeneration (ISR) - -Coming soon. - -### Using Shopify as a CMS - -Next.js Commerce is fully powered by Shopify in a truly headless and data driven way. - -#### Products - -`https://SHOPIFY_STORE_SUBDOMAIN.myshopify.com/admin/products` - -Only `Active` products are shown. `Draft` products will not be shown until they are marked as `Active`. - -`Active` products can still be hidden and not seen by navigating the site, by adding a `nextjs-frontend-hidden` tag on the product. This tag will also tell search engines to not index or crawl the product. The product is still directly accessible via url. This feature is great for "secret" products you only want to people you share the url with. - -Product options and option combinations are driven from Shopify options and variants. When selecting options on the product detail page, other option and variant combinations will be visually validated and verified for availability, like Amazon does. - -Products that are active and "out of stock" are still shown on the site, but the ability to add the product to the cart is disabled. - -#### Collections - -`https://SHOPIFY_STORE_SUBDOMAIN.myshopify.com/admin/collections` - -Create whatever collections you want and configure them however you want. All available collections will show on the search page as filters on the left, with one exception... - -Any collection names that start with the word "hidden" will not show up on the headless front end. The Next.js Commerce theme comes pre-configured to look for two hidden collections. Collections were chosen for this over tags so that order of products could be controlled (collections allow for manual ordering). - -Create the following collections: - -- `Hidden: Homepage Featured Items` -- Products in this collection are displayed in the three featured blocks on the homepage. -- `Hidden: Homepage Carousel` -- Products in this collection are displayed in the auto-scrolling carousel section on the homepage. - -![Shopify collections](https://user-images.githubusercontent.com/446260/233220543-81896a2b-7085-4abc-a4f1-ce321e08b953.jpg) - -![Shopify collection detail](https://user-images.githubusercontent.com/446260/233220544-ecd4c069-49fc-4a0b-8378-aa5e1b4b5257.jpg) - -#### Pages - -`https://SHOPIFY_STORE_SUBDOMAIN.myshopify.com/admin/pages` - -Next.js Commerce contains a dynamic `[page]` route. It will use the value to look for a corresponding page in Shopify. If a page is found, it will display its rich content using Tailwind's prose. If a page is not found, a 404 page is displayed. - -![Shopify pages](https://user-images.githubusercontent.com/446260/233221142-4dc3fa56-5256-4d84-b0a3-331ffb7d79b2.jpg) - -![Shopify page detail](https://user-images.githubusercontent.com/446260/233247700-cbeaf917-fb67-49e9-b9b9-5ee8cb188639.jpg) - -#### Navigation menus - -`https://SHOPIFY_STORE_SUBDOMAIN.myshopify.com/admin/menus` - -Next.js Commerce's header and footer navigation is pre-configured to be controlled by Shopify navigation menus. This means you have full control over what links go here. They can be to collections, pages, external links, and more. - -Create the following navigation menus: - -- `Next.js Frontend Header Menu` -- Menu items to be shown in the headless frontend header. -- `Next.js Frontend Footer Menu` -- Menu items to be shown in the headless frontend footer. - -![Shopify navigation menus](https://user-images.githubusercontent.com/446260/233220571-33f9d5a8-1206-4ab4-ad79-83b4ca954331.jpg) - -![Shopify navigation menu detail](https://user-images.githubusercontent.com/446260/233220573-5f03a51f-4100-461f-a696-f085856e391b.jpg) - -#### SEO - -Shopify's products, collections, pages, etc. allow you to create custom SEO titles and descriptions. Next.js Commerce is pre-configured to display these custom values, but also comes with sensible default fallbacks if they are not provided. - -![Shopify SEO](https://user-images.githubusercontent.com/446260/233247701-0ff2a560-7949-4e6c-b3a8-8168ed6341f8.jpg) +Create a `MEDUSA_BACKEND_API` environment variable in you Next.js Commerce project and use your Medusa backend domain as the the value (default: `http://localhost:9000`). diff --git a/app/[page]/page.tsx b/app/[page]/page.tsx index 7e0defed3..00e118f37 100644 --- a/app/[page]/page.tsx +++ b/app/[page]/page.tsx @@ -1,7 +1,6 @@ import type { Metadata } from 'next'; import Prose from 'components/prose'; -import { getPage } from 'lib/shopify'; import { notFound } from 'next/navigation'; export const runtime = 'edge'; @@ -13,7 +12,7 @@ export async function generateMetadata({ }: { params: { page: string }; }): Promise { - const page = await getPage(params.page); + const page: any = null; if (!page) return notFound(); @@ -36,7 +35,7 @@ export async function generateMetadata({ } export default async function Page({ params }: { params: { page: string } }) { - const page = await getPage(params.page); + const page: any = null; if (!page) return notFound(); diff --git a/app/api/cart/route.ts b/app/api/cart/route.ts index 15f5fb09f..87b46d184 100644 --- a/app/api/cart/route.ts +++ b/app/api/cart/route.ts @@ -1,8 +1,8 @@ import { cookies } from 'next/headers'; import { NextRequest, NextResponse } from 'next/server'; -import { addToCart, removeFromCart, updateCart } from 'lib/shopify'; -import { isShopifyError } from 'lib/type-guards'; +import { addToCart, removeFromCart, updateCart } from 'lib/medusa'; +import { isMedusaError } from 'lib/type-guards'; function formatErrorMessage(err: Error): string { return JSON.stringify(err, Object.getOwnPropertyNames(err)); @@ -10,16 +10,16 @@ function formatErrorMessage(err: Error): string { export async function POST(req: NextRequest): Promise { const cartId = cookies().get('cartId')?.value; - const { merchandiseId } = await req.json(); + const { variantId } = await req.json(); - if (!cartId?.length || !merchandiseId?.length) { + if (!cartId?.length || !variantId?.length) { return NextResponse.json({ error: 'Missing cartId or variantId' }, { status: 400 }); } try { - await addToCart(cartId, [{ merchandiseId, quantity: 1 }]); + await addToCart(cartId, { variantId, quantity: 1 }); return NextResponse.json({ status: 204 }); } catch (e) { - if (isShopifyError(e)) { + if (isMedusaError(e)) { return NextResponse.json({ message: formatErrorMessage(e.message) }, { status: e.status }); } @@ -29,25 +29,22 @@ export async function POST(req: NextRequest): Promise { export async function PUT(req: NextRequest): Promise { const cartId = cookies().get('cartId')?.value; - const { variantId, quantity, lineId } = await req.json(); + const { lineItemId, quantity } = await req.json(); - if (!cartId || !variantId || !quantity || !lineId) { + if (!cartId || !quantity || !lineItemId) { return NextResponse.json( - { error: 'Missing cartId, variantId, lineId, or quantity' }, + { error: 'Missing cartId, variantId, lineItemId, or quantity' }, { status: 400 } ); } try { - await updateCart(cartId, [ - { - id: lineId, - merchandiseId: variantId, - quantity - } - ]); + await updateCart(cartId, { + lineItemId, + quantity + }); return NextResponse.json({ status: 204 }); } catch (e) { - if (isShopifyError(e)) { + if (isMedusaError(e)) { return NextResponse.json({ message: formatErrorMessage(e.message) }, { status: e.status }); } @@ -57,16 +54,16 @@ export async function PUT(req: NextRequest): Promise { export async function DELETE(req: NextRequest): Promise { const cartId = cookies().get('cartId')?.value; - const { lineId } = await req.json(); + const lineItemId = req.nextUrl.searchParams.get('lineItemId'); - if (!cartId || !lineId) { - return NextResponse.json({ error: 'Missing cartId or lineId' }, { status: 400 }); + if (!cartId || !lineItemId) { + return NextResponse.json({ error: 'Missing cartId or lineItemId' }, { status: 400 }); } try { - await removeFromCart(cartId, [lineId]); + await removeFromCart(cartId, lineItemId); return NextResponse.json({ status: 204 }); } catch (e) { - if (isShopifyError(e)) { + if (isMedusaError(e)) { return NextResponse.json({ message: formatErrorMessage(e.message) }, { status: e.status }); } diff --git a/app/page.tsx b/app/page.tsx index a928be52e..7b400c45a 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -6,7 +6,7 @@ import { Suspense } from 'react'; export const runtime = 'edge'; 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 Medusa.', openGraph: { images: [ { diff --git a/app/product/[handle]/page.tsx b/app/product/[handle]/page.tsx index 612cd0236..ce924f09e 100644 --- a/app/product/[handle]/page.tsx +++ b/app/product/[handle]/page.tsx @@ -10,8 +10,8 @@ import { Gallery } from 'components/product/gallery'; import { VariantSelector } from 'components/product/variant-selector'; import Prose from 'components/prose'; import { HIDDEN_PRODUCT_TAG } from 'lib/constants'; -import { getProduct, getProductRecommendations } from 'lib/shopify'; -import { Image } from 'lib/shopify/types'; +import { getProduct } from 'lib/medusa'; +import { Image } from 'lib/medusa/types'; export const runtime = 'edge'; @@ -28,8 +28,8 @@ export async function generateMetadata({ const hide = !product.tags.includes(HIDDEN_PRODUCT_TAG); return { - title: product.seo.title || product.title, - description: product.seo.description || product.description, + title: product.title, + description: product.description, robots: { index: hide, follow: hide, @@ -61,17 +61,19 @@ export default async function ProductPage({ params }: { params: { handle: string return (
-
- ({ - src: image.url, - altText: image.altText - }))} - /> -
+ {product.images && ( +
+ ({ + src: image.url, + altText: image.altText ?? '' + }))} + /> +
+ )}
{/* @ts-expect-error Server Component */} @@ -97,7 +99,8 @@ export default async function ProductPage({ params }: { params: { handle: string } async function RelatedProducts({ id }: { id: string }) { - const relatedProducts = await getProductRecommendations(id); + // const relatedProducts = await getProductRecommendations(id); + const relatedProducts: any[] = []; if (!relatedProducts.length) return null; diff --git a/app/search/[collection]/page.tsx b/app/search/[collection]/page.tsx index 0e1782b7d..60f73f280 100644 --- a/app/search/[collection]/page.tsx +++ b/app/search/[collection]/page.tsx @@ -1,4 +1,4 @@ -import { getCollection, getCollectionProducts } from 'lib/shopify'; +import { getCollection, getCollectionProducts } from 'lib/medusa'; import { Metadata } from 'next'; import { notFound } from 'next/navigation'; diff --git a/app/search/page.tsx b/app/search/page.tsx index 354a2022f..3b6809269 100644 --- a/app/search/page.tsx +++ b/app/search/page.tsx @@ -1,7 +1,7 @@ import Grid from 'components/grid'; import ProductGridItems from 'components/layout/product-grid-items'; import { defaultSort, sorting } from 'lib/constants'; -import { getProducts } from 'lib/shopify'; +import { getProducts } from 'lib/medusa'; export const runtime = 'edge'; diff --git a/app/sitemap.ts b/app/sitemap.ts index 1a0fd8232..f2789dba2 100644 --- a/app/sitemap.ts +++ b/app/sitemap.ts @@ -1,4 +1,4 @@ -import { getCollections, getPages, getProducts } from 'lib/shopify'; +import { getCollections, getProducts } from 'lib/medusa'; import { MetadataRoute } from 'next'; const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL @@ -23,11 +23,5 @@ export default async function sitemap(): Promise ({ - url: `${baseUrl}/${page.handle}`, - lastModified: page.updatedAt - })); - - return [...routesMap, ...collectionsMap, ...productsMap, ...pagesMap]; + return [...routesMap, ...collectionsMap, ...productsMap]; } diff --git a/components/carousel.tsx b/components/carousel.tsx index 30b6a4733..a1ddcfda7 100644 --- a/components/carousel.tsx +++ b/components/carousel.tsx @@ -1,4 +1,4 @@ -import { getCollectionProducts } from 'lib/shopify'; +import { getCollectionProducts } from 'lib/medusa'; import Image from 'next/image'; import Link from 'next/link'; diff --git a/components/cart/button.tsx b/components/cart/button.tsx index aed87ee7a..5a3f538f7 100644 --- a/components/cart/button.tsx +++ b/components/cart/button.tsx @@ -6,7 +6,7 @@ import { useCookies } from 'react-cookie'; import CartIcon from 'components/icons/cart'; import CartModal from './modal'; -import type { Cart } from 'lib/shopify/types'; +import type { Cart } from 'lib/medusa/types'; export default function CartButton({ cart, diff --git a/components/cart/delete-item-button.tsx b/components/cart/delete-item-button.tsx index 789a87754..397514198 100644 --- a/components/cart/delete-item-button.tsx +++ b/components/cart/delete-item-button.tsx @@ -4,7 +4,7 @@ import { useRouter } from 'next/navigation'; import { startTransition, useState } from 'react'; import clsx from 'clsx'; -import type { CartItem } from 'lib/shopify/types'; +import type { CartItem } from 'lib/medusa/types'; export default function DeleteItemButton({ item }: { item: CartItem }) { const router = useRouter(); @@ -13,12 +13,10 @@ export default function DeleteItemButton({ item }: { item: CartItem }) { async function handleRemove() { setRemoving(true); - const response = await fetch(`/api/cart`, { - method: 'DELETE', - body: JSON.stringify({ - lineId: item.id - }) + const response = await fetch(`/api/cart?lineItemId=${item.id}`, { + method: 'DELETE' }); + const data = await response.json(); if (data.error) { diff --git a/components/cart/edit-item-quantity-button.tsx b/components/cart/edit-item-quantity-button.tsx index 2249bd1aa..765cd1070 100644 --- a/components/cart/edit-item-quantity-button.tsx +++ b/components/cart/edit-item-quantity-button.tsx @@ -4,7 +4,7 @@ import { startTransition, useState } from 'react'; import clsx from 'clsx'; import MinusIcon from 'components/icons/minus'; import PlusIcon from 'components/icons/plus'; -import type { CartItem } from 'lib/shopify/types'; +import type { CartItem } from 'lib/medusa/types'; import LoadingDots from '../loading-dots'; export default function EditItemQuantityButton({ @@ -20,13 +20,18 @@ export default function EditItemQuantityButton({ async function handleEdit() { setEditing(true); - const response = await fetch(`/api/cart`, { - method: type === 'minus' && item.quantity - 1 === 0 ? 'DELETE' : 'PUT', - body: JSON.stringify({ - lineId: item.id, - variantId: item.merchandise.id, - quantity: type === 'plus' ? item.quantity + 1 : item.quantity - 1 - }) + const method = type === 'minus' && item.quantity - 1 === 0 ? 'DELETE' : 'PUT'; + const url = method === 'PUT' ? '/api/cart' : `/api/cart?lineItemId=${item.id}`; + + const response = await fetch(url, { + method, + body: + method === 'PUT' + ? JSON.stringify({ + lineItemId: item.id, + quantity: type === 'plus' ? item.quantity + 1 : item.quantity - 1 + }) + : null }); const data = await response.json(); diff --git a/components/cart/index.tsx b/components/cart/index.tsx index 0e3ffcdc1..8d9adca32 100644 --- a/components/cart/index.tsx +++ b/components/cart/index.tsx @@ -1,4 +1,4 @@ -import { createCart, getCart } from 'lib/shopify'; +import { createCart, getCart } from 'lib/medusa'; import { cookies } from 'next/headers'; import CartButton from './button'; diff --git a/components/cart/modal.tsx b/components/cart/modal.tsx index 4220cfe94..413e89337 100644 --- a/components/cart/modal.tsx +++ b/components/cart/modal.tsx @@ -7,7 +7,7 @@ import CloseIcon from 'components/icons/close'; import ShoppingBagIcon from 'components/icons/shopping-bag'; import Price from 'components/price'; import { DEFAULT_OPTION } from 'lib/constants'; -import type { Cart } from 'lib/shopify/types'; +import type { Cart } from 'lib/medusa/types'; import { createUrl } from 'lib/utils'; import DeleteItemButton from './delete-item-button'; import EditItemQuantityButton from './edit-item-quantity-button'; @@ -70,13 +70,13 @@ export default function CartModal({
- {cart.lines.length === 0 ? ( + {cart.lines?.length === 0 ? (

Your cart is empty.

) : null} - {cart.lines.length !== 0 ? ( + {cart.lines?.length !== 0 ? (
    {cart.lines.map((item, i) => { @@ -107,7 +107,8 @@ export default function CartModal({ height={64} alt={ item.merchandise.product.featuredImage.altText || - item.merchandise.product.title + item.merchandise.product.title || + '' } src={item.merchandise.product.featuredImage.url} /> diff --git a/components/grid/three-items.tsx b/components/grid/three-items.tsx index 6814a171a..8bbf671e6 100644 --- a/components/grid/three-items.tsx +++ b/components/grid/three-items.tsx @@ -1,6 +1,6 @@ import { GridTileImage } from 'components/grid/tile'; -import { getCollectionProducts } from 'lib/shopify'; -import type { Product } from 'lib/shopify/types'; +import { getCollectionProducts } from 'lib/medusa'; +import type { Product } from 'lib/medusa/types'; import Link from 'next/link'; function ThreeItemGridItem({ diff --git a/components/layout/footer.tsx b/components/layout/footer.tsx index 72e9e03fc..d9d8afb7e 100644 --- a/components/layout/footer.tsx +++ b/components/layout/footer.tsx @@ -3,15 +3,15 @@ import Link from 'next/link'; import GitHubIcon from 'components/icons/github'; import LogoIcon from 'components/icons/logo'; import VercelIcon from 'components/icons/vercel'; -import { getMenu } from 'lib/shopify'; -import { Menu } from 'lib/shopify/types'; +import { Menu } from 'lib/medusa/types'; const { SITE_NAME } = process.env; export default async function Footer() { const currentYear = new Date().getFullYear(); const copyrightDate = 2023 + (currentYear > 2023 ? `-${currentYear}` : ''); - const menu = await getMenu('next-js-frontend-footer-menu'); + // const menu = await getMenu('next-js-frontend-footer-menu'); + const menu: any[] = []; return (