Skip to content

Commit 1aebf6e

Browse files
committed
fixed pr changes
1 parent 38c9297 commit 1aebf6e

File tree

5 files changed

+91
-105
lines changed

5 files changed

+91
-105
lines changed

src/components/Card.tsx

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
extractBracketContent,
1010
extractWithoutBracketContent,
1111
} from "@/util/utils";
12-
import axios from "axios";
12+
import { getSecureUrl, generateFileName, downloadFile } from "@/util/download";
1313
import Link from "next/link";
1414
import { cn } from "@/lib/utils";
1515

@@ -26,28 +26,6 @@ const Card = ({ paper, onSelect, isSelected }: CardProps) => {
2626
setChecked(isSelected);
2727
}, [isSelected]);
2828

29-
const getSecureUrl = (url: string): string =>
30-
url.startsWith("http://") ? url.replace("http://", "https://") : url;
31-
32-
const generateFileName = (paper: IPaper): string => {
33-
const extension = paper.final_url.split(".").pop();
34-
return `${extractBracketContent(paper.subject)}-${paper.exam}-${paper.slot}-${paper.year}.${extension}`;
35-
};
36-
37-
const downloadFile = async (url: string, filename: string): Promise<void> => {
38-
try {
39-
const response = await axios.get(url, { responseType: "blob" });
40-
const blob = new Blob([response.data]);
41-
const link = document.createElement("a");
42-
link.href = window.URL.createObjectURL(blob);
43-
link.download = filename;
44-
link.click();
45-
window.URL.revokeObjectURL(link.href);
46-
} catch (error) {
47-
console.error("Download failed:", error);
48-
}
49-
};
50-
5129
const handleDownload = async (paper: IPaper) => {
5230
await downloadFile(getSecureUrl(paper.final_url), generateFileName(paper));
5331
};

src/components/CatalogueContent.tsx

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import axios, { type AxiosError } from "axios";
66
import { Button } from "@/components/ui/button";
77
import { type IPaper, type Filters } from "@/interface";
88
import Card from "./Card";
9-
import { extractBracketContent } from "@/util/utils";
109
import { useRouter } from "next/navigation";
1110
import Loader from "./ui/loader";
1211
import SideBar from "../components/SideBar";
@@ -15,6 +14,7 @@ import { Filter } from "lucide-react";
1514
import { Sheet, SheetContent, SheetTrigger } from "./ui/sheet";
1615
import { Pin } from "lucide-react";
1716
import { StoredSubjects } from "@/interface";
17+
import { getSecureUrl, generateFileName, downloadFile } from "@/util/download";
1818

1919
const CatalogueContent = () => {
2020
const router = useRouter();
@@ -163,28 +163,6 @@ const CatalogueContent = () => {
163163
[],
164164
);
165165

166-
const getSecureUrl = (url: string): string =>
167-
url.startsWith("http://") ? url.replace("http://", "https://") : url;
168-
169-
const generateFileName = (paper: IPaper): string => {
170-
const extension = paper.final_url.split(".").pop();
171-
return `${extractBracketContent(paper.subject)}-${paper.exam}-${paper.slot}-${paper.year}.${extension}`;
172-
};
173-
174-
const downloadFile = async (url: string, filename: string): Promise<void> => {
175-
try {
176-
const response = await axios.get(url, { responseType: "blob" });
177-
const blob = new Blob([response.data]);
178-
const link = document.createElement("a");
179-
link.href = window.URL.createObjectURL(blob);
180-
link.download = filename;
181-
link.click();
182-
window.URL.revokeObjectURL(link.href);
183-
} catch (error) {
184-
console.error("Download failed:", error);
185-
}
186-
};
187-
188166
const handleDownloadAll = useCallback(async () => {
189167
const uniquePapers = Array.from(
190168
new Set(selectedPapers.map((paper) => paper._id)),

src/components/SideBar.tsx

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,65 @@ function SideBar({
7070
value: semester,
7171
})) ?? [];
7272

73+
const filtersForSidebar = [
74+
{
75+
label: "Exams",
76+
data: exams,
77+
selected: selectedExams,
78+
updater: (newVal: string[]) =>
79+
handleApplyFilters(
80+
newVal,
81+
selectedSlots,
82+
selectedYears,
83+
selectedCampuses,
84+
selectedSemesters,
85+
selectedAnswerKeyIncluded,
86+
),
87+
},
88+
{
89+
label: "Slots",
90+
data: slots,
91+
selected: selectedSlots,
92+
updater: (newVal: string[]) =>
93+
handleApplyFilters(
94+
selectedExams,
95+
newVal,
96+
selectedYears,
97+
selectedCampuses,
98+
selectedSemesters,
99+
selectedAnswerKeyIncluded,
100+
),
101+
},
102+
{
103+
label: "Years",
104+
data: years,
105+
selected: selectedYears,
106+
updater: (newVal: string[]) =>
107+
handleApplyFilters(
108+
selectedExams,
109+
selectedSlots,
110+
newVal,
111+
selectedCampuses,
112+
selectedSemesters,
113+
selectedAnswerKeyIncluded,
114+
),
115+
},
116+
{
117+
label: "Semesters",
118+
data: semesters,
119+
selected: selectedSemesters,
120+
updater: (newVal: string[]) =>
121+
handleApplyFilters(
122+
selectedExams,
123+
selectedSlots,
124+
selectedYears,
125+
selectedCampuses,
126+
newVal,
127+
selectedAnswerKeyIncluded,
128+
),
129+
},
130+
];
131+
73132
return (
74133
<div className="no-scrollbar fixed sticky top-0 h-[100vh] flex-col items-baseline overflow-y-auto border-r-2 border-[#36266d] bg-[#f3f5ff] pt-[10px] dark:bg-[#070114] md:flex">
75134
<div className="flex w-full items-center justify-between border-b-2 border-[#36266d] px-[10px] py-4">
@@ -134,64 +193,7 @@ function SideBar({
134193
</div>
135194

136195
{/* Filters */}
137-
{[
138-
{
139-
label: "Exams",
140-
data: exams,
141-
selected: selectedExams,
142-
updater: (newVal: string[]) =>
143-
handleApplyFilters(
144-
newVal,
145-
selectedSlots,
146-
selectedYears,
147-
selectedCampuses,
148-
selectedSemesters,
149-
selectedAnswerKeyIncluded,
150-
),
151-
},
152-
{
153-
label: "Slots",
154-
data: slots,
155-
selected: selectedSlots,
156-
updater: (newVal: string[]) =>
157-
handleApplyFilters(
158-
selectedExams,
159-
newVal,
160-
selectedYears,
161-
selectedCampuses,
162-
selectedSemesters,
163-
selectedAnswerKeyIncluded,
164-
),
165-
},
166-
{
167-
label: "Years",
168-
data: years,
169-
selected: selectedYears,
170-
updater: (newVal: string[]) =>
171-
handleApplyFilters(
172-
selectedExams,
173-
selectedSlots,
174-
newVal,
175-
selectedCampuses,
176-
selectedSemesters,
177-
selectedAnswerKeyIncluded,
178-
),
179-
},
180-
{
181-
label: "Semesters",
182-
data: semesters,
183-
selected: selectedSemesters,
184-
updater: (newVal: string[]) =>
185-
handleApplyFilters(
186-
selectedExams,
187-
selectedSlots,
188-
selectedYears,
189-
selectedCampuses,
190-
newVal,
191-
selectedAnswerKeyIncluded,
192-
),
193-
},
194-
].map((section) => (
196+
{filtersForSidebar.map((section) => (
195197
<div
196198
key={section.label}
197199
className="flex w-full flex-col items-baseline justify-between border-b-2 border-[#36266d] px-[10px]"

src/components/pdfViewer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useState, useRef, useCallback, useEffect } from "react";
66
import { Document, Page, pdfjs } from "react-pdf";
77
import { Download, ZoomIn, ZoomOut, Maximize2, Minimize2 } from "lucide-react";
88
import { Button } from "./ui/button";
9-
import { downloadFile } from "./CatalogueContent";
9+
import { downloadFile } from "@/util/download";
1010
import ShareButton from "./ShareButton";
1111
import Loader from "./ui/loader";
1212
import { FaGreaterThan, FaLessThan } from "react-icons/fa6";

src/util/download.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { IPaper } from "@/interface";
2+
import { extractBracketContent } from "./utils";
3+
import axios from "axios";
4+
5+
export const getSecureUrl = (url: string): string =>
6+
url.startsWith("http://") ? url.replace("http://", "https://") : url;
7+
8+
export const generateFileName = (paper: IPaper): string => {
9+
const extension = paper.final_url.split(".").pop();
10+
return `${extractBracketContent(paper.subject)}-${paper.exam}-${paper.slot}-${paper.year}.${extension}`;
11+
};
12+
13+
export const downloadFile = async (
14+
url: string,
15+
filename: string,
16+
): Promise<void> => {
17+
try {
18+
const response = await axios.get(url, { responseType: "blob" });
19+
const blob = new Blob([response.data]);
20+
const link = document.createElement("a");
21+
link.href = window.URL.createObjectURL(blob);
22+
link.download = filename;
23+
link.click();
24+
window.URL.revokeObjectURL(link.href);
25+
} catch (error) {
26+
console.error("Download failed:", error);
27+
}
28+
};

0 commit comments

Comments
 (0)