diff --git a/source/src/pages/Search.tsx b/source/src/pages/Search.tsx index c5881f9..140cf0c 100644 --- a/source/src/pages/Search.tsx +++ b/source/src/pages/Search.tsx @@ -91,9 +91,7 @@ const StyledReactTagsContainer = styled.div` ` const options: TagsData[] = [ - ...map.meta.tags.map((elem) => { - return { value: elem, label: elem } - }), + ...map.meta.tags.map((elem) => ({ value: elem, label: elem })), ] // check if post date is withing the range @@ -115,6 +113,22 @@ function isDateInRange( return Date.parse(from) < compareDate && compareDate < Date.parse(to) } +function isSelectedTagsInPost( + selectedTags: TagsData[] | null, + postTags: string[] | undefined +) { + if (selectedTags == null) return true + if (selectedTags.length == 0) return true + + if (postTags == undefined) return false + + // if tag is empty or null + const tagValues = selectedTags.map((value) => value.value) + if (!postTags.every((val) => tagValues.includes(val))) return false + + return true +} + // Search doesn't work on url change if component is not wrapped // todo: find ways to get rid of wrapper component and use class component export default function Search() { @@ -145,13 +159,11 @@ function _Search() { }, ] - const [dateRange, setDateRange] = useState>(defaultDateRange) const [postCards, setPostCards] = useState([]) + const [dateRange, setDateRange] = useState>(defaultDateRange) const [searchInput, setSearchInput] = useState(query.query) const [selectedTags, setSelectedOption] = useState( - query.tags.map((elem) => { - return { label: elem, value: elem } - }) + query.tags.map((elem) => ({ label: elem, value: elem })) ) function doSearch() { @@ -176,13 +188,10 @@ function _Search() { for (const res of searchIndex.search(searchInput)) { const postData = map.posts[res.ref] - // if post data exists, - // date is within range, - // and if post include tags if ( - postData && - isDateInRange(postData.date, query.from, query.to) && - true + postData && // if post data exists + isDateInRange(postData.date, query.from, query.to) && // date is within range + isSelectedTagsInPost(selectedTags, postData.tags) // if post include tags ) { _postCards.push( ) } - // apply search result - setPostCards(_postCards) } + // apply search result + setPostCards(_postCards) + // todo: set _postCards.length + // eslint-disable-next-line no-empty } catch (err) { console.error(err) @@ -205,7 +216,7 @@ function _Search() { useEffect(() => { doSearch() - }, [dateRange]) + }, [dateRange, selectedTags]) useEffect(() => { const delayDebounceFn = setTimeout(() => { @@ -287,64 +298,126 @@ function _Search() { doSearch() }} /> + {postCards.length} results

Filters

- {(theme) => ( + {(currentTheme) => (