diff --git a/src/app/api/auth/register/route.ts b/src/app/api/auth/register/route.ts index 958f660..1111132 100644 --- a/src/app/api/auth/register/route.ts +++ b/src/app/api/auth/register/route.ts @@ -1,5 +1,6 @@ import { NextResponse } from "next/server"; import { MongoClient, ObjectId } from "mongodb"; +import { number } from "zod"; const client = new MongoClient(process.env.MONGODB_URI ?? ""); diff --git a/src/app/api/bosses/route.tsx b/src/app/api/bosses/route.tsx index d6523f2..e4cbb06 100644 --- a/src/app/api/bosses/route.tsx +++ b/src/app/api/bosses/route.tsx @@ -57,7 +57,7 @@ export async function PATCH(request: Request) { if (alreadySelected) { await userCollection.updateOne({ id: userId }, { $pull: { selectedBosses: id } }); } else { - await userCollection.updateOne({ id: userId }, { $addToSet: { selectedBosses: id } }); + await userCollection.updateOne({ id: userId }, { $addToSet: { selectedBosses: { id } } }); } return NextResponse.json({ message: "Updated successfully" }); diff --git a/src/app/api/selectedBosses/route.tsx b/src/app/api/selectedBosses/route.tsx new file mode 100644 index 0000000..a44dbf9 --- /dev/null +++ b/src/app/api/selectedBosses/route.tsx @@ -0,0 +1,66 @@ +import { NextResponse } from "next/server"; +import { MongoClient, ObjectId } from "mongodb"; +import { getServerSession } from "next-auth"; +import { authOptions } from "@/lib/authOptions"; + +const client = new MongoClient(process.env.MONGODB_URI ?? ""); +// const isValidObjectId = (id: string) => /^[a-fA-F0-9]{24}$/.test(id); + +export async function GET() { + const session = await getServerSession(authOptions); + const userId = (session?.user as any).id; + + try { + await client.connect(); + const db = client.db("elden-ring"); + const userCollection = db.collection("users"); + + const user = await userCollection.findOne({ id: userId }); + + if (!user) { + return NextResponse.json({ message: "User not found" }, { status: 404 }); + } + + const selectedBosses = user.selectedBosses; + + return NextResponse.json(selectedBosses); + } catch (error) { + console.error("Error processing GET request:", error); + return NextResponse.error(); + } finally { + await client.close(); + } +} + +// export async function PATCH(request: Request) { +// const { id } = await request.json(); +// const session = await getServerSession(authOptions); + +// const userId = (session?.user as any).id; + +// if (!isValidObjectId(id) || !userId) { +// return NextResponse.json({ message: "Invalid ID format" }, { status: 400 }); +// } + +// try { +// await client.connect(); +// const db = client.db("elden-ring"); +// const userCollection = db.collection("users"); + +// const user = await userCollection.findOne({ id: userId }); +// const alreadySelected = user?.selectedBosses.includes(id); + +// if (alreadySelected) { +// await userCollection.updateOne({ id: userId }, { $pull: { selectedBosses: id } }); +// } else { +// await userCollection.updateOne({ id: userId }, { $addToSet: { selectedBosses: id } }); +// } + +// return NextResponse.json({ message: "Updated successfully" }); +// } catch (error) { +// console.error("Error processing PATCH request:", error); +// return NextResponse.error(); +// } finally { +// await client.close(); +// } +// } diff --git a/src/app/bosses/page.tsx b/src/app/bosses/page.tsx index baae98c..472314a 100644 --- a/src/app/bosses/page.tsx +++ b/src/app/bosses/page.tsx @@ -12,6 +12,7 @@ import { useEffect, useState } from "react"; import { cn } from "@/lib/utils"; import axios from "axios"; import { useSession } from "next-auth/react"; +import { redirect } from "next/navigation"; export default function BossesPage() { const baseURL = process.env.NEXT_PUBLIC_BASE_URL; @@ -21,11 +22,12 @@ export default function BossesPage() { const userId = session && session.user ? (session?.user as any).id : null; const [data, setData] = useState([]); + const [selectedBosses, setSelectedBosses] = useState([]); const [isSelected, setIsSelected] = useState<{ [key: string]: any }>({}); const [open, setOpen] = useState(false); const [value, setValue] = useState(""); const [loading, setLoading] = useState(true); - const dataLocations = data.map((boss: any) => boss.location); + const dataLocations = data.map((boss: any) => boss.region); const filteredLocations = (a: any[]) => { var seen: { [key: string]: boolean } = {}; @@ -34,6 +36,24 @@ export default function BossesPage() { }); }; + const findMatchingBosses = (dataArray: any[], selectedArray: any[]) => { + return dataArray.filter((boss) => selectedArray.some((selectedBoss) => selectedBoss.id === boss.id)); + }; + + useEffect(() => { + if (data.length > 0 && selectedBosses.length > 0) { + const matchingBosses = findMatchingBosses(data, selectedBosses); + console.log(matchingBosses); + matchingBosses.forEach((boss) => { + console.log(`Boss found: ${boss.name}`); + }); + } + }, [data, selectedBosses]); + + if (!session) { + redirect("/login"); + } + const locationsForFilter = filteredLocations(dataLocations); const fetchData = async () => { @@ -52,6 +72,20 @@ export default function BossesPage() { } }; + const fetchSelectedBosses = async () => { + try { + const response = await axios.get(`${currentEnv}/api/selectedBosses`, { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${userId}`, + }, + }); + setSelectedBosses(response.data); + } catch (error) { + console.error("Error fetching selected bosses:", error); + } + }; + const toggleSelected = async (id: string, name: string) => { if (!session || !session.user) { toast.error("You need to be logged in to select a boss"); @@ -103,7 +137,7 @@ export default function BossesPage() { const showFilteredData = () => { if (value) { - return data.filter((boss: any) => boss.location === value); + return data.filter((boss: any) => boss.region === value); } else { return data; } @@ -111,6 +145,7 @@ export default function BossesPage() { useEffect(() => { fetchData(); + fetchSelectedBosses(); }, []); return ( @@ -121,7 +156,7 @@ export default function BossesPage() { @@ -131,16 +166,16 @@ export default function BossesPage() { No location found. - {locationsForFilter.map((location) => ( + {locationsForFilter.map((region) => ( { setValue(currentValue === value ? "" : currentValue); setOpen(false); }}> - - {location} + + {region} ))} @@ -151,8 +186,7 @@ export default function BossesPage() { )}
{loading - ? // Display skeleton loaders when loading - Array.from({ length: 8 }).map((_, index) => ( + ? Array.from({ length: 8 }).map((_, index) => (
diff --git a/src/components/loginForm.tsx b/src/components/loginForm.tsx index 1bc93c2..cbece46 100644 --- a/src/components/loginForm.tsx +++ b/src/components/loginForm.tsx @@ -92,12 +92,12 @@ export default function LoginForm() {
- +
); diff --git a/src/components/navbar.tsx b/src/components/navbar.tsx index 7612a6c..22b4d06 100644 --- a/src/components/navbar.tsx +++ b/src/components/navbar.tsx @@ -15,6 +15,10 @@ import { signOut, useSession } from "next-auth/react"; export default function Navbar({ className, ...props }: React.HTMLAttributes) { const { data: session } = useSession(); + // function classNames(...classes: string[]) { + // return classes.filter(Boolean).join(" ") + // } + const dynamicRoute = !session && "/bosses" ? "/register" : "/bosses"; const pathname = usePathname(); const [selectedLink, setSelectedLink] = useState(null); @@ -72,7 +76,6 @@ export default function Navbar({ className, ...props }: React.HTMLAttributesSettings - signOut()}>Log out ) : ( @@ -83,18 +86,32 @@ export default function Navbar({ className, ...props }: React.HTMLAttributesHome Bosses - - - - + {session ? ( - + signOut()} href="./register"> + + - + ) : ( + + + + + + + + + + + + + )} diff --git a/src/components/user-auth-form.tsx b/src/components/user-auth-form.tsx index 1c432e8..022f256 100644 --- a/src/components/user-auth-form.tsx +++ b/src/components/user-auth-form.tsx @@ -36,12 +36,12 @@ export function UserAuthForm({ className, ...props }: UserAuthFormProps) {
- +
); diff --git a/src/components/user-nav.tsx b/src/components/user-nav.tsx index ac2fd1e..c01a912 100644 --- a/src/components/user-nav.tsx +++ b/src/components/user-nav.tsx @@ -38,10 +38,10 @@ export function UserNav() { ) : ( <> - -