This commit is contained in:
Kristian Duda 2024-06-28 21:24:12 +02:00
parent 86a083d72d
commit ba3a844f99
2 changed files with 13 additions and 11 deletions

View File

@ -75,9 +75,8 @@ export async function addToCart(
cartId: string,
lines: { merchandiseId: string; quantity: number }[]
): Promise<Cart> {
const prevCart = await payload.findByID<PayloadCart>('carts', cartId);
const prevCart = await payload.findByID<PayloadCart>('carts', cartId, { depth: 0 });
const cartItems = await mergeItems(prevCart.lines, lines, true);
const cart = await payload.update<PayloadCart>('carts', cartId, {
lines: cartItems
});
@ -85,7 +84,7 @@ export async function addToCart(
}
export async function removeFromCart(cartId: string, lineIds: string[]): Promise<Cart> {
const prevCart = await payload.findByID<PayloadCart>('carts', cartId);
const prevCart = await payload.findByID<PayloadCart>('carts', cartId, { depth: 0 });
const lines = prevCart?.lines?.filter((lineItem) => !lineIds.includes(lineItem.id!)) ?? [];
const cart = await payload.update<PayloadCart>('carts', cartId, { lines });
return reshapeCart(cart.doc);
@ -135,9 +134,8 @@ export async function updateCart(
cartId: string,
lines: { id: string; merchandiseId: string; quantity: number }[]
): Promise<Cart> {
const prevCart = await payload.findByID<PayloadCart>('carts', cartId);
const prevCart = await payload.findByID<PayloadCart>('carts', cartId, { depth: 0 });
const cartItems = await mergeItems(prevCart.lines, lines, false);
const cart = await payload.update<PayloadCart>('carts', cartId, { lines: cartItems });
return reshapeCart(cart.doc);
}
@ -267,7 +265,7 @@ export async function getCollectionProducts({
if (tag) {
filters.push({
tags: {
equals: collection
equals: tag
}
});
}

View File

@ -51,7 +51,11 @@ type Doc<T> = {
doc: T;
};
type FindParams = {
type BaseParams = {
depth?: number;
};
type FindParams = BaseParams & {
where?: Where;
depth?: number;
sort?: string;
@ -70,15 +74,15 @@ export class Payload {
this.baseUrl = baseUrl;
}
find = <T>(collection: Collection, params: FindParams) => {
find = <T>(collection: Collection, params: FindParams = {}) => {
const query = qs.stringify(params, { addQueryPrefix: true });
const url = `${this.baseUrl}/api/${collection}${query}`;
return ajax<PaginatedDocs<T>>('GET', url);
};
findByID = <T>(collection: Collection, id: string) => {
const url = `${this.baseUrl}/api/${collection}/${id}`;
findByID = <T>(collection: Collection, id: string, params: BaseParams = {}) => {
const query = qs.stringify(params, { addQueryPrefix: true });
const url = `${this.baseUrl}/api/${collection}/${id}${query}`;
return ajax<T>('GET', url);
};