Skip to content

Commit

Permalink
Add shop.filter
Browse files Browse the repository at this point in the history
  • Loading branch information
rayc2045 committed May 3, 2024
1 parent 45fc966 commit 72183c6
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,38 @@ const shop = {
? this.products.filter(product => product.name.toLowerCase().includes(this.searchText))
: this.products;
},
get categories() {
return utils.getUniques(this.products.map(product => product.category))
get filter() {
const filter = this.products.reduce(
(accumulator, product) => {
if (product.label && !accumulator.labels.includes(product.label))
accumulator.labels.push(product.label);
if (!accumulator.categories.includes(product.category))
accumulator.categories.push(product.category);
accumulator.price.min = Math.min(accumulator.price.min, product.price);
accumulator.price.max = Math.max(accumulator.price.max, product.price);
if (product.discount && !accumulator.discounts.includes(product.discount))
accumulator.discounts.push(product.discount);
product.colors.forEach(color => {
if (!accumulator.colors.includes(color)) accumulator.colors.push(color);
});
if (!accumulator.stocks.includes(product.stock))
accumulator.stocks.push(product.stock);
return accumulator;
},
{
labels: [],
categories: [],
price: {
min: Infinity,
max: 0,
},
discounts: [],
colors: [],
stocks: [],
}
);
filter.discounts.sort((a, b) => parseFloat(b.split('%')[0]) - parseFloat(a.split('%')[0]))
return filter;
},
async loadProducts() {
this.products = await utils.getData('./src/data/products.json');
Expand Down

0 comments on commit 72183c6

Please sign in to comment.