Skip to content

Commit 1874f70

Browse files
committed
fix: in reportTag files
1) usecontext instead of propdrilling paper information from pdfviewer till the very child element 2) separate reusable component for Label and input 3)disabled the button if user didn't change anything.instead of throwing an error.
1 parent ac5dac2 commit 1874f70

10 files changed

Lines changed: 287 additions & 205 deletions

File tree

src/app/api/report-tag/route.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ import { connectToDatabase } from "@/lib/database/mongoose";
33
import TagReport from "@/db/tagReport";
44
import { Ratelimit } from "@upstash/ratelimit";
55
import { redis } from "@/lib/utils/redis";
6+
import { exams } from "@/components/select_options";
67

7-
const ALLOWED_EXAMS = ["CAT-1", "CAT-2", "FAT"];
8+
interface ReportedFieldInput {
9+
field: string;
10+
value?: string;
11+
}
812
const ALLOWED_FIELDS = ["subject", "courseCode", "exam", "slot", "year"];
913

1014
const ratelimit = new Ratelimit({
@@ -13,7 +17,7 @@ const ratelimit = new Ratelimit({
1317
analytics: true,
1418
});
1519

16-
function getClientIp(req: any): string {
20+
function getClientIp(req: Request & { ip?: string}): string {
1721
return req.ip || "127.0.0.1";
1822
}
1923

@@ -49,12 +53,14 @@ export async function POST(req: Request & { ip?: string }) {
4953
{ status: 429 }
5054
);
5155
}
52-
const reportedFields = (body.reportedFields ?? [])
53-
.map((r: any) => ({
54-
field: String(r.field).trim(),
55-
value: r.value?.trim(),
56-
}))
57-
.filter((r: any) => r.field);
56+
const reportedFields: ReportedFieldInput[] = Array.isArray(body.reportedFields)
57+
? body.reportedFields
58+
.map((r:Partial<ReportedFieldInput>) => ({
59+
field: typeof r.field === "string" ? r.field.trim() : "",
60+
value: typeof r.value === "string" ? r.value.trim() : undefined,
61+
}))
62+
.filter((r:Partial<ReportedFieldInput>) => r.field)
63+
: [];
5864

5965
for (const rf of reportedFields) {
6066
if (!ALLOWED_FIELDS.includes(rf.field)) {
@@ -64,7 +70,7 @@ export async function POST(req: Request & { ip?: string }) {
6470
);
6571
}
6672
if (rf.field === "exam" && rf.value) {
67-
if (!ALLOWED_EXAMS.includes(rf.value)) {
73+
if (!exams.some(e => e.toLowerCase() === rf.value?.toLowerCase())) {
6874
return NextResponse.json(
6975
{ error: `Invalid exam value: ${rf.value}` },
7076
{ status: 400 }

src/app/paper/[id]/page.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { extractBracketContent } from "@/lib/utils/string";
77
import axios, { type AxiosResponse } from "axios";
88
import { type Metadata } from "next";
99
import { redirect } from "next/navigation";
10+
import { PaperProvider } from "@/context/PaperContext";
1011

1112
export async function generateMetadata({
1213
params,
@@ -164,15 +165,20 @@ const PaperPage = async ({ params }: { params: { id: string } }) => {
164165
</div>
165166
</h1>
166167
<center>
168+
<PaperProvider
169+
value={{
170+
paperId: params.id,
171+
subject: paper.subject,
172+
exam: paper.exam,
173+
slot: paper.slot,
174+
year: paper.year,
175+
}}
176+
>
167177
<PdfViewer
168178
url={paper.file_url}
169179
name={`${extractBracketContent(paper.subject)}-${paper.exam}-${paper.slot}-${paper.year}`}
170-
paperId={params.id}
171-
subject={paper.subject}
172-
exam={paper.exam}
173-
slot={paper.slot}
174-
year={paper.year}
175180
></PdfViewer>
181+
</PaperProvider>
176182
</center>
177183
<RelatedPapers />
178184
</>

src/components/ReportButton.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@ import { useState } from "react";
44
import { FaFlag } from "react-icons/fa6";
55
import { Button } from "./ui/button";
66
import ReportTagModal from "./ReportTagModal";
7+
import { usePaper } from "@/context/PaperContext";
78

8-
export default function ReportButton({
9-
paperId, subject, exam, slot, year
10-
}: {
11-
paperId: string;
12-
subject?: string;
13-
exam?: string;
14-
slot?: string;
15-
year?: string;
16-
}) {
9+
export default function ReportButton(){
10+
const { paperId, subject, exam, slot, year } = usePaper();
1711
const [open, setOpen] = useState(false);
18-
1912
return (
2013
<>
2114
<Button

0 commit comments

Comments
 (0)