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

View File

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