| 1 |
import { memo, useEffect, useState } from "@wordpress/element"; |
| 2 |
import InspectorControl from "../../components/inspectorControls/inspectorControls"; |
| 3 |
import { DownArrow, panelBodyRightIcon, panelBodyTitleIcon } from "../../icons/icons"; |
| 4 |
|
| 5 |
import Inspector from "./inspect"; |
| 6 |
import classNames from "classnames"; |
| 7 |
|
| 8 |
const Render = ({ attributes, setAttributes, allTaxonomies }) => { |
| 9 |
const [dropdownOpen, setDropdown] = useState(false); |
| 10 |
const [selectTerm, setSelectTerm] = useState("All"); |
| 11 |
const [searchValue, setSearchValue] = useState(""); |
| 12 |
|
| 13 |
attributes.allTaxonomies = allTaxonomies; |
| 14 |
const { |
| 15 |
uniqueId, |
| 16 |
taxonomyType, |
| 17 |
selectTermsType, |
| 18 |
selectedTerms, |
| 19 |
excludeTerms, |
| 20 |
categoryLabel, |
| 21 |
overrideCategoryLabel, |
| 22 |
allTextLabel, |
| 23 |
showPostCount, |
| 24 |
searchInDropdown, |
| 25 |
filterType, |
| 26 |
searchPlaceholderText, |
| 27 |
titleGlobalTypography, |
| 28 |
optionGlobalTypography, |
| 29 |
} = attributes; |
| 30 |
|
| 31 |
const [activeBtn, setActiveBtn] = useState(allTextLabel); |
| 32 |
|
| 33 |
useEffect(() => { |
| 34 |
const clickOutSite = (e) => { |
| 35 |
const target = e.target.closest(".sp-smart-post-taxonomy-filter"); |
| 36 |
if (!target && dropdownOpen) { |
| 37 |
setDropdown(false); |
| 38 |
} |
| 39 |
}; |
| 40 |
window.addEventListener("click", clickOutSite); |
| 41 |
|
| 42 |
return () => window.removeEventListener("click", clickOutSite); |
| 43 |
}); |
| 44 |
|
| 45 |
let allTerms = []; |
| 46 |
if ("all" === selectTermsType) { |
| 47 |
allTaxonomies?.forEach((taxonomy) => { |
| 48 |
if (taxonomyType === taxonomy.name) { |
| 49 |
allTerms = [...allTerms, ...taxonomy.terms_items]; |
| 50 |
} |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
if ("exclude" === selectTermsType) { |
| 55 |
const excludeTermIds = excludeTerms.map((term) => term.value); |
| 56 |
allTaxonomies?.forEach((taxonomy) => { |
| 57 |
if (taxonomyType === taxonomy.name) { |
| 58 |
allTerms = [...allTerms, ...taxonomy.terms_items]; |
| 59 |
} |
| 60 |
}); |
| 61 |
|
| 62 |
allTerms = allTerms.filter((term) => !excludeTermIds.includes(term.value)); |
| 63 |
} |
| 64 |
|
| 65 |
if ("specific" === selectTermsType) { |
| 66 |
allTerms = selectedTerms; |
| 67 |
} |
| 68 |
|
| 69 |
const setTaxonomy = (e) => { |
| 70 |
const termValue = e.target.closest("a").dataset.value; |
| 71 |
|
| 72 |
setActiveBtn(termValue); |
| 73 |
setSelectTerm(termValue); |
| 74 |
setDropdown(false); |
| 75 |
}; |
| 76 |
|
| 77 |
useEffect(() => { |
| 78 |
setActiveBtn("all"); |
| 79 |
}, [taxonomyType, selectTermsType]); |
| 80 |
|
| 81 |
const termLabel = allTerms?.find((term) => term.value === selectTerm); |
| 82 |
|
| 83 |
if (searchValue) { |
| 84 |
allTerms = allTerms.filter((term) => term.label.toLowerCase().includes(searchValue.toLowerCase())); |
| 85 |
} |
| 86 |
|
| 87 |
return ( |
| 88 |
<> |
| 89 |
<InspectorControl |
| 90 |
TitleIcon={panelBodyTitleIcon} |
| 91 |
RightIcon={panelBodyRightIcon} |
| 92 |
Inspector={Inspector} |
| 93 |
attributes={attributes} |
| 94 |
setAttributes={setAttributes} |
| 95 |
/> |
| 96 |
|
| 97 |
<div id={uniqueId}> |
| 98 |
<div className="sp-smart-post-taxonomy-filter sp-smart-post-live-filter"> |
| 99 |
{categoryLabel && ( |
| 100 |
<span |
| 101 |
className={classNames( |
| 102 |
"sp-smart-post-live-filter-label", |
| 103 |
titleGlobalTypography?.class ? titleGlobalTypography.class : "" |
| 104 |
)} |
| 105 |
> |
| 106 |
{overrideCategoryLabel ? overrideCategoryLabel : taxonomyType.replaceAll("_", " ")} |
| 107 |
</span> |
| 108 |
)} |
| 109 |
{"dropdown" === filterType && ( |
| 110 |
<button |
| 111 |
onClick={() => setDropdown(!dropdownOpen)} |
| 112 |
className={classNames( |
| 113 |
"sp-smart-post-live-filter-btn", |
| 114 |
optionGlobalTypography?.class ? optionGlobalTypography.class : "" |
| 115 |
)} |
| 116 |
> |
| 117 |
<span>{termLabel?.label ? termLabel?.label : activeBtn}</span> <DownArrow /> |
| 118 |
</button> |
| 119 |
)} |
| 120 |
<ul className={`sp-smart-post-live-filter-${filterType} ${dropdownOpen ? "open" : ""}`}> |
| 121 |
{"dropdown" === filterType && searchInDropdown && ( |
| 122 |
<li key={"searchField"}> |
| 123 |
<div className="sp-dropdown-search-field"> |
| 124 |
<input |
| 125 |
type="text" |
| 126 |
name="sp-search-term-field" |
| 127 |
placeholder={searchPlaceholderText} |
| 128 |
onChange={(e) => setSearchValue(e.target.value)} |
| 129 |
/> |
| 130 |
</div> |
| 131 |
</li> |
| 132 |
)} |
| 133 |
<li> |
| 134 |
<a |
| 135 |
className={allTextLabel == activeBtn ? "active" : ""} |
| 136 |
href="#" |
| 137 |
onClick={setTaxonomy} |
| 138 |
data-value={allTextLabel} |
| 139 |
> |
| 140 |
{allTextLabel} |
| 141 |
</a> |
| 142 |
</li> |
| 143 |
{allTerms?.map((term, i) => ( |
| 144 |
<li key={term.value}> |
| 145 |
<a |
| 146 |
className={term.value == activeBtn ? "active" : ""} |
| 147 |
href="#" |
| 148 |
onClick={setTaxonomy} |
| 149 |
data-value={term.value} |
| 150 |
> |
| 151 |
{`${term.label} ${showPostCount ? `(${i + 1})` : ""}`} |
| 152 |
</a> |
| 153 |
</li> |
| 154 |
))} |
| 155 |
</ul> |
| 156 |
</div> |
| 157 |
</div> |
| 158 |
</> |
| 159 |
); |
| 160 |
}; |
| 161 |
|
| 162 |
export default memo(Render); |
| 163 |
|