refactor: replace shopify imports

This commit is contained in:
Victor Gerbrands 2023-05-02 15:42:35 +02:00
parent 7021789621
commit 3ab0bd18d0
20 changed files with 45 additions and 45 deletions

View File

@ -1,7 +1,7 @@
import { cookies } from 'next/headers'; import { cookies } from 'next/headers';
import { NextRequest, NextResponse } from 'next/server'; import { NextRequest, NextResponse } from 'next/server';
import { addToCart, removeFromCart, updateCart } from 'lib/shopify'; import { addToCart, removeFromCart, updateCart } from 'lib/medusa';
import { isShopifyError } from 'lib/type-guards'; import { isShopifyError } from 'lib/type-guards';
function formatErrorMessage(err: Error): string { function formatErrorMessage(err: Error): string {
@ -10,13 +10,13 @@ function formatErrorMessage(err: Error): string {
export async function POST(req: NextRequest): Promise<Response> { export async function POST(req: NextRequest): Promise<Response> {
const cartId = cookies().get('cartId')?.value; 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 }); return NextResponse.json({ error: 'Missing cartId or variantId' }, { status: 400 });
} }
try { try {
await addToCart(cartId, [{ merchandiseId, quantity: 1 }]); await addToCart(cartId, [{ variantId, quantity: 1 }]);
return NextResponse.json({ status: 204 }); return NextResponse.json({ status: 204 });
} catch (e) { } catch (e) {
if (isShopifyError(e)) { if (isShopifyError(e)) {

View File

@ -10,8 +10,8 @@ import { Gallery } from 'components/product/gallery';
import { VariantSelector } from 'components/product/variant-selector'; import { VariantSelector } from 'components/product/variant-selector';
import Prose from 'components/prose'; import Prose from 'components/prose';
import { HIDDEN_PRODUCT_TAG } from 'lib/constants'; import { HIDDEN_PRODUCT_TAG } from 'lib/constants';
import { getProduct } from 'lib/shopify'; import { getProduct } from 'lib/medusa';
import { Image } from 'lib/shopify/types'; import { Image } from 'lib/medusa/types';
export const runtime = 'edge'; export const runtime = 'edge';

View File

@ -1,4 +1,4 @@
import { getCollection, getCollectionProducts } from 'lib/shopify'; import { getCollection, getCollectionProducts } from 'lib/medusa';
import { Metadata } from 'next'; import { Metadata } from 'next';
import { notFound } from 'next/navigation'; import { notFound } from 'next/navigation';

View File

@ -1,7 +1,7 @@
import Grid from 'components/grid'; 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';
import { getProducts } from 'lib/shopify'; import { getProducts } from 'lib/medusa';
export const runtime = 'edge'; export const runtime = 'edge';

View File

@ -1,4 +1,4 @@
import { getCollections, getProducts } from 'lib/shopify'; import { getCollections, getProducts } from 'lib/medusa';
import { MetadataRoute } from 'next'; import { MetadataRoute } from 'next';
const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL

View File

@ -1,4 +1,4 @@
import { getCollectionProducts } from 'lib/shopify'; import { getCollectionProducts } from 'lib/medusa';
import Image from 'next/image'; import Image from 'next/image';
import Link from 'next/link'; import Link from 'next/link';

View File

@ -6,7 +6,7 @@ import { useCookies } from 'react-cookie';
import CartIcon from 'components/icons/cart'; import CartIcon from 'components/icons/cart';
import CartModal from './modal'; import CartModal from './modal';
import type { Cart } from 'lib/shopify/types'; import type { Cart } from 'lib/medusa/types';
export default function CartButton({ export default function CartButton({
cart, cart,

View File

@ -4,7 +4,7 @@ import { useRouter } from 'next/navigation';
import { startTransition, useState } from 'react'; import { startTransition, useState } from 'react';
import clsx from 'clsx'; 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 }) { export default function DeleteItemButton({ item }: { item: CartItem }) {
const router = useRouter(); const router = useRouter();

View File

@ -4,7 +4,7 @@ import { startTransition, useState } from 'react';
import clsx from 'clsx'; import clsx from 'clsx';
import MinusIcon from 'components/icons/minus'; import MinusIcon from 'components/icons/minus';
import PlusIcon from 'components/icons/plus'; 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'; import LoadingDots from '../loading-dots';
export default function EditItemQuantityButton({ export default function EditItemQuantityButton({

View File

@ -1,4 +1,4 @@
import { createCart, getCart } from 'lib/shopify'; import { createCart, getCart } from 'lib/medusa';
import { cookies } from 'next/headers'; import { cookies } from 'next/headers';
import CartButton from './button'; import CartButton from './button';

View File

@ -7,7 +7,7 @@ import CloseIcon from 'components/icons/close';
import ShoppingBagIcon from 'components/icons/shopping-bag'; import ShoppingBagIcon from 'components/icons/shopping-bag';
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 type { Cart } from 'lib/medusa/types';
import { createUrl } from 'lib/utils'; import { createUrl } from 'lib/utils';
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';
@ -70,13 +70,13 @@ export default function CartModal({
</button> </button>
</div> </div>
{cart.lines.length === 0 ? ( {cart.lines?.length === 0 ? (
<div className="mt-20 flex w-full flex-col items-center justify-center overflow-hidden"> <div className="mt-20 flex w-full flex-col items-center justify-center overflow-hidden">
<ShoppingBagIcon className="h-16" /> <ShoppingBagIcon className="h-16" />
<p className="mt-6 text-center text-2xl font-bold">Your cart is empty.</p> <p className="mt-6 text-center text-2xl font-bold">Your cart is empty.</p>
</div> </div>
) : null} ) : null}
{cart.lines.length !== 0 ? ( {cart.lines?.length !== 0 ? (
<div className="flex h-full flex-col justify-between overflow-hidden"> <div className="flex h-full flex-col justify-between overflow-hidden">
<ul className="flex-grow overflow-auto p-6"> <ul className="flex-grow overflow-auto p-6">
{cart.lines.map((item, i) => { {cart.lines.map((item, i) => {

View File

@ -1,6 +1,6 @@
import { GridTileImage } from 'components/grid/tile'; import { GridTileImage } from 'components/grid/tile';
import { getCollectionProducts } from 'lib/shopify'; import { getCollectionProducts } from 'lib/medusa';
import type { Product } from 'lib/shopify/types'; import type { Product } from 'lib/medusa/types';
import Link from 'next/link'; import Link from 'next/link';
function ThreeItemGridItem({ function ThreeItemGridItem({
@ -41,13 +41,13 @@ export async function ThreeItemGrid() {
if (!homepageItems[0] || !homepageItems[1] || !homepageItems[2]) return null; if (!homepageItems[0] || !homepageItems[1] || !homepageItems[2]) return null;
// const [firstProduct, secondProduct, thirdProduct] = homepageItems; const [firstProduct, secondProduct, thirdProduct] = homepageItems;
return ( return (
<section className="lg:grid lg:grid-cols-6 lg:grid-rows-2" data-testid="homepage-products"> <section className="lg:grid lg:grid-cols-6 lg:grid-rows-2" data-testid="homepage-products">
{/* <ThreeItemGridItem size="full" item={firstProduct} background="purple" /> <ThreeItemGridItem size="full" item={firstProduct} background="purple" />
<ThreeItemGridItem size="half" item={secondProduct} background="black" /> <ThreeItemGridItem size="half" item={secondProduct} background="black" />
<ThreeItemGridItem size="half" item={thirdProduct} background="pink" /> */} <ThreeItemGridItem size="half" item={thirdProduct} background="pink" />
</section> </section>
); );
} }

View File

@ -3,7 +3,7 @@ import Link from 'next/link';
import GitHubIcon from 'components/icons/github'; import GitHubIcon from 'components/icons/github';
import LogoIcon from 'components/icons/logo'; import LogoIcon from 'components/icons/logo';
import VercelIcon from 'components/icons/vercel'; import VercelIcon from 'components/icons/vercel';
import { Menu } from 'lib/shopify/types'; import { Menu } from 'lib/medusa/types';
const { SITE_NAME } = process.env; const { SITE_NAME } = process.env;

View File

@ -1,10 +1,7 @@
import Link from 'next/link'; import Link from 'next/link';
import { Suspense } from 'react';
import Cart from 'components/cart';
import CartIcon from 'components/icons/cart';
import LogoIcon from 'components/icons/logo'; import LogoIcon from 'components/icons/logo';
import { Menu } from 'lib/shopify/types'; import { Menu } from 'lib/medusa/types';
import MobileMenu from './mobile-menu'; import MobileMenu from './mobile-menu';
import Search from './search'; import Search from './search';
@ -43,10 +40,10 @@ export default async function Navbar() {
</div> </div>
<div className="flex w-1/3 justify-end"> <div className="flex w-1/3 justify-end">
<Suspense fallback={<CartIcon className="h-6" />}> {/* <Suspense fallback={<CartIcon className="h-6" />}> */}
{/* @ts-expect-error Server Component */} {/* @ts-expect-error Server Component */}
<Cart /> {/* <Cart /> */}
</Suspense> {/* </Suspense> */}
</div> </div>
</nav> </nav>
); );

View File

@ -8,7 +8,7 @@ import { useEffect, useState } from 'react';
import CloseIcon from 'components/icons/close'; import CloseIcon from 'components/icons/close';
import MenuIcon from 'components/icons/menu'; import MenuIcon from 'components/icons/menu';
import { Menu } from 'lib/shopify/types'; import { Menu } from 'lib/medusa/types';
import Search from './search'; import Search from './search';
export default function MobileMenu({ menu }: { menu: Menu[] }) { export default function MobileMenu({ menu }: { menu: Menu[] }) {

View File

@ -1,6 +1,6 @@
import Grid from 'components/grid'; import Grid from 'components/grid';
import { GridTileImage } from 'components/grid/tile'; import { GridTileImage } from 'components/grid/tile';
import { Product } from 'lib/shopify/types'; import { Product } from 'lib/medusa/types';
import Link from 'next/link'; import Link from 'next/link';
export default function ProductGridItems({ products }: { products: Product[] }) { export default function ProductGridItems({ products }: { products: Product[] }) {

View File

@ -1,7 +1,7 @@
import clsx from 'clsx'; import clsx from 'clsx';
import { Suspense } from 'react'; import { Suspense } from 'react';
import { getCollections } from 'lib/shopify'; import { getCollections } from 'lib/medusa';
import FilterList from './filter'; import FilterList from './filter';
async function CollectionList() { async function CollectionList() {

View File

@ -5,7 +5,7 @@ import { useRouter, useSearchParams } from 'next/navigation';
import { useEffect, useState, useTransition } from 'react'; import { useEffect, useState, useTransition } from 'react';
import LoadingDots from 'components/loading-dots'; import LoadingDots from 'components/loading-dots';
import { ProductVariant } from 'lib/shopify/types'; import { ProductVariant } from 'lib/medusa/types';
export function AddToCart({ export function AddToCart({
variants, variants,

View File

@ -1,7 +1,7 @@
'use client'; 'use client';
import clsx from 'clsx'; import clsx from 'clsx';
import { ProductOption, ProductVariant } from 'lib/shopify/types'; import { ProductOption, ProductVariant } from 'lib/medusa/types';
import { createUrl } from 'lib/utils'; import { createUrl } from 'lib/utils';
import Link from 'next/link'; import Link from 'next/link';
import { usePathname, useRouter, useSearchParams } from 'next/navigation'; import { usePathname, useRouter, useSearchParams } from 'next/navigation';

View File

@ -1,5 +1,6 @@
import { isShopifyError } from 'lib/type-guards'; import { isShopifyError } from 'lib/type-guards';
import { Cart, Collection, Product } from './types'; import { ProductCollection } from '../medusa/types';
import { Cart, Product } from './types';
// const endpoint = `${process.env.MEDUSA_BACKEND_API!}`; // const endpoint = `${process.env.MEDUSA_BACKEND_API!}`;
const endpoint = `http://localhost:9000/store`; const endpoint = `http://localhost:9000/store`;
@ -49,18 +50,19 @@ export default async function medusaRequest(
export async function createCart(): Promise<Cart> { export async function createCart(): Promise<Cart> {
const res = await medusaRequest('POST', '/carts', {}); const res = await medusaRequest('POST', '/carts', {});
console.log('Cart created!');
console.log(res);
return res.body.cart; return res.body.cart;
} }
export async function addToCart( export async function addToCart(
cartId: string, cartId: string,
lines: { merchandiseId: string; quantity: number }[] lineItems: { variantId: string; quantity: number }[]
): Promise<Cart> { ): Promise<Cart> {
console.log(lines); console.log(lineItems);
// TODO: transform lines into Medusa line items // TODO: transform lines into Medusa line items
const res = await medusaRequest('POST', `/carts/${cartId}/line-items`, { const res = await medusaRequest('POST', `/carts/${cartId}/line-items`, {
variant_id: 'something', lineItems
quantity: 1
}); });
return res.body.data.cart; return res.body.data.cart;
@ -93,8 +95,9 @@ export async function getCart(cartId: string): Promise<Cart | null> {
return res.body.cart; return res.body.cart;
} }
export async function getCollection(handle: string): Promise<Collection | undefined> { export async function getCollection(handle: string): Promise<ProductCollection | undefined> {
const res = await medusaRequest('get', `/collections?handle[]=${handle}&limit=1`); const res = await medusaRequest('get', `/collections?handle[]=${handle}&limit=1`);
console.log({ collection: res.body.collection });
return res.body.collection; return res.body.collection;
} }
@ -103,11 +106,11 @@ export async function getCollectionProducts(handle: string): Promise<Product[]>
if (!res.body?.collection?.products) { if (!res.body?.collection?.products) {
return []; return [];
} }
console.log({ 'collection products': res.body.collection.products });
return res.body.collection.products; return res.body.collection.products;
} }
export async function getCollections(): Promise<Collection[]> { export async function getCollections(): Promise<ProductCollection[]> {
const collections = [ const collections = [
{ {
handle: '', handle: '',
@ -122,7 +125,7 @@ export async function getCollections(): Promise<Collection[]> {
} }
]; ];
return collections; return [];
} }
export async function getProduct(handle: string): Promise<Product | undefined> { export async function getProduct(handle: string): Promise<Product | undefined> {