mirror of
https://github.com/vercel/commerce.git
synced 2025-05-20 16:36:59 +00:00
Merge pull request #6 from medusajs/feat/seb-feedback
feat: product sorting
This commit is contained in:
commit
834e6bdff3
@ -67,7 +67,7 @@ const reshapeCart = (cart: MedusaCart): Cart => {
|
|||||||
const lines = cart?.items?.map((item) => reshapeLineItem(item)) || [];
|
const lines = cart?.items?.map((item) => reshapeLineItem(item)) || [];
|
||||||
const totalQuantity = lines.length;
|
const totalQuantity = lines.length;
|
||||||
const checkoutUrl = '/';
|
const checkoutUrl = '/';
|
||||||
const currencyCode = cart.region?.currency_code || 'USD';
|
const currencyCode = cart.region?.currency_code.toUpperCase() || 'USD';
|
||||||
|
|
||||||
let subtotalAmount = '0';
|
let subtotalAmount = '0';
|
||||||
if (cart.subtotal && cart.region) {
|
if (cart.subtotal && cart.region) {
|
||||||
@ -115,6 +115,7 @@ const reshapeLineItem = (lineItem: MedusaLineItem): CartItem => {
|
|||||||
maxVariantPrice: calculateVariantAmount(lineItem.variant)
|
maxVariantPrice: calculateVariantAmount(lineItem.variant)
|
||||||
},
|
},
|
||||||
updatedAt: lineItem.updated_at,
|
updatedAt: lineItem.updated_at,
|
||||||
|
createdAt: lineItem.created_at,
|
||||||
tags: [],
|
tags: [],
|
||||||
descriptionHtml: lineItem.description ?? '',
|
descriptionHtml: lineItem.description ?? '',
|
||||||
featuredImage: {
|
featuredImage: {
|
||||||
@ -145,7 +146,7 @@ const reshapeLineItem = (lineItem: MedusaLineItem): CartItem => {
|
|||||||
lineItem.total,
|
lineItem.total,
|
||||||
lineItem.variant?.prices?.[0]?.currency_code
|
lineItem.variant?.prices?.[0]?.currency_code
|
||||||
).toString(),
|
).toString(),
|
||||||
currencyCode: lineItem.variant?.prices?.[0]?.currency_code || 'EUR'
|
currencyCode: lineItem.variant?.prices?.[0]?.currency_code.toUpperCase() || 'EUR'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const quantity = lineItem.quantity;
|
const quantity = lineItem.quantity;
|
||||||
@ -164,17 +165,18 @@ const reshapeProduct = (product: MedusaProduct): Product => {
|
|||||||
let amount = '0';
|
let amount = '0';
|
||||||
let currencyCode = 'USD';
|
let currencyCode = 'USD';
|
||||||
if (variant && variant.prices?.[0]?.amount) {
|
if (variant && variant.prices?.[0]?.amount) {
|
||||||
currencyCode = variant.prices?.[0]?.currency_code ?? 'USD';
|
currencyCode = variant.prices?.[0]?.currency_code.toUpperCase() ?? 'USD';
|
||||||
amount = convertToDecimal(variant.prices[0].amount, currencyCode).toString();
|
amount = convertToDecimal(variant.prices[0].amount, currencyCode).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
const priceRange = {
|
const priceRange = {
|
||||||
maxVariantPrice: {
|
maxVariantPrice: {
|
||||||
amount,
|
amount,
|
||||||
currencyCode: product.variants?.[0]?.prices?.[0]?.currency_code ?? ''
|
currencyCode: product.variants?.[0]?.prices?.[0]?.currency_code.toUpperCase() ?? ''
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const updatedAt = product.updated_at;
|
const updatedAt = product.updated_at;
|
||||||
|
const createdAt = product.created_at;
|
||||||
const tags = product.tags?.map((tag) => tag.value) || [];
|
const tags = product.tags?.map((tag) => tag.value) || [];
|
||||||
const descriptionHtml = product.description ?? '';
|
const descriptionHtml = product.description ?? '';
|
||||||
const featuredImage = {
|
const featuredImage = {
|
||||||
@ -194,6 +196,7 @@ const reshapeProduct = (product: MedusaProduct): Product => {
|
|||||||
featuredImage,
|
featuredImage,
|
||||||
priceRange,
|
priceRange,
|
||||||
updatedAt,
|
updatedAt,
|
||||||
|
createdAt,
|
||||||
tags,
|
tags,
|
||||||
descriptionHtml,
|
descriptionHtml,
|
||||||
availableForSale,
|
availableForSale,
|
||||||
@ -342,15 +345,30 @@ export async function getProduct(handle: string): Promise<Product> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getProducts({
|
export async function getProducts({
|
||||||
query
|
query = '',
|
||||||
|
reverse,
|
||||||
|
sortKey
|
||||||
}: {
|
}: {
|
||||||
query?: string;
|
query?: string;
|
||||||
reverse?: boolean;
|
reverse?: boolean;
|
||||||
sortKey?: string;
|
sortKey?: string;
|
||||||
}): Promise<Product[]> {
|
}): Promise<Product[]> {
|
||||||
const res = await medusaRequest('get', `/products?q=${query}&limit=20`);
|
const res = await medusaRequest('GET', `/products?q=${query}&limit=20`);
|
||||||
const products: Product[] = res.body.products.map((product: MedusaProduct) =>
|
let products: Product[] = res.body.products.map((product: MedusaProduct) =>
|
||||||
reshapeProduct(product)
|
reshapeProduct(product)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
sortKey === 'PRICE' &&
|
||||||
|
products.sort(
|
||||||
|
(a, b) =>
|
||||||
|
parseFloat(a.priceRange.maxVariantPrice.amount) -
|
||||||
|
parseFloat(b.priceRange.maxVariantPrice.amount)
|
||||||
|
);
|
||||||
|
|
||||||
|
sortKey === 'CREATED_AT' &&
|
||||||
|
products.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
||||||
|
|
||||||
|
reverse && products.reverse();
|
||||||
|
|
||||||
return products;
|
return products;
|
||||||
}
|
}
|
||||||
|
@ -62,6 +62,7 @@ export type Product = Partial<Omit<MedusaProduct, 'tags' | 'options' | 'variants
|
|||||||
priceRange: {
|
priceRange: {
|
||||||
maxVariantPrice: Money;
|
maxVariantPrice: Money;
|
||||||
};
|
};
|
||||||
|
createdAt: Date;
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
descriptionHtml: string;
|
descriptionHtml: string;
|
||||||
tags: Array<string>;
|
tags: Array<string>;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user