Skip to content

Commit

Permalink
Merge pull request #3 from FetchCV/dev
Browse files Browse the repository at this point in the history
User search in global nav bar
  • Loading branch information
hnasheralneam authored Jul 22, 2024
2 parents acd2571 + 6d0a981 commit 4b174d1
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 345 deletions.
37 changes: 30 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -120,7 +116,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 });
});
Expand Down Expand Up @@ -176,7 +178,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); });
9 changes: 1 addition & 8 deletions public/home.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,13 @@
FetchCV, you can show off all your projects and
achievements in one place!
</p>
<p>Pro tip: you can search people by GitHub username, even if they don't have an account!</p>
</div>
</div>
<br />
<!-- Get started -->
<div class="block text-center">
<h2 class="text-2xl p-4 font-semibold text-zinc-900 dark:text-zinc-200">Get started!</h2>
<!-- GitHub username profile -->
<a href="/gh-username-search">
<div
class="inline-flex justify-center transition-all p-2 mx-1 shadow-lg bg-zinc-200 dark:bg-zinc-800 border-[1px] border-zinc-300 dark:border-zinc-700 border-t-zinc-100 dark:border-t-zinc-600 rounded-md dark:text-white">
<span class="material-symbols-rounded text-public mr-2">search</span>
<p>GitHub username search</p>
</div>
</a>
<!-- Profile Link -->
<a href="/profile">
<div
Expand Down
29 changes: 0 additions & 29 deletions public/pages/gh-username-search.ejs

This file was deleted.

16 changes: 0 additions & 16 deletions public/pages/github-profile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,10 @@
<%- include("../partials/navbar.ejs") %>
<body class="px-8 flex justify-center bg-zinc-50 dark:bg-zinc-900 dark:text-white">
<div class="mb-20">
<!-- top bar -->
<div class="flex my-2">
<!-- User search -->
<div
class="flex w-full justify-center items-center shadow-lg bg-zinc-100 dark:bg-zinc-800 border-[1px] border-zinc-300 dark:border-zinc-700 border-t-zinc-100 dark:border-t-zinc-600 rounded-md">
<input class="px-6 py-2 text-base bg-transparent w-[100%]" type="text" id="username"
placeholder="Enter a GitHub username">
<span tabindex="0" class="material-symbols-rounded text-public mr-2"
onclick="newUsername()">person_search</span>
</div>
<script>
let usernameJSON = `<%- JSON.stringify(username) %>`;
let username = JSON.parse(usernameJSON);
</script>
</div>
<!-- Error box - hidden by default -->
<div
class="error-box shadow-2xl py-4 px-8 my-2 border-[1px] border-zinc-300 dark:border-zinc-700 border-t-zinc-100 dark:border-t-zinc-600 rounded-md hidden bg-blue-800 bg-red-800 bg-green-800">
Expand All @@ -37,10 +26,6 @@
</div>


<!-- this should be working -->
<div id="root"></div>


<!-- Profile Card -->
<div
class="shadow-2xl py-4 px-8 my-2 bg-zinc-100 dark:bg-zinc-800 border-[1px] border-zinc-300 dark:border-zinc-700 border-t-zinc-100 dark:border-t-zinc-600 rounded-md hidden user-info">
Expand Down Expand Up @@ -91,7 +76,6 @@
<script src="/scripts/displayErrors.js"></script>
<script src="/scripts/github/main.js"></script>
<script src="/scripts/github/getToken.js"></script>
<script src="/scripts/userProfile.js"></script>
<script src="/scripts/github/fetchData.js"></script>
<script src="/scripts/github/displayData.js"></script>
</html>
21 changes: 0 additions & 21 deletions public/pages/user-profile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,10 @@
<%- include("../partials/navbar.ejs") %>
<body class="px-8 flex justify-center bg-zinc-50 dark:bg-zinc-900 dark:text-white">
<div class="mb-20">
<!-- top bar -->
<div class="flex my-2">
<a href="/">
<div
class="inline-flex justify-center transition-all p-2 mr-2 shadow-lg bg-zinc-100 dark:bg-zinc-800 border-[1px] border-zinc-300 dark:border-zinc-700 border-t-zinc-100 dark:border-t-zinc-600 hover:bg-zinc-200 dark:hover:bg-zinc-700 rounded-md dark:text-white">
<span class="material-symbols-rounded text-public">home</span>
</div>
</a>
<!-- User search (will be replaced when accounts are added) -->
<div
class="flex w-full justify-center items-center shadow-lg bg-zinc-100 dark:bg-zinc-800 border-[1px] border-zinc-300 dark:border-zinc-700 border-t-zinc-100 dark:border-t-zinc-600 rounded-md">
<input class="px-6 py-2 text-base bg-transparent w-[100%]" type="text" id="username"
placeholder="Enter a GitHub username">
<span tabindex="0" class="material-symbols-rounded text-public mr-2"
onclick="newUsername()">person_search</span>
</div>
<script>
let usernameJSON = `<%- JSON.stringify(username) %>`;
let username = JSON.parse(usernameJSON);
</script>
</div>
<!-- Error box - hidden by default -->
<div
class="error-box shadow-2xl py-4 px-8 my-2 border-[1px] border-zinc-300 dark:border-zinc-700 border-t-zinc-100 dark:border-t-zinc-600 rounded-md hidden bg-blue-800 bg-red-800 bg-green-800">
Expand All @@ -43,9 +26,6 @@
</div>


<!-- this should be working -->
<div id="root"></div>


<!-- Profile Card -->
<div
Expand Down Expand Up @@ -111,7 +91,6 @@
<script src="/scripts/github/fetchData.js"></script>
<script src="/scripts/fetchData.js"></script>
<script src="/scripts/github/displayData.js"></script>
<script src="/scripts/userProfile.js"></script>
<script src="/scripts/showEditProfile.js"></script>

</html>
11 changes: 6 additions & 5 deletions public/partials/navbar.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
<h2 class="inline-block font-bold text-xl dark:text-white transition-all p-2 mx-2 hover:bg-zinc-200 dark:hover:bg-zinc-700 rounded-md hover:cursor-pointer"
style="font-family: 'Inter Tight';" title="home">FetchCV</h2>
</a>
<div class="inline-flex w-full justify-center items-center shadow-lg bg-zinc-100 dark:bg-zinc-800 border-[1px] border-zinc-300 dark:border-zinc-700 border-t-zinc-100 dark:border-t-zinc-600 rounded-md">
<input class="w-full px-6 py-2 text-base bg-transparent" type="text" id="username"
placeholder="Enter a GitHub username">
<span tabindex="0" class="material-symbols-rounded text-public mr-2" onclick="newUsername()">person_search</span>
<div class="relative inline-flex w-full justify-center items-center shadow-lg bg-zinc-100 dark:bg-zinc-800 border-[1px] border-zinc-300 dark:border-zinc-700 border-t-zinc-100 dark:border-t-zinc-600 rounded-md">
<input class="w-full px-6 py-2 text-base bg-transparent" type="text" id="user-name"
placeholder="Enter a username">
<span tabindex="0" class="material-symbols-rounded text-public mr-2" onclick="getUpdatedSearchResults(this.parent.firstChild.value)">person_search</span>
<div class="search-results hidden absolute top-[4rem] left-0 right-0 px-4 py-2 backdrop-blur-lg bg-zinc-100 dark:bg-zinc-800 rounded-lg border-2 border-zinc-200 dark:border-zinc-700" style="box-shadow: 0 .5rem 1rem #22222260;"></div>
</div>
<a href="/profile">
<div class="float-right inline-flex justify-center p-2 my-0.5 mx-2 hover:bg-zinc-200 dark:hover:bg-zinc-700 rounded-md">
Expand All @@ -31,4 +32,4 @@
</a>
</div>

<script src="scripts/github/usernameSearch.js"></script>
<script src="/scripts/userSearch.js"></script>
20 changes: 10 additions & 10 deletions public/scripts-src/github/getId.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
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);
}
}
24 changes: 0 additions & 24 deletions public/scripts-src/github/usernameSearch.ts

This file was deleted.

27 changes: 0 additions & 27 deletions public/scripts-src/userProfile.ts

This file was deleted.

78 changes: 78 additions & 0 deletions public/scripts-src/userSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// @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) {
showElement(searchResultsElement);
getUpdatedSearchResults(usernameElement.value);
}
else {
hideElement(searchResultsElement);
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];
let userElement = document.createElement("div");
createSearchResultElement(userElement, user.handle, user.profile.description);
let divider = document.createElement("hr");
divider.classList.add("dark:border-zinc-700");
userElement.append(divider);
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 = `
<div class="bg-white dark:bg-zinc-900 rounded-lg my-2 px-4 py-2">
<a href="/user/${name}">
<p class="text-lg">${name}</p>
<p class="font-light">${description}</p>
</a>
</div>
`;
}

function showElement(element) {
if (element.classList.contains("hidden")) {
element.classList.remove("hidden");
}
}

function hideElement(element) {
if (!element.classList.contains("hidden")) {
element.classList.add("hidden");
}
}
Loading

0 comments on commit 4b174d1

Please sign in to comment.