Skip to content

Commit b10fbb8

Browse files
bc-yevhenii-buliuk“bc-yevhenii-buliuk”
andauthored
fix removing from cart and add not-found ui (#36)
Co-authored-by: “bc-yevhenii-buliuk” <“yevhenii.buliuk@bigcommerce.com”>
1 parent 5936cd5 commit b10fbb8

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

components/cart/actions.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use server';
22

3-
import { TAGS } from 'lib/constants';
43
import { addToCart, removeFromCart, updateCart } from 'lib/bigcommerce';
4+
import { TAGS } from 'lib/constants';
55
import { revalidateTag } from 'next/cache';
66
import { cookies } from 'next/headers';
77

@@ -70,8 +70,13 @@ export async function updateItemQuantity(
7070

7171
try {
7272
if (quantity === 0) {
73-
await removeFromCart(cartId, [lineId]);
73+
const response = await removeFromCart(cartId, [lineId]);
7474
revalidateTag(TAGS.cart);
75+
76+
if (!response && cartId) {
77+
cookies().delete('cartId');
78+
}
79+
7580
return;
7681
}
7782

lib/bigcommerce/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isVercelCommerceError } from 'lib/type-guards';
2+
import { notFound } from 'next/navigation';
23
import { NextRequest, NextResponse } from 'next/server';
34
import { BIGCOMMERCE_GRAPHQL_API_ENDPOINT } from './constants';
45

@@ -81,7 +82,7 @@ const getEntityIdByHandle = async (entityHandle: string) => {
8182
}
8283
});
8384

84-
return res.body.data.site.route.node.entityId;
85+
return res.body.data.site.route.node?.entityId;
8586
};
8687

8788
export async function bigCommerceFetch<T>({
@@ -585,6 +586,11 @@ export async function getMenu(handle: string): Promise<VercelMenu[]> {
585586

586587
export async function getPage(handle: string): Promise<VercelPage> {
587588
const entityId = await getEntityIdByHandle(handle);
589+
590+
if (!entityId) {
591+
notFound();
592+
}
593+
588594
const res = await bigCommerceFetch<BigCommercePageOperation>({
589595
query: getPageQuery,
590596
variables: {

0 commit comments

Comments
 (0)