Submit TagsChoosePopUp search handler

This commit is contained in:
Mateusz Tylka 2023-05-24 11:13:54 +02:00
parent 2c4c89ef80
commit 203d2f8cd0
2 changed files with 26 additions and 1 deletions

View File

@ -6,13 +6,16 @@ import theme from '../../../../utils/theme';
import Button from '../../../../components/generic/Button';
import TagsChoosePopUpStyle from './TagsChoosePopUpStyle';
import renderTagItems from './functions/renderTagItems';
import tagsSearchQueryHandler from './functions/tagsSearchQueryHandler';
const TagsChoosePopUp = (props) => {
const [tagsChoosed, setTagsChoosed] = React.useState([]);
const [tagsInit, setTagsInit] = React.useState([]);
const [tags, setTags] = React.useState([]);
React.useEffect(() => {
setTags(props.tags.slice());
setTagsInit(props.tags.slice());
setTagsChoosed(props.submissionTags.slice());
}, [props.tags, props.submissionTags]);
@ -41,7 +44,11 @@ const TagsChoosePopUp = (props) => {
closeHandler={() => props.setTagsPopUp(false)}
>
<TagsChoosePopUpStyle>
<Search />
<Search
searchQueryHandler={(e) =>
tagsSearchQueryHandler(e, tagsInit, tagsChoosed, setTags)
}
/>
<FlexColumn as="ul" className="TagsChoosePopUpStyle__tags-list">
{renderTagItems(
tagsChoosed,

View File

@ -0,0 +1,18 @@
const tagsSearchQueryHandler = (event, tagsInit, tagsChoosed, setTags) => {
let tagsToSearch = tagsInit;
for (let tagChoosen of tagsChoosed)
tagsToSearch = tagsToSearch.filter((tag) => tag.name !== tagChoosen.name);
const searchQuery = event.target.value;
let tagsToRender = [];
if (searchQuery === '') {
setTags(tagsToSearch);
} else {
for (let tag of tagsToSearch) {
if (tag.name.toLowerCase().includes(searchQuery.toLowerCase()))
tagsToRender.push(tag);
}
setTags(tagsToRender);
}
};
export default tagsSearchQueryHandler;