Skip to content

Commit 66f497d

Browse files
dynamic
1 parent 04aa0cc commit 66f497d

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/components/Card.tsx

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { useEffect, useState } from "react";
22
import { type Paper } from "@/interface";
33
import Image from "next/image";
4-
import { Eye } from "lucide-react";
4+
import { Eye, Download } from "lucide-react";
55
import {
66
extractBracketContent,
77
extractWithoutBracketContent,
88
} from "@/util/utils";
99
import { capsule } from "@/util/utils";
10+
import axios from "axios";
1011
import { useRouter } from "next/navigation";
1112
import Link from "next/link";
1213

@@ -26,11 +27,31 @@ const Card = ({
2627
setChecked(isSelected);
2728
}, [isSelected]);
2829

30+
const handleDownload = async (paper: Paper) => {
31+
const extension = paper.finalUrl.split(".").pop();
32+
const fileName = `${extractBracketContent(paper.subject)}-${paper.exam}-${paper.slot}-${paper.year}.${extension}`;
33+
await downloadFile(paper.finalUrl, fileName);
34+
};
35+
2936
function handleCheckboxChange() {
3037
setChecked(!checked);
3138
onSelect(paper, !checked);
3239
}
3340

41+
async function downloadFile(url: string, filename: string) {
42+
try {
43+
const response = await axios.get(url, { responseType: "blob" });
44+
const blob = new Blob([response.data]);
45+
const link = document.createElement("a");
46+
link.href = window.URL.createObjectURL(blob);
47+
link.download = filename;
48+
link.click();
49+
window.URL.revokeObjectURL(link.href);
50+
} catch (error) {
51+
console.error("Error downloading file:", error);
52+
}
53+
}
54+
3455
function handleOpen() {
3556
const storedPapers = JSON.parse(
3657
localStorage.getItem("clickedPapers") ?? "[]",
@@ -85,23 +106,14 @@ const Card = ({
85106
</div>
86107
<div className="flex gap-2">
87108
<Eye size={20} className="cursor-pointer" onClick={handleOpen} />
88-
<button
89-
onClick={() => {
90-
const iframe = document.createElement("iframe");
91-
iframe.style.display = "none";
92-
iframe.src = paper.finalUrl;
93-
document.body.appendChild(iframe);
94-
setTimeout(() => {
95-
document.body.removeChild(iframe);
96-
}, 1000);
97-
}}
98-
>
99-
Download
109+
<button onClick={() => handleDownload(paper)}>
110+
<Download size={20} />
100111
</button>
101112
</div>
102113
</div>
103114
</div>
104115
);
105116
};
117+
export const dynamic = 'force-dynamic'
106118

107119
export default Card;

0 commit comments

Comments
 (0)