generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #533 from hlxsites/507-product-category-list-block
507 product category list block
- Loading branch information
Showing
6 changed files
with
186 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
a, div, | ||
} from '../../scripts/dom-builder.js'; | ||
import { toClassName } from '../../scripts/lib-franklin.js'; | ||
|
||
const createFilters = (items, activeTag, tagName) => { | ||
// collect tag filters | ||
const allTags = items.map((item) => item[tagName].replace(/,\s*/g, ',').split(',')); | ||
const filterTags = new Set([].concat(...allTags)); | ||
filterTags.delete(''); | ||
filterTags.delete('Blog'); // filter out generic blog tag | ||
filterTags.delete('News'); // filter out generic news tag | ||
// render tag cloud | ||
const newUrl = new URL(window.location); | ||
newUrl.searchParams.delete('tag'); | ||
newUrl.searchParams.delete('page'); | ||
const tags = div( | ||
{ class: 'flex flex-wrap gap-2 mt-10 mb-4' }, | ||
a( | ||
{ | ||
class: | ||
'text-center my-2 inline-block rounded-full px-4 py-1 text-sm font-bold bg-d text-danaherpurple-500 bg-danaherpurple-50 hover:bg-gray-100 hover:text-gray-500', | ||
href: newUrl.toString(), | ||
}, | ||
'View All', | ||
), | ||
); | ||
[...filterTags].sort().forEach((tag) => { | ||
newUrl.searchParams.set('tag', toClassName(tag).toLowerCase()); | ||
const tagAnchor = a( | ||
{ | ||
class: | ||
'text-center my-2 inline-block rounded-full px-4 py-1 text-sm font-bold bg-d hover:bg-gray-100 hover:text-gray-500', | ||
href: newUrl.toString(), | ||
}, | ||
tag, | ||
); | ||
if (toClassName(tag).toLowerCase() === activeTag) { | ||
tagAnchor.classList.add('bg-danaherpurple-500', 'text-white'); | ||
tagAnchor.setAttribute('aria-current', 'tag'); | ||
} else { | ||
tagAnchor.classList.add('text-danaherpurple-500', 'bg-danaherpurple-50'); | ||
} | ||
tags.append(tagAnchor); | ||
}); | ||
return tags; | ||
}; | ||
export default createFilters; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import ffetch from '../../scripts/ffetch.js'; | ||
import { | ||
ul, li, a, h4, span, | ||
} from '../../scripts/dom-builder.js'; | ||
import { makePublicUrl, imageHelper } from '../../scripts/scripts.js'; | ||
import { toClassName } from '../../scripts/lib-franklin.js'; | ||
import createFilters from './filter.js'; | ||
|
||
const getSelectionFromUrl = (field) => toClassName(new URLSearchParams(window.location.search).get(field)) || ''; | ||
|
||
function createCard(product, firstCard = false) { | ||
const cardWrapper = a( | ||
{ href: makePublicUrl(product.path), title: product.title }, | ||
imageHelper(product.image, product.title, firstCard), | ||
h4( | ||
{ | ||
class: '!text-sm !font-normal !text-danahergray-900 !break-words !h-16', | ||
}, | ||
product.title, | ||
span({ class: 'text-lg font-semibold text-danaherpurple-500' }, ' →'), | ||
), | ||
); | ||
const card = li({ | ||
class: | ||
'w-52 lg:w-40 h-52 flex flex-col col-span-1 relative mx-auto justify-center transform transition duration-500 border-l-0-5 border-black hover:scale-105 overflow-hidden bg-white max-w-xl pl-6 pr-6 lg:pr-0', | ||
}, cardWrapper); | ||
card.querySelector('img').className = 'mb-2 h-24 w-full object-cover'; | ||
return card; | ||
} | ||
|
||
export default async function decorate(block) { | ||
block.parentElement.parentElement.classList.add('!pb-0'); | ||
|
||
let products = await ffetch('/us/en/products-index.json') | ||
.filter(({ fullCategory }) => fullCategory.split('|').length === 1) | ||
.filter(({ fullCategory }) => fullCategory !== '') | ||
.filter(({ path }) => !path.includes('/topics')) | ||
.filter(({ path }) => !path.includes('/product-coveo')) | ||
.filter(({ path }) => !path.includes('/brands')) | ||
.all(); | ||
|
||
const activeTagFilter = getSelectionFromUrl('tag'); | ||
let filteredProducts = products; | ||
products = products.sort((item1, item2) => item1.title.localeCompare(item2.title)); | ||
if (activeTagFilter) { | ||
filteredProducts = products.filter( | ||
(item) => toClassName(item.brand).toLowerCase().indexOf(activeTagFilter) > -1, | ||
); | ||
} | ||
|
||
const cardList = ul({ | ||
class: | ||
'container grid max-w-7xl w-full mx-auto gap-6 grid-cols-1 sm:grid-cols-3 lg:grid-cols-6 px-4 py-4 sm:px-0 justify-items-center mt-3 mb-3', | ||
}); | ||
|
||
filteredProducts.forEach((product, index) => { | ||
cardList.append(createCard(product, index === 0)); | ||
}); | ||
const filterTags = createFilters(products, activeTagFilter, 'brand'); | ||
block.textContent = ''; | ||
block.append(filterTags, cardList); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters