Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix removing from cart and add Not Found UI #35

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 12 additions & 3 deletions 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 @@ -300,7 +301,10 @@ export async function addToCart(
return bigCommerceToVercelCart(bigCommerceCart, productsByIdList, checkout, checkoutUrl);
}

export async function removeFromCart(cartId: string, lineIds: string[]): Promise<VercelCart | undefined> {
export async function removeFromCart(
cartId: string,
lineIds: string[]
): Promise<VercelCart | undefined> {
let cartState: { status: number; body: BigCommerceDeleteCartItemOperation };
const removeCartItem = async (itemId: string) => {
const res = await bigCommerceFetch<BigCommerceDeleteCartItemOperation>({
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -585,6 +589,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
Loading