Skip to content

Commit 68dc7ab

Browse files
committed
fixed merge conflicts
2 parents 8243acd + 438f17e commit 68dc7ab

4 files changed

Lines changed: 103 additions & 11 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"google-auth-library": "^10.2.1",
4040
"googleapis": "^155.0.0",
4141
"jose": "^5.10.0",
42+
"jszip": "^3.10.1",
4243
"lodash": "^4.17.21",
4344
"lucide-react": "^0.395.0",
4445
"mongoose": "^8.13.2",

pnpm-lock.yaml

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/CatalogueContent.tsx

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222
downloadFile,
2323
} from "@/util/download_paper";
2424
import type { ICourses } from "@/interface";
25+
import JSZip from "jszip";
26+
import { toast } from "react-hot-toast";
2527
import { useCourses } from "@/context/courseContext";
2628

2729
const CatalogueContent = () => {
@@ -205,17 +207,36 @@ const CatalogueContent = () => {
205207
[],
206208
);
207209

208-
const handleDownloadAll = useCallback(async () => {
210+
const handleDownloadSelected = useCallback(async () => {
211+
const zip = new JSZip();
209212
const uniquePapers = Array.from(
210213
new Set(selectedPapers.map((paper) => paper._id)),
211214
).map((id) => selectedPapers.find((paper) => paper._id === id)) as IPaper[];
212-
215+
if (!uniquePapers) {
216+
toast.error("No papers selected for download.");
217+
}
213218
for (const paper of uniquePapers) {
214-
await downloadFile(
215-
getSecureUrl(paper.final_url),
216-
generateFileName(paper),
217-
);
219+
try {
220+
const response = await fetch(getSecureUrl(paper.final_url));
221+
const blob = await response.blob();
222+
const filename = generateFileName(paper);
223+
zip.file(filename, blob);
224+
} catch (err) {
225+
// Optionally handle individual download errors
226+
console.error(`Failed to fetch ${paper.final_url}`, err);
227+
}
218228
}
229+
230+
const zipBlob = await zip.generateAsync({ type: "blob" });
231+
const url = URL.createObjectURL(zipBlob);
232+
const a = document.createElement("a");
233+
a.href = url;
234+
a.download = "papers.zip";
235+
document.body.appendChild(a);
236+
a.click();
237+
a.remove();
238+
URL.revokeObjectURL(url);
239+
toast.success("Download Initiated");
219240
}, [selectedPapers]);
220241

221242
const handleApplyFilters = useCallback(
@@ -318,7 +339,7 @@ const CatalogueContent = () => {
318339
selectedPapers={selectedPapers}
319340
subject={subject}
320341
filterOptions={filterOptions}
321-
handleDownloadAll={handleDownloadAll}
342+
handleDownloadSelected={handleDownloadSelected}
322343
closeFilters={closeFilters}
323344
/>
324345
</div>
@@ -356,7 +377,7 @@ const CatalogueContent = () => {
356377
selectedPapers={selectedPapers}
357378
subject={subject}
358379
filterOptions={filterOptions}
359-
handleDownloadAll={handleDownloadAll}
380+
handleDownloadSelected={handleDownloadSelected}
360381
closeFilters={closeFilters}
361382
/>
362383
</SheetContent>

src/components/SideBar.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function SideBar({
2222
handleApplyFilters,
2323
handleSelectAll,
2424
handleDeselectAll,
25-
handleDownloadAll,
25+
handleDownloadSelected: handleDownloadAll,
2626
}: {
2727
loading: boolean;
2828
selectedExams: string[];
@@ -47,7 +47,7 @@ function SideBar({
4747
) => void;
4848
handleSelectAll: () => void;
4949
handleDeselectAll: () => void;
50-
handleDownloadAll: () => void;
50+
handleDownloadSelected: () => void;
5151
}) {
5252
const exams =
5353
filterOptions?.unique_exams.map((exam) => ({
@@ -188,7 +188,7 @@ function SideBar({
188188
onClick={handleDownloadAll}
189189
className="cursor-pointer rounded-full border-2 border-black px-2 py-1 font-play text-xs font-semibold hover:bg-[#B2B8FF] hover:text-black dark:border-white dark:hover:border-[#434dba] dark:hover:bg-[#434dba] dark:hover:text-white"
190190
>
191-
Download All
191+
Download Selected
192192
</div>
193193
</div>
194194

0 commit comments

Comments
 (0)