Handle null ids

This commit is contained in:
Lee Robinson 2024-07-28 14:49:48 -05:00
parent c131178529
commit 2b2b897cef
4 changed files with 81 additions and 77 deletions

View File

@ -37,7 +37,7 @@ export async function removeItem(prevState: any, merchandiseId: string) {
const lineItem = cart.lines.find((line) => line.merchandise.id === merchandiseId); const lineItem = cart.lines.find((line) => line.merchandise.id === merchandiseId);
if (lineItem) { if (lineItem && lineItem.id) {
await removeFromCart(cartId, [lineItem.id]); await removeFromCart(cartId, [lineItem.id]);
revalidateTag(TAGS.cart); revalidateTag(TAGS.cart);
} else { } else {
@ -72,7 +72,7 @@ export async function updateItemQuantity(
const lineItem = cart.lines.find((line) => line.merchandise.id === merchandiseId); const lineItem = cart.lines.find((line) => line.merchandise.id === merchandiseId);
if (lineItem) { if (lineItem && lineItem.id) {
if (quantity === 0) { if (quantity === 0) {
await removeFromCart(cartId, [lineItem.id]); await removeFromCart(cartId, [lineItem.id]);
} else { } else {
@ -114,5 +114,5 @@ export async function redirectToCheckout() {
export async function createCartAndSetCookie() { export async function createCartAndSetCookie() {
let cart = await createCart(); let cart = await createCart();
cookies().set('cartId', cart.id); cookies().set('cartId', cart.id!);
} }

View File

@ -52,7 +52,7 @@ function createOrUpdateCartItem(
const totalAmount = calculateItemCost(quantity, variant.price.amount); const totalAmount = calculateItemCost(quantity, variant.price.amount);
return { return {
id: existingItem?.id || `${variant.id}_${Date.now()}`, id: existingItem?.id,
quantity, quantity,
cost: { cost: {
totalAmount: { totalAmount: {
@ -91,7 +91,7 @@ function updateCartTotals(lines: CartItem[]): Pick<Cart, 'totalQuantity' | 'cost
function createEmptyCart(): Cart { function createEmptyCart(): Cart {
return { return {
id: `optimistic_${Date.now()}`, id: undefined,
checkoutUrl: '', checkoutUrl: '',
totalQuantity: 0, totalQuantity: 0,
lines: [], lines: [],

View File

@ -90,7 +90,11 @@ export default function CartModal() {
) : ( ) : (
<div className="flex h-full flex-col justify-between overflow-hidden p-1"> <div className="flex h-full flex-col justify-between overflow-hidden p-1">
<ul className="flex-grow overflow-auto py-4"> <ul className="flex-grow overflow-auto py-4">
{cart.lines.map((item, i) => { {cart.lines
.sort((a, b) =>
a.merchandise.product.title.localeCompare(b.merchandise.product.title)
)
.map((item, i) => {
const merchandiseSearchParams = {} as MerchandiseSearchParams; const merchandiseSearchParams = {} as MerchandiseSearchParams;
item.merchandise.selectedOptions.forEach(({ name, value }) => { item.merchandise.selectedOptions.forEach(({ name, value }) => {

View File

@ -20,7 +20,7 @@ export type CartProduct = {
}; };
export type CartItem = { export type CartItem = {
id: string; id: string | undefined;
quantity: number; quantity: number;
cost: { cost: {
totalAmount: Money; totalAmount: Money;
@ -96,7 +96,7 @@ export type SEO = {
}; };
export type ShopifyCart = { export type ShopifyCart = {
id: string; id: string | undefined;
checkoutUrl: string; checkoutUrl: string;
cost: { cost: {
subtotalAmount: Money; subtotalAmount: Money;