Skip to content

Commit

Permalink
🚀 Username Profile Routing
Browse files Browse the repository at this point in the history
  • Loading branch information
ouckah committed Apr 17, 2024
1 parent 9fbf86d commit 770d235
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
5 changes: 1 addition & 4 deletions client/src/components/ProfilePicture.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { useEffect, useState } from 'react'
import { useParams } from 'react-router-dom'

import { HOST } from '../util/apiRoutes'
import { fetchWithAuth } from '../util/fetchUtils'

export const ProfilePicture = ({ profile, setPfp }) => {
const { id } = useParams()

const [fetchedPfp, setFetchedPfp] = useState(null)

const fetchPfp = async () => {
try {
const data = await fetchWithAuth({
url: `${HOST}/api/pfp/${id}`,
url: `${HOST}/api/pfp/${profile._id}`,
method: 'GET',
})

Expand Down
43 changes: 35 additions & 8 deletions client/src/pages/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ function Profile() {
const fetchProfile = async () => {
try {
setLoading(true)
const isValidId = await isMongoDBId(id)

if (!isValidId) {
setProfile(null)
throw new Error('Invalid MongoDB ID')
}

const data = await fetchWithAuth({
url: `${HOST}/api/profile/get/${id}`,
Expand All @@ -65,6 +59,31 @@ function Profile() {
}
}

const fetchProfileByUsername = async () => {
try {
setLoading(true)

const data = await fetchWithAuth({
url: `${HOST}/api/profile/getBy?username=${id}`,
method: 'GET',
})

if (data.school) fetchSchool(data.school)

// Successful fetch and data extraction
setProfile(data)
setUsername(data.username)
setLinkedin(extractLinkedinUsername(data.linkedin))
setLocation(data.location)
setPfp(data.pfp)
} catch (error) {
console.error('Error:', error.message)
setProfile(null)
} finally {
setLoading(false)
}
}

const fetchSchool = async (id) => {
try {
const data = await fetchWithAuth({
Expand Down Expand Up @@ -182,13 +201,21 @@ function Profile() {

useEffect(() => {
const fetchInfo = async () => {
await fetchProfile()
const isValidId = await isMongoDBId(id)

if (!isValidId.response) {
await fetchProfileByUsername()
} else {
await fetchProfile()
}
}

fetchInfo()
}, [id])

const admin = user && (user.profileId === id || user.username === id)
// establish if the user viewing the profile
// has admin privileges of the viewed profile
const admin = user && profile && user.profileId === profile._id

const currentExperienceInfo = // {'Incoming / Currently / Previously', Work Title, Company Name}
profile && profile.pipeline && profile.pipeline.length > 0
Expand Down
38 changes: 38 additions & 0 deletions server/controllers/profileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,43 @@ const getProfile = async (req, res) => {
res.status(200).json(profile);
};

// GET a single profile by username
const getProfileByUsername = async (req, res) => {
const { username } = req.query;

// Search for profile by username
const profile = await Profile.findOne({ username });

if (!profile) {
return res.status(404).json({ error: "No such Profile." });
}

// check if profile is anonymous
if (profile.anonymous) {
// Convert the Mongoose document to a plain JavaScript object
let anonymousProfile = profile.toObject();

// Use object destructuring to exclude certain properties
const { linkedin, pfp, location, lastName, ...rest } = anonymousProfile;

// Create a new profile object with the properties you want to retain and modify
anonymousProfile = {
...rest,
firstName: "Anonymous",

// set unwanted properties an empty string
linkedin: "",
pfp: "",
location: "",
lastName: "",
};

return res.status(200).json(anonymousProfile);
}

res.status(200).json(profile);
};

// GET a certain amount of random profiles
const getRandomProfiles = async (req, res) => {
try {
Expand Down Expand Up @@ -221,6 +258,7 @@ const updateProfile = async (req, res) => {
module.exports = {
getProfiles,
getProfile,
getProfileByUsername,
getRandomProfiles,
deleteProfile,
updateProfile,
Expand Down
4 changes: 4 additions & 0 deletions server/routes/profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require("express");
const {
getProfiles,
getProfile,
getProfileByUsername,
getRandomProfiles,
deleteProfile,
updateProfile,
Expand All @@ -16,6 +17,9 @@ read.get("/", getProfiles);
// GET a single profile
read.get("/get/:id", getProfile);

// GET a single profile by username
read.get("/getBy", getProfileByUsername);

// GET a certain amount of random profiles
read.get("/random", getRandomProfiles);

Expand Down

0 comments on commit 770d235

Please sign in to comment.