Skip to content

Commit

Permalink
Add category styles (#18)
Browse files Browse the repository at this point in the history
* add classes to tabs and list

pass whole category to list component and update values accordingly

* add styles to page if any are found in category
  • Loading branch information
circlecube authored Oct 31, 2022
1 parent b882219 commit 7f15b51
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
27 changes: 24 additions & 3 deletions components/marketplace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import { default as MarketplaceIsLoading } from '../marketplaceIsLoading/';
}
}
setIsLoading( false );
applyStyles();
}
}, [ marketplaceCategories ] );

Expand All @@ -97,6 +98,7 @@ import { default as MarketplaceIsLoading } from '../marketplaceIsLoading/';
let thecategories = [];
categories.forEach((cat)=>{
cat.currentCount = constants.perPage;
cat.className = 'newfold-marketplace-tab-'+cat.name;

if ( cat.products_count > 0 ) {
thecategories.push(cat);
Expand All @@ -114,14 +116,33 @@ import { default as MarketplaceIsLoading } from '../marketplaceIsLoading/';
const saveCategoryDisplayCount = (categoryName, newCount) => {
let updatedMarketplaceCategories = [...marketplaceCategories];
// find matching cat, and update perPage amount
updatedMarketplaceCategories.forEach((cat)=>{
updatedMarketplaceCategories.forEach( (cat) => {
if (cat.name === categoryName ) {
cat.currentCount = newCount;
}
});
setMarketplaceCategories( updatedMarketplaceCategories );
};

/**
* Apply styles if they exist
*/
const applyStyles = () => {
if ( marketplaceCategories ) {
marketplaceCategories.forEach( (category) => {
if(
category.styles && // category has styles
!document.querySelector('[data-styleid="' + category.className + '"]') // not already added
) {
const style = document.createElement("style")
style.textContent = category.styles;
style.dataset.styleid = category.className;
document.head.appendChild(style);
}
});
}
};

/**
* render marketplace preloader
*
Expand All @@ -145,14 +166,14 @@ import { default as MarketplaceIsLoading } from '../marketplaceIsLoading/';
<Components.TabPanel
className="newfold-marketplace-tabs"
activeClass="current-tab"
orientation="vertical"
orientation="horizontal"
initialTabName={ initialTab }
onSelect={ onTabNavigate }
tabs={ marketplaceCategories }
>
{ ( tab ) => <MarketplaceList
marketplaceItems={marketplaceItems}
category={tab.title}
category={tab}
Components={Components}
methods={methods}
constants={constants}
Expand Down
6 changes: 3 additions & 3 deletions components/marketplaceList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const MarketplaceList = ({ marketplaceItems, currentCount, category, Components,
*/
const filterProductsByCategory = (items, category) => {
return items.filter((item) => {
return item.categories.includes( category );
return item.categories.includes( category.title );
});
};

Expand Down Expand Up @@ -65,11 +65,11 @@ const MarketplaceList = ({ marketplaceItems, currentCount, category, Components,
* this is so users don't need to load more every time they click back into a category
*/
methods.useEffect(() => {
saveCategoryDisplayCount( category, itemsCount );
saveCategoryDisplayCount( category.name, itemsCount );
}, [ itemsCount ] );

return (
<div className="marketplaceList">
<div className={ `marketplace-list marketplace-list-${ category.name }` }>
<div className="grid col2">
{ activeItems.length > 0 && activeItems.map((item) => (
<MarketplaceItem
Expand Down

0 comments on commit 7f15b51

Please sign in to comment.