Skip to content

Commit 5da17de

Browse files
author
advikgupta
committed
created users paper section
1 parent 1b54f01 commit 5da17de

File tree

4 files changed

+172
-3
lines changed

4 files changed

+172
-3
lines changed

src/app/api/upcoming-papers/route.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ export async function GET() {
1818
{ status: 404 },
1919
);
2020
}
21-
const nextSlot = String.fromCharCode(slot.charCodeAt(0) + 1)
22-
const correspondingSlots = [slot + "1", slot + "2", nextSlot + "1", nextSlot + "2"];
21+
const nextSlot = String.fromCharCode(slot.charCodeAt(0) + 1);
22+
const correspondingSlots = [
23+
slot + "1",
24+
slot + "2",
25+
nextSlot + "1",
26+
nextSlot + "2",
27+
];
2328
const selectedSubjects = await UpcomingSubject.find({
2429
slots: { $in: correspondingSlots },
2530
});

src/app/api/user-papers/route.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { NextResponse } from "next/server";
2+
import { connectToDatabase } from "@/lib/mongoose";
3+
import Paper from "@/db/papers";
4+
5+
export const dynamic = "force-dynamic";
6+
7+
export async function POST(req: Request) {
8+
try {
9+
await connectToDatabase();
10+
const body = await req.json();
11+
12+
const subjects: string[] = body;
13+
14+
const usersPapers = await Paper.find({
15+
subject: { $in: subjects },
16+
});
17+
18+
let transformedPapers = usersPapers.map((paper) => ({
19+
subject: paper.subject,
20+
slots: [paper.slot],
21+
}));
22+
23+
// check duplicates
24+
transformedPapers = Array.from(
25+
new Map(transformedPapers.map((item) => [item.subject, item])).values(),
26+
);
27+
28+
console.log("usersPapers", usersPapers);
29+
30+
return NextResponse.json(transformedPapers, {
31+
status: 200,
32+
});
33+
} catch (error) {
34+
console.error("Error fetching papers:", error);
35+
return NextResponse.json(
36+
{
37+
error: "Failed to fetch papers.",
38+
},
39+
{ status: 500 },
40+
);
41+
}
42+
}

src/components/UsersPapers.tsx

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"use client";
2+
3+
import { useEffect, useState } from "react";
4+
import axios from "axios";
5+
import { type IUpcomingPaper } from "@/interface";
6+
import Loader from "./ui/loader";
7+
import UpcomingPaper from "./UpcomingPaper";
8+
import {
9+
Carousel,
10+
CarouselContent,
11+
CarouselItem,
12+
CarouselNext,
13+
CarouselPrevious,
14+
} from "@/components/ui/carousel";
15+
import Autoplay from "embla-carousel-autoplay";
16+
import { chunkArray } from "@/util/utils";
17+
18+
function UsersPapers() {
19+
const [displayPapers, setDisplayPapers] = useState<IUpcomingPaper[]>([]);
20+
const [isLoading, setIsLoading] = useState(true);
21+
const [chunkSize, setChunkSize] = useState<number>(4);
22+
23+
useEffect(() => {
24+
const handleResize = () => {
25+
if (window.innerWidth < 640) {
26+
setChunkSize(4);
27+
} else {
28+
setChunkSize(8);
29+
}
30+
};
31+
32+
localStorage.setItem(
33+
"userSubjects",
34+
JSON.stringify([
35+
"Information Security [CBS3002]",
36+
"Foundations of Data Analytics [BCSE351E]",
37+
"Design and Analysis of Algorithms [MCSE502L]",
38+
"Complex Variables and Linear Algebra [BMAT201L]",
39+
"Differential Equations and Transforms [BMAT102L]",
40+
]),
41+
);
42+
43+
handleResize();
44+
window.addEventListener("resize", handleResize);
45+
46+
return () => {
47+
window.removeEventListener("resize", handleResize);
48+
};
49+
}, []);
50+
51+
const chunkedPapers = chunkArray(displayPapers, chunkSize);
52+
53+
useEffect(() => {
54+
async function fetchPapers() {
55+
try {
56+
const storedSubjects = JSON.parse(localStorage.getItem("userSubjects"));
57+
setIsLoading(true);
58+
const response = await axios.post("/api/user-papers", storedSubjects);
59+
setDisplayPapers(response.data);
60+
} catch (error) {
61+
console.error("Failed to fetch papers:", error);
62+
} finally {
63+
setIsLoading(false);
64+
}
65+
}
66+
67+
void fetchPapers();
68+
}, []);
69+
70+
if (isLoading) {
71+
return <Loader prop="m-10" />;
72+
}
73+
74+
const plugins = [Autoplay({ delay: 8000, stopOnInteraction: true })];
75+
76+
return (
77+
<div className="px-4">
78+
<p className="my-8 text-center font-play text-lg font-semibold">
79+
Users Papers
80+
</p>
81+
82+
<div className="">
83+
<Carousel
84+
opts={{
85+
align: "start",
86+
loop: true,
87+
}}
88+
plugins={plugins}
89+
className="w-full"
90+
>
91+
<div className="relative mt-4 flex justify-end gap-4">
92+
<CarouselPrevious className="relative" />
93+
<CarouselNext className="relative" />
94+
</div>
95+
<CarouselContent>
96+
{chunkedPapers.map((paperGroup, index) => {
97+
return (
98+
<CarouselItem
99+
key={`carousel-item-${index}`}
100+
className="grid grid-cols-2 grid-rows-2 gap-4 md:grid-cols-4 lg:auto-rows-fr"
101+
>
102+
{paperGroup.map((paper, subIndex) => (
103+
<div key={subIndex} className="h-full">
104+
<UpcomingPaper
105+
subject={paper.subject}
106+
slots={paper.slots}
107+
/>
108+
</div>
109+
))}
110+
</CarouselItem>
111+
);
112+
})}
113+
</CarouselContent>
114+
</Carousel>
115+
</div>
116+
</div>
117+
);
118+
}
119+
120+
export default UsersPapers;

src/components/screens/Hero.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import React from "react";
22
import SearchBar from "../Searchbar/searchbar";
33
import StoredPapers from "../StoredPapers";
4+
import UsersPapers from "../UsersPapers";
45

56
const Hero = () => {
67
return (
78
<div id="hero" className="flex flex-col justify-between">
8-
<h1 className="font-vipnabd mx-auto my-8 text-center text-3xl font-extrabold">
9+
<h1 className="mx-auto my-8 text-center font-vipnabd text-3xl font-extrabold">
910
Built by Students for Students
1011
</h1>
1112
<div className="px-6">
1213
<SearchBar />
1314
</div>
15+
<UsersPapers />
1416
<StoredPapers />
1517
{/* <div className="hidden lg:flex flex-col items-center whitespace-nowrap text-center">
1618
<h1 className="font-play text-md">Learn More</h1>

0 commit comments

Comments
 (0)