-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: removed unused code and moved search filter to custom hook
- Loading branch information
1 parent
c04c9ea
commit e5883e0
Showing
4 changed files
with
40 additions
and
244 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
import { useState, useEffect } from 'react' | ||
import { filterItems } from '../utils/filterItems' | ||
|
||
// Custom hook to manage filter state | ||
export const useFilterState = (data, searchQuery) => { | ||
const [filteredData, setFilteredData] = useState([]) | ||
|
||
useEffect(() => { | ||
const searchFields = ['title', 'description'] // Fields to search for courses | ||
const filtered = filterItems(data, searchQuery, searchFields) | ||
setFilteredData(filtered) | ||
}, [data, searchQuery]) | ||
|
||
return { | ||
filteredData, | ||
} | ||
} |
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,20 @@ | ||
// Utility function to filter items based on search query | ||
export const filterItems = (items, searchQuery, searchFields = []) => { | ||
return items.filter((item) => { | ||
return searchFields.some((field) => { | ||
let content = '' | ||
if (field === 'title' && item.metadata) { | ||
// Get all titles from the metadata | ||
const titles = Object.values(item.metadata) | ||
.filter((meta) => meta?.title) | ||
.map((titleObj) => titleObj.title) | ||
// Check if any of the titles includes the search query | ||
return titles.some((title) => title.toLowerCase().includes(searchQuery.toLowerCase())) | ||
} else { | ||
content = item[field] | ||
} | ||
|
||
return content && content.toLowerCase().includes(searchQuery.toLowerCase()) | ||
}) | ||
}) | ||
} |