mirror of
https://github.com/vercel/commerce.git
synced 2025-05-20 08:26:59 +00:00
fix: remove line items when 0
This commit is contained in:
parent
5bcf6cb5eb
commit
4d99e73f20
@ -29,22 +29,19 @@ export async function POST(req: NextRequest): Promise<Response> {
|
||||
|
||||
export async function PUT(req: NextRequest): Promise<Response> {
|
||||
const cartId = cookies().get('cartId')?.value;
|
||||
const { variantId, quantity, lineId } = await req.json();
|
||||
const { lineItemId, quantity } = await req.json();
|
||||
|
||||
if (!cartId || !variantId || !quantity || !lineId) {
|
||||
if (!cartId || !quantity || !lineItemId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Missing cartId, variantId, lineId, or quantity' },
|
||||
{ error: 'Missing cartId, variantId, lineItemId, or quantity' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
try {
|
||||
await updateCart(cartId, [
|
||||
{
|
||||
id: lineId,
|
||||
merchandiseId: variantId,
|
||||
quantity
|
||||
}
|
||||
]);
|
||||
await updateCart(cartId, {
|
||||
lineItemId,
|
||||
quantity
|
||||
});
|
||||
return NextResponse.json({ status: 204 });
|
||||
} catch (e) {
|
||||
if (isMedusaError(e)) {
|
||||
@ -58,13 +55,13 @@ export async function PUT(req: NextRequest): Promise<Response> {
|
||||
export async function DELETE(req: NextRequest): Promise<Response> {
|
||||
const cartId = cookies().get('cartId')?.value;
|
||||
console.log(req.nextUrl);
|
||||
const lineId = req.nextUrl.searchParams.get('lineId');
|
||||
const lineItemId = req.nextUrl.searchParams.get('lineItemId');
|
||||
|
||||
if (!cartId || !lineId) {
|
||||
return NextResponse.json({ error: 'Missing cartId or lineId' }, { status: 400 });
|
||||
if (!cartId || !lineItemId) {
|
||||
return NextResponse.json({ error: 'Missing cartId or lineItemId' }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
await removeFromCart(cartId, [lineId]);
|
||||
await removeFromCart(cartId, [lineItemId]);
|
||||
return NextResponse.json({ status: 204 });
|
||||
} catch (e) {
|
||||
if (isMedusaError(e)) {
|
||||
|
@ -14,7 +14,7 @@ export default function DeleteItemButton({ item }: { item: CartItem }) {
|
||||
setRemoving(true);
|
||||
|
||||
console.log(item.id);
|
||||
const response = await fetch(`/api/cart?lineId=${item.id}`, {
|
||||
const response = await fetch(`/api/cart?lineItemId=${item.id}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
|
@ -20,13 +20,18 @@ export default function EditItemQuantityButton({
|
||||
async function handleEdit() {
|
||||
setEditing(true);
|
||||
|
||||
const response = await fetch(`/api/cart`, {
|
||||
method: type === 'minus' && item.quantity - 1 === 0 ? 'DELETE' : 'PUT',
|
||||
body: JSON.stringify({
|
||||
lineId: item.id,
|
||||
variantId: item.merchandise.id,
|
||||
quantity: type === 'plus' ? item.quantity + 1 : item.quantity - 1
|
||||
})
|
||||
const method = type === 'minus' && item.quantity - 1 === 0 ? 'DELETE' : 'PUT';
|
||||
const url = method === 'PUT' ? '/api/cart' : `/api/cart?lineItemId=${item.id}`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
method,
|
||||
body:
|
||||
method === 'PUT'
|
||||
? JSON.stringify({
|
||||
lineItemId: item.id,
|
||||
quantity: type === 'plus' ? item.quantity + 1 : item.quantity - 1
|
||||
})
|
||||
: null
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
@ -253,31 +253,31 @@ export async function addToCart(
|
||||
return reshapeCart(res.body.cart);
|
||||
}
|
||||
|
||||
export async function removeFromCart(cartId: string, lineIds: string[]): Promise<Cart> {
|
||||
export async function removeFromCart(cartId: string, lineItemIds: string[]): Promise<Cart> {
|
||||
// TODO: We only allow you to pass a single line item to delete
|
||||
const res = await medusaRequest('DELETE', `/carts/${cartId}/line-items/${lineIds[0]}`);
|
||||
console.log(res);
|
||||
const res = await medusaRequest('DELETE', `/carts/${cartId}/line-items/${lineItemIds[0]}`);
|
||||
return reshapeCart(res.body.cart);
|
||||
}
|
||||
|
||||
export async function updateCart(
|
||||
cartId: string,
|
||||
lines: { id: string; merchandiseId: string; quantity: number }[]
|
||||
{ lineItemId, quantity }: { lineItemId: string; quantity: number }
|
||||
): Promise<Cart> {
|
||||
console.log(lines);
|
||||
// TODO: transform lines into Medusa line items
|
||||
const res = await medusaRequest('POST', `/carts/${cartId}`, {});
|
||||
const res = await medusaRequest('POST', `/carts/${cartId}/line-items/${lineItemId}`, {
|
||||
quantity
|
||||
});
|
||||
return reshapeCart(res.body.cart);
|
||||
}
|
||||
|
||||
export async function getCart(cartId: string): Promise<Cart | null> {
|
||||
const res = await medusaRequest('GET', `/carts/${cartId}`);
|
||||
const cart = res.body.cart;
|
||||
|
||||
if (!res.body.cart) {
|
||||
if (!cart) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return reshapeCart(res.body.cart);
|
||||
return reshapeCart(cart);
|
||||
}
|
||||
|
||||
export async function getCollection(handle: string): Promise<ProductCollection | undefined> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user