| 1 |
import { __ } from "@wordpress/i18n"; |
| 2 |
import { BlockTypeIcon } from "../../icons/icons"; |
| 3 |
|
| 4 |
const FilterSidebar = ( props ) => { |
| 5 |
const { |
| 6 |
changeStates, |
| 7 |
fieldValue, |
| 8 |
fieldOptions, |
| 9 |
allData, |
| 10 |
tabName, |
| 11 |
} = props; |
| 12 |
|
| 13 |
const filterCount = allData?.reduce( (acc, item) => { |
| 14 |
if ( tabName === "ready-pages" ) { |
| 15 |
item.pages?.forEach((page) => { |
| 16 |
const type = page.type; |
| 17 |
if (type) { |
| 18 |
acc[type] = (acc[type] || 0) + 1; |
| 19 |
} |
| 20 |
}); |
| 21 |
} else { |
| 22 |
const category = item.category; |
| 23 |
acc[category] = (acc[category] || 0) + 1; |
| 24 |
} |
| 25 |
return acc; |
| 26 |
}, {}); |
| 27 |
|
| 28 |
// Calculate Free/Pro counts for Pattern Types |
| 29 |
const freeProCount = allData?.reduce( |
| 30 |
(acc, item) => { |
| 31 |
acc.all = (acc.all || 0) + 1; |
| 32 |
if (item.pro) { |
| 33 |
acc.pro = (acc.pro || 0) + 1; |
| 34 |
} else { |
| 35 |
acc.free = (acc.free || 0) + 1; |
| 36 |
} |
| 37 |
return acc; |
| 38 |
}, |
| 39 |
{ all: 0, free: 0, pro: 0 } |
| 40 |
); |
| 41 |
|
| 42 |
const blockTypeObject = { |
| 43 |
"ready-patterns": __("Block Types", "post-carousel"), |
| 44 |
"starter-sites": __("Category Types", "post-carousel"), |
| 45 |
"ready-pages": __("Pages Types", "post-carousel"), |
| 46 |
} |
| 47 |
|
| 48 |
return ( |
| 49 |
<div className="sp-smart-template-filter-category"> |
| 50 |
<h3 className="sp-smart-template-library-sidebar-header Pattern-Types"> |
| 51 |
Pattern Types |
| 52 |
</h3> |
| 53 |
<div className="sp-smart-post-patter-type"> |
| 54 |
{["all", "free", "pro"].map((option, index) => ( |
| 55 |
<label |
| 56 |
key={index} |
| 57 |
htmlFor={`free-pro-option-${index}`} |
| 58 |
className={`components-radio-control__option ${option === fieldValue?.freePro ? "sp-active" : ""}`} |
| 59 |
> |
| 60 |
<input |
| 61 |
type="radio" |
| 62 |
id={`free-pro-option-${index}`} |
| 63 |
name="sp-smart-free-pro-option" |
| 64 |
className="components-radio-control__input" |
| 65 |
value={option} |
| 66 |
checked={option === fieldValue?.freePro} |
| 67 |
onChange={() => changeStates("freePro", option)} |
| 68 |
/> |
| 69 |
<span className="components-radio-control__label"> |
| 70 |
{option === "all" && "All"} |
| 71 |
{option === "free" && "Free"} |
| 72 |
{option === "pro" && "Pro"} |
| 73 |
<span> |
| 74 |
{option === "all" && `(${freeProCount?.all ?? 0})`} |
| 75 |
{option === "free" && `(${freeProCount?.free ?? 0})`} |
| 76 |
{option === "pro" && `(${freeProCount?.pro ?? 0})`} |
| 77 |
</span> |
| 78 |
</span> |
| 79 |
</label> |
| 80 |
))} |
| 81 |
</div> |
| 82 |
<h3 className="sp-smart-template-library-sidebar-header Block-Types"> |
| 83 |
<BlockTypeIcon /> { blockTypeObject[tabName] } |
| 84 |
</h3> |
| 85 |
{ fieldOptions?.filterArr?.length > 0 && ( |
| 86 |
<> |
| 87 |
<div className="sp-smart-sidebar-category-filter"> |
| 88 |
{ fieldOptions?.filterArr?.map( (option, index) => { |
| 89 |
const allOptionCount = tabName === "ready-pages" ? fieldOptions?.filterArr?.length : allData?.length; |
| 90 |
const optionCount = filterCount[ option?.value ] |
| 91 |
|
| 92 |
return ( |
| 93 |
<label |
| 94 |
key={ index } |
| 95 |
htmlFor={`filter-option-${index}`} |
| 96 |
className={`sp-smart-sidebar-option ${option?.value === fieldValue?.filter ? "sp-active" : ""}`} |
| 97 |
> |
| 98 |
<input |
| 99 |
type="radio" |
| 100 |
id={`filter-option-${index}`} |
| 101 |
name="sp-smart-category-option" |
| 102 |
className="sp-smart-category-filter" |
| 103 |
value={option?.value} |
| 104 |
checked={ option?.value === fieldValue?.filter } |
| 105 |
onChange={() => changeStates("filter", option?.value)} |
| 106 |
/> |
| 107 |
<span className="sp-smart-radio-label"> |
| 108 |
{option?.label} |
| 109 |
{ option?.value === "all" && ( <span>({allOptionCount})</span> ) } |
| 110 |
{ (option?.value !== "all" && filterCount[ option?.value ]) && ( <span>({optionCount})</span> ) } |
| 111 |
</span> |
| 112 |
</label> |
| 113 |
); |
| 114 |
})} |
| 115 |
</div> |
| 116 |
</> |
| 117 |
) } |
| 118 |
</div> |
| 119 |
); |
| 120 |
} |
| 121 |
export default FilterSidebar; |