i am facing issue while adding sort by feature in my nextjs project using algolia
useEffect(() => {
setLoading(true);
const index = algoliaClient.initIndex(ALGOLIA_INDEX);
const filters = choseFilters(cta_type);
if (category_Item_name) {
index
.search("", {
facetFilters: filters,
page: page,
hitsPerPage: 20,
facets:['*']
})
.then(({ hits,facets }) => {
if (page === 0) {
setSearchResults(hits);
const filteredFacets = Object.fromEntries(
Object.entries(facets).filter(([key, _]) => !['Category_en','Object_Type','Sub_Category_en','price','disabled','Sub_Category',selected_language === "ar"?'Brand_name_en':'Brand_name'].includes(key))
);
setFacets((prevFacets) => {
const updatedFacets = { ...prevFacets };
Object.keys(filteredFacets).forEach(key => {
updatedFacets[key] = { ...prevFacets[key],...filteredFacets[key] };
});
return updatedFacets;
});
} else {
setSearchResults((prevResults) => [...prevResults, ...hits]);
}
setLoading(false);
setAlgoliaLength(hits?.length);
})
.catch((err) => {
console.error("Search error:", err);
});
}
}, [category_Item_name, choseFilters, cta_type, page,selected_language]);
above code is called in GlobalContext
and i have created a custom drop down as shown in the image currently i am looking for price only
how can i achieve it ?
here is my dropDOwn CODE for sort
const FilterDropdown = () => {
const items = [
{
key: "Relevance",
label: "Relevance",
},
{
key: "High to Low",
label: "Price : High to Low",
},
{
key: "Low to High",
label: "Price : Low to High",
},
{
key: "Popularity",
label: "Popularity",
},
{
key: "Featured",
label: "Featured",
},
];
return (
<Dropdown >
<DropdownTrigger>
<Button
variant="bordered"
className='pRegular text-primaryGrey z-9'
>
Relevance <i className="fa-solid fa-angle-down"></i>
</Button>
</DropdownTrigger>
<DropdownMenu aria-label="Dynamic Actions" items={items}>
{(item) => (
<DropdownItem
key={item.key}
// color={item.key === "delete" ? "danger" : "default"}
// className={item.key === "delete" ? "text-danger" : ""}
>
{item.label}
</DropdownItem>
)}
</DropdownMenu>
</Dropdown>
)
}
export default FilterDropdown
i tried adding sortBy parameter this was not working
index
.search("", {
facetFilters: filters,
page: page,
hitsPerPage: 20,
facets:['*'],
sortBy:['price:asc']
})