1
1
import { useEffect , useState } from "react" ;
2
2
import { type Paper } from "@/interface" ;
3
3
import Image from "next/image" ;
4
- import { Eye , Download } from "lucide-react" ;
4
+ import { Eye } from "lucide-react" ;
5
5
import {
6
6
extractBracketContent ,
7
7
extractWithoutBracketContent ,
8
8
} from "@/util/utils" ;
9
9
import { capsule } from "@/util/utils" ;
10
- import axios from "axios" ;
11
10
import { useRouter } from "next/navigation" ;
12
11
import Link from "next/link" ;
13
12
@@ -27,31 +26,11 @@ const Card = ({
27
26
setChecked ( isSelected ) ;
28
27
} , [ isSelected ] ) ;
29
28
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
-
36
29
function handleCheckboxChange ( ) {
37
30
setChecked ( ! checked ) ;
38
31
onSelect ( paper , ! checked ) ;
39
32
}
40
33
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
-
55
34
function handleOpen ( ) {
56
35
const storedPapers = JSON . parse (
57
36
localStorage . getItem ( "clickedPapers" ) ?? "[]" ,
@@ -106,8 +85,18 @@ const Card = ({
106
85
</ div >
107
86
< div className = "flex gap-2" >
108
87
< Eye size = { 20 } className = "cursor-pointer" onClick = { handleOpen } />
109
- < button onClick = { ( ) => handleDownload ( paper ) } >
110
- < Download size = { 20 } />
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
111
100
</ button >
112
101
</ div >
113
102
</ div >
0 commit comments