From 55fac5186ffd95f27cc80cd8741e9bc8e837d672 Mon Sep 17 00:00:00 2001 From: Shoeb Ilyas Date: Mon, 12 Jun 2023 07:17:12 +0000 Subject: [PATCH 1/6] feat: search by location --- models/Profile.js | 3 +++ pages/search.js | 48 ++++++++++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/models/Profile.js b/models/Profile.js index 77100df6f5e..d97f25f5822 100644 --- a/models/Profile.js +++ b/models/Profile.js @@ -23,6 +23,9 @@ const ProfileSchema = new mongoose.Schema( lat: Number, lon: Number, updatedAt: Date, + city: String, + state: String, + country: String, }, views: { type: Number, diff --git a/pages/search.js b/pages/search.js index 8d4a0d7b77e..f2ab2ae02ee 100755 --- a/pages/search.js +++ b/pages/search.js @@ -20,7 +20,7 @@ export async function getStaticProps() { tags: [], }; try { - data.users = await getUsers({ cards: true }); + data.users = await getUsers({ cards: false }); } catch (e) { logger.error(e, "ERROR search users"); } @@ -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 @@ -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); @@ -85,19 +84,23 @@ 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 = Object.entries(user?.location) + .join(", ") + .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) ); }); @@ -123,7 +126,7 @@ export default function Search({ data: { users, tags, randUsers } }) { } const items = cleanedInput.split(","); - + if (cleanedInput.length) { if (searchTagNameInInput(inputValue, keyword)) { return setInputValue( @@ -139,20 +142,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 ( <> From a3dfb82545116b536ff886453ff33d1138aa9a37 Mon Sep 17 00:00:00 2001 From: Shoeb Ilyas Date: Mon, 12 Jun 2023 07:21:43 +0000 Subject: [PATCH 2/6] fix: pick only necessary location fields --- pages/search.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pages/search.js b/pages/search.js index f2ab2ae02ee..cac47539fca 100755 --- a/pages/search.js +++ b/pages/search.js @@ -1,5 +1,6 @@ import { useEffect, useState } from "react"; import { useRouter } from "next/router"; +import { pick } from "lodash"; import UserCard from "@components/user/UserCard"; import Alert from "@components/Alert"; @@ -84,7 +85,9 @@ 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 = Object.entries(user?.location) + const userLocationString = Object.entries( + pick(user.location, ["city", "state", "provided", "country"]) + ) .join(", ") .toLowerCase(); From c1ecb5e503b7c22c7d78b8536f0d4d43bd436638 Mon Sep 17 00:00:00 2001 From: Shoeb Ilyas Date: Wed, 14 Jun 2023 14:54:55 +0000 Subject: [PATCH 3/6] fix(schema): removed lodash, fixed schema --- models/Profile.js | 3 --- pages/search.js | 7 ++----- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/models/Profile.js b/models/Profile.js index d97f25f5822..77100df6f5e 100644 --- a/models/Profile.js +++ b/models/Profile.js @@ -23,9 +23,6 @@ const ProfileSchema = new mongoose.Schema( lat: Number, lon: Number, updatedAt: Date, - city: String, - state: String, - country: String, }, views: { type: Number, diff --git a/pages/search.js b/pages/search.js index cac47539fca..c11c382755a 100755 --- a/pages/search.js +++ b/pages/search.js @@ -1,6 +1,5 @@ import { useEffect, useState } from "react"; import { useRouter } from "next/router"; -import { pick } from "lodash"; import UserCard from "@components/user/UserCard"; import Alert from "@components/Alert"; @@ -21,7 +20,7 @@ export async function getStaticProps() { tags: [], }; try { - data.users = await getUsers({ cards: false }); + data.users = await getUsers({ }); } catch (e) { logger.error(e, "ERROR search users"); } @@ -85,9 +84,7 @@ 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 = Object.entries( - pick(user.location, ["city", "state", "provided", "country"]) - ) + const userLocationString = [user.location.provided] .join(", ") .toLowerCase(); From 5a8718ca9c89ccc1def8e51db284733f6fa807c5 Mon Sep 17 00:00:00 2001 From: Shoeb Ilyas Date: Thu, 15 Jun 2023 06:53:10 +0000 Subject: [PATCH 4/6] fix(location): handle undefined location in search --- pages/search.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pages/search.js b/pages/search.js index c11c382755a..54af6268838 100755 --- a/pages/search.js +++ b/pages/search.js @@ -84,9 +84,9 @@ 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.provided] + const userLocationString = user.location ? [user.location.provided] .join(", ") - .toLowerCase(); + .toLowerCase() : ''; // check if all search terms/keywords are matching with the the uses const isUserMatched = terms.every((term) => { From 01e71091cf3b272bf462d46d0c12168945a01c86 Mon Sep 17 00:00:00 2001 From: Shoeb Ilyas Date: Thu, 15 Jun 2023 06:55:43 +0000 Subject: [PATCH 5/6] fix(search): getusers() params --- pages/search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/search.js b/pages/search.js index 54af6268838..a5305d3a19f 100755 --- a/pages/search.js +++ b/pages/search.js @@ -20,7 +20,7 @@ export async function getStaticProps() { tags: [], }; try { - data.users = await getUsers({ }); + data.users = await getUsers(); } catch (e) { logger.error(e, "ERROR search users"); } From 5dc63d76015f3e49b199a4997e75e14b8456102c Mon Sep 17 00:00:00 2001 From: Shoeb Ilyas Date: Thu, 15 Jun 2023 14:20:46 +0000 Subject: [PATCH 6/6] fix: search location string --- pages/search.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pages/search.js b/pages/search.js index a5305d3a19f..c39180ecf54 100755 --- a/pages/search.js +++ b/pages/search.js @@ -84,9 +84,7 @@ 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] - .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) => {