From 66df115031c4d75c438b50f75e532bb745ae7dde Mon Sep 17 00:00:00 2001 From: Hamza Date: Mon, 22 Jul 2024 10:57:30 -0400 Subject: [PATCH 1/2] Working on username search --- app.js | 33 ++++++++++- public/pages/user-profile.ejs | 6 -- public/partials/navbar.ejs | 16 +++--- public/scripts-src/github/getId.ts | 20 +++---- public/scripts-src/github/usernameSearch.ts | 2 +- public/scripts-src/userSearch.ts | 62 +++++++++++++++++++++ public/scripts/github/usernameSearch.js | 2 +- public/scripts/userSearch.js | 55 ++++++++++++++++++ public/styles/tailwind.css | 10 ++++ 9 files changed, 178 insertions(+), 28 deletions(-) create mode 100644 public/scripts-src/userSearch.ts create mode 100644 public/scripts/userSearch.js diff --git a/app.js b/app.js index 392eeda..9036573 100644 --- a/app.js +++ b/app.js @@ -14,12 +14,12 @@ mongoose.connect( const userSchema = new mongoose.Schema({ githubId: String, - profile: Object /* + profile: Object, /* { description: something // will check if exists, if not use github } */ - + handle: String }); const User = mongoose.model("User", userSchema); @@ -120,7 +120,13 @@ async function githubOAuthLogin(req, res) { } function createGithubOAuthUser(githubId, req, res) { - const user = new User({ githubId: githubId }); + const user = new User({ + githubId: githubId, + handle: req.session.user.login, + profile: { + description: req.session.user.bio + } + }); user.save().then((result) => { res.render("pages/welcome", { userData: req.session.user }); }); @@ -176,7 +182,28 @@ app.post("/edit/description", (req, res) => { }); }); +// Search +app.get("/search/user/:username", (req, res) => { + const username = req.params.username; + const results = 15; + User.find({ handle: { $regex: "^" + username } }) + .then((users) => {// should only send name and descr + res.json({ users: users.slice(0, results) }); + }) + .catch((err) => { + console.log(err); + }); +}); + // Connect app app.listen(PORT, () => { console.log("Server is running on port " + PORT); }); + + +// add new filed to existing schema members - remember to add to schema as well! +// User.updateMany( +// { handle: { $exists: false } }, +// { $set: { handle: "hnasheralneam" } }, +// { multi: true } +// ).then((oth) => { console.log(oth); }).catch((err) => { console.error("err-", err); }); \ No newline at end of file diff --git a/public/pages/user-profile.ejs b/public/pages/user-profile.ejs index b717afd..2b669ed 100644 --- a/public/pages/user-profile.ejs +++ b/public/pages/user-profile.ejs @@ -16,12 +16,6 @@
- -
- home -
-
diff --git a/public/partials/navbar.ejs b/public/partials/navbar.ejs index bf434d8..526d455 100644 --- a/public/partials/navbar.ejs +++ b/public/partials/navbar.ejs @@ -13,16 +13,17 @@ -
+

FetchCV

-
- - person_search -
+ - \ No newline at end of file + + \ No newline at end of file diff --git a/public/scripts-src/github/getId.ts b/public/scripts-src/github/getId.ts index 123398d..52ab578 100644 --- a/public/scripts-src/github/getId.ts +++ b/public/scripts-src/github/getId.ts @@ -1,11 +1,11 @@ async function getId() { - try { - const response = await fetch(`https://api.github.com/users/${username}`, header); - if (response.ok) { - return (await response.json()).id; - } - } - catch (error) { - console.error("Failed to fetch github id. Error - ", error); - } - } \ No newline at end of file + try { + const response = await fetch(`https://api.github.com/users/${username}`, header); + if (response.ok) { + return (await response.json()).id; + } + } + catch (error) { + console.error("Failed to fetch github id. Error - ", error); + } +} \ No newline at end of file diff --git a/public/scripts-src/github/usernameSearch.ts b/public/scripts-src/github/usernameSearch.ts index d046098..9d2394a 100644 --- a/public/scripts-src/github/usernameSearch.ts +++ b/public/scripts-src/github/usernameSearch.ts @@ -16,7 +16,7 @@ function newUsername() { const usernameInput = document.getElementById("username"); if (usernameInput) { - usernameInput.addEventListener("keydown", function (event) { + usernameInput.addEventListener("keydown", (event) => { if (event.key == "Enter") { newUsername(); } diff --git a/public/scripts-src/userSearch.ts b/public/scripts-src/userSearch.ts new file mode 100644 index 0000000..fec63ee --- /dev/null +++ b/public/scripts-src/userSearch.ts @@ -0,0 +1,62 @@ +// @ts-nocheck + +//=============================================== +// Username search +//=============================================== + +let usernameElement = document.getElementById("user-name") as HTMLInputElement; + +const usernameInput = document.getElementById("user-name"); +if (usernameInput) { + usernameInput.addEventListener("keyup", (event) => { + if (usernameElement.value.length > 0) { + getUpdatedSearchResults(usernameElement.value); + } + else { + showUpdatedSearchResults([]); + } + }); +} + +async function getUpdatedSearchResults(search) { + try { + const response = await fetch(`/search/user/${search.replace("/", "slash")}`); + if (response.ok) { + let data = await response.json(); + showUpdatedSearchResults(data.users); + } + } + catch (error) { + console.error("Error fetching users - ", error); // should show to user + } +} + +let searchResultsElement = document.querySelector(".search-results"); + +function showUpdatedSearchResults(users) { + searchResultsElement.innerHTML = ""; + for (let i = 0; i < users.length; i++) { + let user = users[i]; + console.log(user); + let userElement = document.createElement("div"); + createSearchResultElement(userElement, user.handle, user.profile.description); + searchResultsElement.append(userElement); + } + // Should only show if github username does exist + if (usernameElement.value.length > 0) { + let userElement = document.createElement("div"); + createSearchResultElement(userElement, "github/" + usernameElement.value, "No results? Try with their GitHub username"); + searchResultsElement.append(userElement); + } +} + +function createSearchResultElement(element, name, description) { + element.innerHTML = ` + + `; +} \ No newline at end of file diff --git a/public/scripts/github/usernameSearch.js b/public/scripts/github/usernameSearch.js index fd249a3..88b0a00 100644 --- a/public/scripts/github/usernameSearch.js +++ b/public/scripts/github/usernameSearch.js @@ -13,7 +13,7 @@ function newUsername() { } const usernameInput = document.getElementById("username"); if (usernameInput) { - usernameInput.addEventListener("keydown", function (event) { + usernameInput.addEventListener("keydown", (event) => { if (event.key == "Enter") { newUsername(); } diff --git a/public/scripts/userSearch.js b/public/scripts/userSearch.js new file mode 100644 index 0000000..4e53533 --- /dev/null +++ b/public/scripts/userSearch.js @@ -0,0 +1,55 @@ +// @ts-nocheck +//=============================================== +// Username search +//=============================================== +let usernameElement = document.getElementById("user-name"); +const usernameInput = document.getElementById("user-name"); +if (usernameInput) { + usernameInput.addEventListener("keyup", (event) => { + if (usernameElement.value.length > 0) { + getUpdatedSearchResults(usernameElement.value); + } + else { + showUpdatedSearchResults([]); + } + }); +} +async function getUpdatedSearchResults(search) { + try { + const response = await fetch(`/search/user/${search.replace("/", "slash")}`); + if (response.ok) { + let data = await response.json(); + showUpdatedSearchResults(data.users); + } + } + catch (error) { + console.error("Error fetching users - ", error); // should show to user + } +} +let searchResultsElement = document.querySelector(".search-results"); +function showUpdatedSearchResults(users) { + searchResultsElement.innerHTML = ""; + for (let i = 0; i < users.length; i++) { + let user = users[i]; + console.log(user); + let userElement = document.createElement("div"); + createSearchResultElement(userElement, user.handle, user.profile.description); + searchResultsElement.append(userElement); + } + // Should only show if github username does exist + if (usernameElement.value.length > 0) { + let userElement = document.createElement("div"); + createSearchResultElement(userElement, "github/" + usernameElement.value, "No results? Try with their GitHub username"); + searchResultsElement.append(userElement); + } +} +function createSearchResultElement(element, name, description) { + element.innerHTML = ` + + `; +} diff --git a/public/styles/tailwind.css b/public/styles/tailwind.css index a71cd29..63f4a18 100644 --- a/public/styles/tailwind.css +++ b/public/styles/tailwind.css @@ -907,6 +907,16 @@ video { background-color: rgb(244 244 245 / 0.85); } +.bg-red-50 { + --tw-bg-opacity: 1; + background-color: rgb(254 242 242 / var(--tw-bg-opacity)); +} + +.bg-red-500 { + --tw-bg-opacity: 1; + background-color: rgb(239 68 68 / var(--tw-bg-opacity)); +} + .bg-none { background-image: none; } From 6d0a981bfdf9b5bbe4eba83746db5d9d111b996a Mon Sep 17 00:00:00 2001 From: Hamza Date: Mon, 22 Jul 2024 11:41:09 -0400 Subject: [PATCH 2/2] User search in global nav --- app.js | 4 - public/home.ejs | 9 +- public/pages/gh-username-search.ejs | 29 --- public/pages/github-profile.ejs | 16 -- public/pages/user-profile.ejs | 15 -- public/partials/navbar.ejs | 11 +- public/scripts-src/github/usernameSearch.ts | 24 --- public/scripts-src/userProfile.ts | 27 --- public/scripts-src/userSearch.ts | 26 ++- public/scripts/github/usernameSearch.js | 21 -- public/scripts/userProfile.js | 22 --- public/scripts/userSearch.js | 22 ++- public/styles/tailwind.css | 205 ++++---------------- tailwind.config.js | 2 +- 14 files changed, 87 insertions(+), 346 deletions(-) delete mode 100644 public/pages/gh-username-search.ejs delete mode 100644 public/scripts-src/github/usernameSearch.ts delete mode 100644 public/scripts-src/userProfile.ts delete mode 100644 public/scripts/github/usernameSearch.js delete mode 100644 public/scripts/userProfile.js diff --git a/app.js b/app.js index 9036573..153bbc7 100644 --- a/app.js +++ b/app.js @@ -47,10 +47,6 @@ app.get("/", (req, res) => { res.render("home"); }); -app.get("/gh-username-search", (req, res) => { - res.render("pages/gh-username-search"); -}); - app.get("/profile", (req, res) => { if (!req.session.user) { return res.render("pages/login", { client_id: process.env.GITHUB_CLIENT_ID }); diff --git a/public/home.ejs b/public/home.ejs index 76c7303..32dd0fb 100644 --- a/public/home.ejs +++ b/public/home.ejs @@ -34,20 +34,13 @@ FetchCV, you can show off all your projects and achievements in one place!

+

Pro tip: you can search people by GitHub username, even if they don't have an account!


Get started!

- - -
- search -

GitHub username search

-
-
- - - - - - - - - GitHub Username Search | FetchCV - -<%- include("../partials/navbar.ejs") %> - -
- -
- -
- - person_search -
-
-
- - - diff --git a/public/pages/github-profile.ejs b/public/pages/github-profile.ejs index 704fb9b..2948a40 100644 --- a/public/pages/github-profile.ejs +++ b/public/pages/github-profile.ejs @@ -14,21 +14,10 @@ <%- include("../partials/navbar.ejs") %>
- -
- -
- - person_search -
-
- -
- -