Skip to content

Commit

Permalink
Make related products random
Browse files Browse the repository at this point in the history
  • Loading branch information
rayc2045 committed May 14, 2024
1 parent a3fad61 commit 7cb929c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
18 changes: 16 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ const utils = {
getUniques(arr) {
return arr.filter((item, idx) => arr.indexOf(item) === idx);
},
getShuffles(arr) {
const array = arr.slice();
let currentIdx = array.length;
while (currentIdx !== 0) {
let randomIdx = Math.floor(Math.random() * currentIdx);
currentIdx--;
[array[currentIdx], array[randomIdx]] = [array[randomIdx], array[currentIdx]];
}
return array;
},
copyText(text) {
navigator.clipboard.writeText(text.trim());
},
Expand Down Expand Up @@ -426,9 +436,13 @@ const cart = {
async addItem(item) {
let isInCart = false;
for (const cartItem of this.items)
if (cartItem.name === item.name && cartItem.color === item.color) {
if (
cartItem.name === item.name &&
cartItem.category === item.category &&
cartItem.color === item.color
) {
isInCart = true;
cartItem.num++;
cartItem.num += item.num || 1;
break;
}
if (!isInCart) this.items.push({ ...item, num: 1 });
Expand Down
16 changes: 8 additions & 8 deletions src/pages/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ <h1 class="text-5xl md:text-6xl">
</a>
</section>

<section>
<h3 class="mb-12 text-xl text-center">New & Noteworthy</h3>
<ul class="grid place-content-center grid-cols-2 md:grid-cols-4 gap-8">
<section class="space-y-12">
<h3 class="text-xl text-center">New & Noteworthy</h3>
<ul class="grid place-content-center grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-8">
<template
x-for="product in shop.products.filter(p => p.label === 'new').slice(0, 4)"
x-for="product in getShuffles(shop.products.filter(p => p.label === 'new')).slice(0, 4)"
:key="product.name"
>
<li x-html="component('product-card')"></li>
Expand Down Expand Up @@ -88,11 +88,11 @@ <h3 class="mb-12 text-xl text-center">New & Noteworthy</h3>
</div>
</section>

<section>
<h3 class="mb-12 text-xl text-center">Handpicked this Week</h3>
<ul class="grid place-content-center grid-cols-2 md:grid-cols-4 gap-8">
<section class="space-y-12">
<h3 class="text-xl text-center">Handpicked this Week</h3>
<ul class="grid place-content-center grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-8">
<template
x-for="product in shop.products.filter(p => p.discount.length).slice(0, 4)"
x-for="product in getShuffles(shop.products.filter(p => p.discount.length)).slice(0, 4)"
:key="product.name"
>
<li x-html="component('product-card')"></li>
Expand Down

0 comments on commit 7cb929c

Please sign in to comment.