Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
IanHollow committed May 13, 2024
1 parent 747fdb3 commit 482cccf
Show file tree
Hide file tree
Showing 16 changed files with 228 additions and 190 deletions.
11 changes: 1 addition & 10 deletions app/[dh_choice]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,11 @@ export default function Page({ searchParams }) {

return (
<main>

<div className="container mx-auto">
<h1 className="font-semibold py-5 text-4xl text-[#003C6C]">
{searchParams.name}
</h1>






{/* Search button */}
<div>
<button onClick={handleSearch}>Search</button>
Expand All @@ -185,10 +179,7 @@ export default function Page({ searchParams }) {
{/* Categories */}
{categories.map((category, i) => (
<div key={i}>
<Accordion
category={category}
isOpen={expandedCategory === i}
/>
<Accordion category={category} isOpen={expandedCategory === i} />
</div>
))}
</div>
Expand Down
1 change: 0 additions & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export default function RootLayout({
<body className={monst.className}>
<Navbar height={navbarHeight} />
<div className="" style={{ paddingTop: navbarHeight }}>

{children}
</div>
</body>
Expand Down
73 changes: 39 additions & 34 deletions app/loginPage/page.tsx
Original file line number Diff line number Diff line change
@@ -1,72 +1,74 @@
"use client";
import React, { useContext, useEffect, useState } from "react";
import { GoogleOAuthProvider, useGoogleLogin, googleLogout, TokenResponse} from "@react-oauth/google";
import {
GoogleOAuthProvider,
useGoogleLogin,
googleLogout,
TokenResponse,
} from "@react-oauth/google";
import { jwtDecode } from "jwt-decode";
import axios from "axios";




interface User {
name: string;
email: string;
picture: string;
}
const LoginPage = () => {
return(
<GoogleOAuthProvider clientId={'1040494859138-vji3ddfil5jancg23ifaginvmn71hktf.apps.googleusercontent.com'}>
<LoginComponent/>
return (
<GoogleOAuthProvider
clientId={
"1040494859138-vji3ddfil5jancg23ifaginvmn71hktf.apps.googleusercontent.com"
}
>
<LoginComponent />
</GoogleOAuthProvider>
)
}

);
};

const LoginComponent = () => {
const [user, setUser] = useState<User | null>(null);




useEffect(() => {
console.log("LoginPage component mounted");
}, []);


const handleLoginSuccess = (tokenResponse: any) => {
if ('code' in tokenResponse) {
if ("code" in tokenResponse) {
// Handle authorization code flow
console.log('Authorization Code:', tokenResponse.code);
console.log("Authorization Code:", tokenResponse.code);
// Exchange code for tokens here

// Store authentication token in the browser's local storage for navigation bar use
localStorage.setItem('token', tokenResponse.code)
localStorage.setItem("token", tokenResponse.code);
// Redirect the user to main page
window.location.href = '/';
window.location.href = "/";
} else {
// Handle implicit flow
console.log('Token Received:', tokenResponse.access_token);
console.log("Token Received:", tokenResponse.access_token);
const decoded: User = jwtDecode(tokenResponse.id_token);
setUser({
name: decoded.name,
email: decoded.email,
picture: decoded.picture
picture: decoded.picture,
});
// Send token to backend if necessary
axios.post('http://localhost:8000/myapi/users/google-oauth2/', { token: tokenResponse.access_token })
.then(res => console.log('Backend login successful', res))
.catch(err => console.error('Backend login failed', err));
axios
.post("http://localhost:8000/myapi/users/google-oauth2/", {
token: tokenResponse.access_token,
})
.then((res) => console.log("Backend login successful", res))
.catch((err) => console.error("Backend login failed", err));
}


};
const handleLogin = useGoogleLogin({
flow: "auth-code",

onSuccess: (tokenResponse) => {
handleLoginSuccess(tokenResponse);
console.log('Logged in successfully');
console.log("Logged in successfully");
},
onError: (errorResponse) => console.error('Login Failed', errorResponse),
onError: (errorResponse) => console.error("Login Failed", errorResponse),
});

// const handleLogout = () => {
Expand All @@ -75,13 +77,16 @@ const LoginComponent = () => {
// // Remove the token from local storage
// localStorage.removeItem('token');
// // Redirect the user to the login page
// window.location.href = '/loginPage';
// window.location.href = '/loginPage';
// console.log('Logged out successfully');
// };

return (
<div>
<button onClick={() => handleLogin()} className="hover:underline decoration-yellow-400 underline-offset-8 m-5 p-2 text-[#003C6C] font-medium text-xl">
<button
onClick={() => handleLogin()}
className="hover:underline decoration-yellow-400 underline-offset-8 m-5 p-2 text-[#003C6C] font-medium text-xl"
>
Login with Google
</button>
{/* <button onClick={handleLogout} className="hover:underline decoration-yellow-400 underline-offset-8 top-0 right-0 m-5 p-2 text-[#003C6C] font-medium text-xl">
Expand All @@ -90,13 +95,13 @@ const LoginComponent = () => {
{user && (
<div>
<img src={user.picture} alt="User profile" />
<h2>Welcome, {user.name} - {user.email}</h2>
<h2>
Welcome, {user.name} - {user.email}
</h2>
</div>
)}
</div>

);
};


export default LoginPage;
export default LoginPage;
22 changes: 16 additions & 6 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ interface Food {
extra_data: Array<string>;
}


interface subCategory {
name: string;
foods: Array<Food>;
Expand Down Expand Up @@ -68,7 +67,6 @@ export default function Home() {
.then((response) => {
const dhs: DiningHall[] = response.data["locations"];
setDhs(dhs);

})
.catch((error) => {
console.log(error);
Expand Down Expand Up @@ -137,13 +135,26 @@ export default function Home() {
<h2 className="font-medium text-2xl text-[#003C6C] flex items-center justify-center pb-5">
<ul className="flex flex-col md:p-0 md:flex-row md:border-0 ">
<li className="">
<button className="px-10 hover:underline decoration-yellow-400 underline-offset-8 decoration-4" >Dining Halls</button>
<button className="px-10 hover:underline decoration-yellow-400 underline-offset-8 decoration-4">
Dining Halls
</button>
</li>
<li>
<a href="#" className="px-10 hover:underline decoration-yellow-400 underline-offset-8 decoration-4" >Markets</a>
<a
href="#"
className="px-10 hover:underline decoration-yellow-400 underline-offset-8 decoration-4"
>
Markets
</a>
</li>
<li>
<a href="#" className="px-10 hover:underline decoration-yellow-400 underline-offset-8 decoration-4" >Cafes & Other</a>{/* pr-X dicates how far off right we want. */}
<a
href="#"
className="px-10 hover:underline decoration-yellow-400 underline-offset-8 decoration-4"
>
Cafes & Other
</a>
{/* pr-X dicates how far off right we want. */}
</li>
</ul>
</h2>
Expand All @@ -157,7 +168,6 @@ export default function Home() {
))}
</ul>
</h3>

</div>
</main>
);
Expand Down
20 changes: 11 additions & 9 deletions app/profile/page.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
'use client';
import React from 'react';
"use client";
import React from "react";
import { googleLogout } from "@react-oauth/google";


const Page = () => {
const handleLogout = () => {
googleLogout();
// Remove the token from local storage
localStorage.removeItem('token');
localStorage.removeItem("token");
// Redirect the user to the login page after logging out
window.location.href = '/loginPage';
console.log('Logged out successfully');
window.location.href = "/loginPage";
console.log("Logged out successfully");
};

return (
<div>
<h1>Profile</h1>
<button onClick={() => handleLogout()} className="hover:underline decoration-yellow-400 underline-offset-8 top-0 right-0 m-5 p-2 text-[#003C6C] font-medium text-xl">
Logout
<button
onClick={() => handleLogout()}
className="hover:underline decoration-yellow-400 underline-offset-8 top-0 right-0 m-5 p-2 text-[#003C6C] font-medium text-xl"
>
Logout
</button>
</div>
);
};

export default Page;
export default Page;
6 changes: 5 additions & 1 deletion backend/myapi/db_functions/food.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

## Basic CRUD


def set_food(name: str) -> None:
# check if food already exists
food = foods_collection.find_one({"food_name": name})
Expand All @@ -18,10 +19,12 @@ def set_food(name: str) -> None:
# add food
foods_collection.insert_one({"food_name": name, "ratings": {}})


def get_food(name: str) -> dict | None:
return foods_collection.find_one({"food_name": name})

def update_food(food_name: str, username: str, rating: int|None = None) -> None:

def update_food(food_name: str, username: str, rating: int | None = None) -> None:
# check if food exists
food = foods_collection.find_one({"food_name": food_name})
if not food:
Expand All @@ -35,5 +38,6 @@ def update_food(food_name: str, username: str, rating: int|None = None) -> None:
{"food_name": food_name}, {"$set": {"ratings": food["ratings"]}}
)


def delete_food(name: str) -> None:
foods_collection.delete_one({"food_name": name})
23 changes: 17 additions & 6 deletions backend/myapi/db_functions/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,56 @@
name: str
"""


## Basic CRUD
def set_location(location: dict) -> None:
locations_collection.insert_one(location)


def get_location(name: str) -> dict | None:
return locations_collection.find_one({"name": name})


def update_location(name: str, location: dict) -> None:
"""
check if the location exists then overwrite the location
check if the location exists then overwrite the location
"""
# check if the location exists
if get_location(name) is None:
set_location(location) # if not, create a new location
set_location(location) # if not, create a new location

# overwrite the location
locations_collection.update_one({"name": name}, {"$set": location})


def delete_location(name: str) -> None:
locations_collection.delete_one({"name": name})


## Bulk CRUD


def set_locations(locations: list[dict]) -> None:
locations_collection.insert_many(locations)


def get_locations(names: list[str] = []) -> list[dict]:
"""
if no names are given, return all locations
else, return the locations with the given names
if no names are given, return all locations
else, return the locations with the given names
"""
if len(names) == 0:
return list(locations_collection.find({})) # get all locations
return list(locations_collection.find({})) # get all locations

return list(
locations_collection.find({"name": {"$in": names}})
) # get specific locations

return list(locations_collection.find({"name": {"$in": names}})) # get specific locations

def update_locations(locations: list[dict]) -> None:
for location in locations:
update_location(location["name"], location)


def delete_locations(names: list[str]) -> None:
locations_collection.delete_many({"name": {"$in": names}})
Loading

0 comments on commit 482cccf

Please sign in to comment.