Skip to content

Commit

Permalink
fix removing from cart and add not-found ui
Browse files Browse the repository at this point in the history
  • Loading branch information
“bc-yevhenii-buliuk” committed Dec 22, 2023
1 parent 5936cd5 commit 2f0e8eb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 7 additions & 2 deletions components/cart/actions.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 7 additions & 1 deletion lib/bigcommerce/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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<T>({
Expand Down Expand Up @@ -585,6 +586,11 @@ export async function getMenu(handle: string): Promise<VercelMenu[]> {

export async function getPage(handle: string): Promise<VercelPage> {
const entityId = await getEntityIdByHandle(handle);

if (!entityId) {
notFound();
}

const res = await bigCommerceFetch<BigCommercePageOperation>({
query: getPageQuery,
variables: {
Expand Down

0 comments on commit 2f0e8eb

Please sign in to comment.