Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/lopatoj/hackrice14
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen-Mikhailov committed Sep 22, 2024
2 parents a083500 + f06ef8c commit e1bb0a2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 30 deletions.
3 changes: 1 addition & 2 deletions client/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import ChatSelect from './pages/ChatSelect/ChatSelect'

// Import our custom CSS
import './scss/styles.scss'
import { getChat } from './modules/backend_functions.ts'

function App()
{
Expand All @@ -29,7 +28,7 @@ function App()
<Route path="/userinfo" element={<Userinfo/>} />
<Route path="/catalog" element={<Catalog/>} />
<Route path="/chat" element={<Chat/>} />
<Route path="/chat/:id" loader={({ params }) => getChat(params.id)} element={<Chat/>} />
<Route path="/chat/:id" element={<Chat/>} />
<Route path="/chatSelect" element={<ChatSelect/>} />
</Routes>
</div>
Expand Down
4 changes: 2 additions & 2 deletions client/src/modules/backend_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ async function setProfileBio(user: User, new_bio: string) {

async function getChat(chat_id?: string): Promise<Chat | null>
{
if (!chat_id)
if (!chat_id || !auth.currentUser)
return null

const starting_point = getStartingPoint();
const response = await fetch(`${starting_point}/chats/${chat_id}?id_token=${encodeURIComponent(await auth.currentUser!.getIdToken(true))}`)
const response = await fetch(`${starting_point}/chats/${chat_id}?id_token=${encodeURIComponent(await auth.currentUser.getIdToken(true))}`)
if (!response.ok)
return null

Expand Down
50 changes: 30 additions & 20 deletions client/src/pages/Chat/Chat.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import { useRef, useState } from "react"
import { useEffect, useRef, useState } from "react"
import "./Chat.css"
import { Chat as ChatType, Message } from "server/src/routers/chats";
import { auth } from "../../modules/firebase";
import { io } from "socket.io-client";
import { getStartingPoint } from "../../modules/backend_functions";
import { redirect, useLoaderData } from "react-router-dom";
import { useNavigate, useParams } from "react-router-dom";
import { getChat } from "../../modules/backend_functions";

function Chat()
{
const input = useRef<HTMLInputElement>(null);
const chat = useLoaderData() as ChatType | null;

if (!chat) {
redirect("/");
}

const [messages, setMessages] = useState<Message[]>(chat?.messages || []);

const socket = io(`${getStartingPoint()}/chats/socket.io`, {
auth: {
token: auth.currentUser?.getIdToken()
}
});
socket.connect();
socket.on("connect", async () => {
socket.emit("join", chat?._id);
});

const [chat, setChat] = useState<ChatType | null>(null);
const [messages, setMessages] = useState<Message[]>([]);
const id = useParams().id;
const nav = useNavigate();

useEffect(() => {
(async () => {
setChat(await getChat(id));
setMessages(chat?.messages || []);

if(!chat) {
nav("/");
return;
}

const socket = io(`${getStartingPoint()}/chats/socket.io`, {
auth: {
token: auth.currentUser?.getIdToken()
}
});
socket.connect();
socket.on("connect", async () => {
socket.emit("join", chat?._id);
});
})();
}, [id, chat?.messages]);

return <div>
<div>
<h1>Chat Page</h1>
Expand Down
4 changes: 2 additions & 2 deletions client/src/pages/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ function Home()
<h4>Crowdsourced Academic Encouragement to Combat Procrastination.</h4>
<br></br>
Designed
<span style={{color: "green"}}> for college students, </span>
<span style={{color: "#8271ff"}}> for college students, </span>
by college students.

<br></br>
With a matching algorithm powered by Generative AI,
Moti-Vibes
<span style={{color: "rgb(6, 115, 232)"}}> Moti-Vibes </span>
helps you
be held accountable for your terrible work ethic.

Expand Down
2 changes: 2 additions & 0 deletions server/src/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type Match = {

export type UserData = {
firebase_id: string,
open_to_wave: boolean,
matches: Match[],
bio: string,
skills: number[],
Expand All @@ -36,6 +37,7 @@ export const authMiddleware = async (req: Request<{}, {}, {}, { id_token: strin
if (!user) {
user = {
firebase_id: uid,
open_to_wave: true,
matches: [],
bio: "",
skills: []
Expand Down
8 changes: 4 additions & 4 deletions server/src/routers/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ profile.get("/me", (_req, res: Response<{}, { user: UserData }>) => {
res.json(res.locals.user);
});

profile.post("/me", async (req: Request<{}, {}, { bio: string }>, res: Response<{}, { user: UserData }>) => {
let { bio } = req.body;
profile.post("/me", async (req: Request<{}, {}, { bio: string, open_to_wave: boolean }>, res: Response<{}, { user: UserData }>) => {
let { bio, open_to_wave } = req.body;

console.log(req);

if (bio === undefined) {
if (bio === undefined || open_to_wave === undefined) {
res.status(400).send("Missing bio");
return;
}

await users.updateOne({ firebase_id: res.locals.user.firebase_id }, { "$set": { bio } });
await users.updateOne({ firebase_id: res.locals.user.firebase_id }, { "$set": { bio, open_to_wave } });
res.json({ ...res.locals.user, bio });
});

Expand Down

0 comments on commit e1bb0a2

Please sign in to comment.