some more cleanup

This commit is contained in:
Jieren Chen 2024-09-23 19:11:06 -04:00
parent 2a0a13e761
commit df41862e59
No known key found for this signature in database
GPG Key ID: 2FF322D21B5DB10B
4 changed files with 20 additions and 10 deletions

View File

@ -1,9 +1,9 @@
import { Carousel } from 'components/carousel';
import { getCartId } from 'components/cart/actions';
import { ThreeItemGrid } from 'components/grid/three-items';
import Footer from 'components/layout/footer';
import { Wrapper } from 'components/wrapper';
import { getCart } from 'lib/fourthwall';
import { cookies } from 'next/headers';
export const metadata = {
description: 'High-performance ecommerce store built with Next.js, Vercel, and Fourthwall.',
@ -13,7 +13,7 @@ export const metadata = {
};
export default async function HomePage({ searchParams }: { searchParams: { currency?: string } }) {
const cartId = cookies().get('cartId')?.value;
const cartId = await getCartId();
const currency = searchParams.currency || 'USD';
// Don't await the fetch, pass the Promise to the context provider
const cart = getCart(cartId, currency);

View File

@ -1,6 +1,7 @@
import type { Metadata } from 'next';
import { notFound } from 'next/navigation';
import { getCartId } from 'components/cart/actions';
import Footer from 'components/layout/footer';
import { Gallery } from 'components/product/gallery';
import { ProductProvider } from 'components/product/product-context';
@ -8,7 +9,6 @@ import { ProductDescription } from 'components/product/product-description';
import { Wrapper } from 'components/wrapper';
import { HIDDEN_PRODUCT_TAG } from 'lib/constants';
import { getCart, getProduct } from 'lib/fourthwall';
import { cookies } from 'next/headers';
import { Suspense } from 'react';
export async function generateMetadata({
@ -51,7 +51,7 @@ export async function generateMetadata({
export default async function ProductPage({ params, searchParams }: { params: { handle: string }, searchParams: { currency?: string } }) {
const currency = searchParams.currency || 'USD';
const cartId = cookies().get('cartId')?.value;
const cartId = await getCartId()
const cart = getCart(cartId, currency)

View File

@ -6,8 +6,18 @@ import { revalidateTag } from 'next/cache';
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
export async function getCartId(): Promise<string | undefined> {
const tokenHash = process.env.NEXT_PUBLIC_FW_STOREFRONT_TOKEN;
return cookies().get(`${tokenHash}/cartId`)?.value;
}
function setCartId(cartId: string) {
const tokenHash = process.env.NEXT_PUBLIC_FW_STOREFRONT_TOKEN;
cookies().set(`${tokenHash}/cartId`, cartId);
}
export async function addItem(prevState: any, selectedVariantId: string | undefined) {
let cartId = cookies().get('cartId')?.value;
let cartId = await getCartId();
if (!cartId || !selectedVariantId) {
return 'Error adding item to cart';
@ -22,7 +32,7 @@ export async function addItem(prevState: any, selectedVariantId: string | undefi
}
export async function removeItem(prevState: any, merchandiseId: string) {
let cartId = cookies().get('cartId')?.value;
let cartId = await getCartId();
if (!cartId) {
return 'Missing cart ID';
@ -98,7 +108,7 @@ export async function updateItemQuantity(
export async function redirectToCheckout(currency: string) {
const CHECKOUT_URL = process.env.NEXT_PUBLIC_FW_CHECKOUT;
let cartId = cookies().get('cartId')?.value;
let cartId = await getCartId();
if (!cartId) {
return 'Missing cart ID';
@ -115,5 +125,5 @@ export async function redirectToCheckout(currency: string) {
export async function createCartAndSetCookie() {
let cart = await createCart();
cookies().set('cartId', cart.id!);
setCartId(cart.id!!);
}

View File

@ -5,8 +5,8 @@ module.exports = {
remotePatterns: [
{
protocol: 'https',
hostname: 'storage.googleapis.com',
pathname: '/cdn.staging.fourthwall.com/**'
hostname: '**',
pathname: '**'
}
]
}