Skip to content

Products can be added at max stock amount - #35

Open
anttiasmala wants to merge 11 commits into
mainfrom
products-can-be-added-at-max-stock-amount
Open

Products can be added at max stock amount#35
anttiasmala wants to merge 11 commits into
mainfrom
products-can-be-added-at-max-stock-amount

Conversation

@anttiasmala

Copy link
Copy Markdown
Owner

Products can be added to cart until max stock amount is reached

@vercel

vercel Bot commented Jul 29, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
shop ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 29, 2025 2:51pm

Comment thread pages/api/cart/[uuid].ts
createdAt: true,
updatedAt: true,
uuid: true,
stock: true,

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omit stock from request sent to frontend for now

Comment thread pages/api/cart/[uuid].ts
Comment on lines -67 to +73
return res.status(200).json(cartItems);
const clonedCartItems = structuredClone(cartItems);
const newCartItems = clonedCartItems.map((clonedCartItem) => {
return { ...clonedCartItem, whenRequestSent: new Date() };
});

return res.status(200).json(newCartItems);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StructureClone the cartItems and add newCartItems. Loop them through and add whenRequsetSent attribute. This will make GET requests differ from each other so useEffect will "grab" them

Comment thread pages/api/cart/[uuid].ts
Comment on lines -92 to +135
const upsertedItem = await prisma.cartItem.upsert({
const cartItem = await prisma.cartItem.findFirst({
where: {
cartUUID_productUUID: {
productUUID: product.uuid,
cartUUID: cart.uuid,
},
},
create: {
cartUUID: cart.uuid,
productUUID: product.uuid,
amount: productToBeAdded.amount,
},
update: {
amount: {
increment: productToBeAdded.amount,
});

// cartItem does not exist, create a new one
if (!cartItem) {
const createdItem = await prisma.cartItem.create({
data: {
amount: productToBeAdded.amount,
cartUUID: cart.uuid,
productUUID: product.uuid,
},
});
return res.status(200).json(createdItem);
}

if (cartItem.amount + 1 > product.stock) {
throw new HttpError('Maximum amount of products reached!', 400);
}

const newAmount =
cartItem.amount >= product.stock ? product.stock : cartItem.amount + 1;

const updatedItem = await prisma.cartItem.update({
where: {
cartUUID_productUUID: {
cartUUID: cart.uuid,
productUUID: product.uuid,
},
},
data: {
amount: newAmount,
},
});
return res.status(200).json(upsertedItem);
return res.status(200).json(updatedItem);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change upsertedItem to createdItem/updatedItem. Uses "old" method due to need to know the amount of products in cart

Comment thread pages/cart.tsx
Comment on lines -232 to +242
// this will prevent request sent to backend if amount of products were same
// so for example if user just click the input and clicks out without changing anything
if (amountOfProductBeforeBlur === amountOfProduct) return;
await mutateAsync();
await queryClient.invalidateQueries({
queryKey: QueryAndMutationKeys.CartProducts,
});
try {
// this will prevent request sent to backend if amount of products were same
// so for example if user just click the input and clicks out without changing anything
if (amountOfProductBeforeBlur === amountOfProduct) return;
await mutateAsync();
await queryClient.invalidateQueries({
queryKey: QueryAndMutationKeys.CartProducts,
});
} catch (e) {
handleError(e);
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add try/catch block

Comment thread shared/types.ts
Comment on lines -32 to +37
export type GetCart = { amount: number; createdAt: Date; updatedAt: Date } & {
export type GetCart = {
amount: number;
createdAt: Date;
updatedAt: Date;
whenRequestSent?: Date;
} & {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whenRequestSent's idea is to tell when the request was sent. Just to make GET request of products change every time so useEffect will "grab" them

Comment thread pages/products.tsx
Comment on lines -224 to +225
console.error(e);
handleError(e);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleError is better here

Comment thread pages/cart.tsx
Comment on lines -276 to +278
await queryClient.invalidateQueries({
queryKey: QueryAndMutationKeys.CartProducts,
});
await refetchCartProducts(queryClient);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this new function to replace the old queryClient.invalidateQueries etc. Might make the function generic later on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant