From 5b481599a526d28f83fad0d940de1b9b2452d5d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cbc-yevhenii-buliuk=E2=80=9D?= <“yevhenii.buliuk@bigcommerce.com”> Date: Fri, 22 Dec 2023 14:13:43 +0200 Subject: [PATCH] fix removing from cart and add not-found ui --- components/cart/actions.ts | 9 +++++++-- lib/bigcommerce/index.ts | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/components/cart/actions.ts b/components/cart/actions.ts index df98446aa3..d2e1a6f008 100644 --- a/components/cart/actions.ts +++ b/components/cart/actions.ts @@ -1,7 +1,7 @@ 'use server'; -import { TAGS } from 'lib/constants'; import { addToCart, removeFromCart, updateCart } from 'lib/bigcommerce'; +import { TAGS } from 'lib/constants'; import { revalidateTag } from 'next/cache'; import { cookies } from 'next/headers'; @@ -70,8 +70,13 @@ export async function updateItemQuantity( try { if (quantity === 0) { - await removeFromCart(cartId, [lineId]); + const response = await removeFromCart(cartId, [lineId]); revalidateTag(TAGS.cart); + + if (!response && cartId) { + cookies().delete('cartId'); + } + return; } diff --git a/lib/bigcommerce/index.ts b/lib/bigcommerce/index.ts index 2b6dea0db5..0ff6f932a4 100644 --- a/lib/bigcommerce/index.ts +++ b/lib/bigcommerce/index.ts @@ -1,4 +1,5 @@ import { isVercelCommerceError } from 'lib/type-guards'; +import { notFound } from 'next/navigation'; import { NextRequest, NextResponse } from 'next/server'; import { BIGCOMMERCE_GRAPHQL_API_ENDPOINT } from './constants'; @@ -81,7 +82,7 @@ const getEntityIdByHandle = async (entityHandle: string) => { } }); - return res.body.data.site.route.node.entityId; + return res.body.data.site.route.node?.entityId; }; export async function bigCommerceFetch({ @@ -300,7 +301,10 @@ export async function addToCart( return bigCommerceToVercelCart(bigCommerceCart, productsByIdList, checkout, checkoutUrl); } -export async function removeFromCart(cartId: string, lineIds: string[]): Promise { +export async function removeFromCart( + cartId: string, + lineIds: string[] +): Promise { let cartState: { status: number; body: BigCommerceDeleteCartItemOperation }; const removeCartItem = async (itemId: string) => { const res = await bigCommerceFetch({ @@ -331,7 +335,7 @@ export async function removeFromCart(cartId: string, lineIds: string[]): Promise const cart = cartState!.body.data.cart.deleteCartLineItem.cart; - if (cart === null) { + if (cart === null) { return undefined; } @@ -585,6 +589,11 @@ export async function getMenu(handle: string): Promise { export async function getPage(handle: string): Promise { const entityId = await getEntityIdByHandle(handle); + + if (!entityId) { + notFound(); + } + const res = await bigCommerceFetch({ query: getPageQuery, variables: {