Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions pages/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function getStaticProps() {
tags: [],
};
try {
data.users = await getUsers({ cards: true });
data.users = await getUsers();
} catch (e) {
logger.error(e, "ERROR search users");
}
Expand Down Expand Up @@ -55,7 +55,7 @@ export default function Search({ data: { users, tags, randUsers } }) {
setNotFound(username);
}
}, [username]);

useEffect(() => {
if (!inputValue) {
//Setting the users as null when the input field is empty
Expand All @@ -64,18 +64,17 @@ export default function Search({ data: { users, tags, randUsers } }) {
setNotFound();
return;
}

if (inputValue.length < 2) {
return;
}

const timer = setTimeout(() => {
filterData(inputValue);
}, 500);

return () => clearTimeout(timer);
}, [inputValue]);


const filterData = (value) => {
const cleanedInput = cleanSearchInput(value);
Expand All @@ -85,19 +84,21 @@ export default function Search({ data: { users, tags, randUsers } }) {
const nameLower = user.name.toLowerCase();
const usernameLower = user.username.toLowerCase();
const userTagsString = user.tags.join(", ").toLowerCase();

const userLocationString = user.location ? user.location.provided.toLowerCase() : "";

// check if all search terms/keywords are matching with the the uses
const isUserMatched = terms.every((term) => {
const cleanedTerm = term.trim();

if(!cleanedInput) {
if (!cleanedInput) {
return false;
};
}

return (
usernameLower.includes(cleanedTerm) ||
nameLower.includes(cleanedTerm) ||
userTagsString.includes(cleanedTerm)
usernameLower.includes(cleanedTerm) ||
nameLower.includes(cleanedTerm) ||
userTagsString.includes(cleanedTerm) ||
userLocationString.includes(cleanedTerm)
);
});

Expand All @@ -123,7 +124,7 @@ export default function Search({ data: { users, tags, randUsers } }) {
}

const items = cleanedInput.split(",");

if (cleanedInput.length) {
if (searchTagNameInInput(inputValue, keyword)) {
return setInputValue(
Expand All @@ -139,20 +140,23 @@ export default function Search({ data: { users, tags, randUsers } }) {

// removes leading/trailing whitespaces and extra spaces and converted to lowercase
const cleanSearchInput = (searchInput) => {
return searchInput.trim().replace(/\s{2,}/g, ' ').toLowerCase();
}
return searchInput
.trim()
.replace(/\s{2,}/g, " ")
.toLowerCase();
};

const searchTagNameInInput = (searchInput, tagName) => {
const tags = cleanSearchInput(searchInput).split(",");
for(let tag of tags) {
if(tag.trim() === tagName.toLowerCase()) {

for (let tag of tags) {
if (tag.trim() === tagName.toLowerCase()) {
return true;
};
}
}

return false;
}
};

return (
<>
Expand Down