@@ -22,6 +22,8 @@ import {
2222 downloadFile ,
2323} from "@/util/download_paper" ;
2424import type { ICourses } from "@/interface" ;
25+ import JSZip from "jszip" ;
26+ import { toast } from "react-hot-toast" ;
2527import { useCourses } from "@/context/courseContext" ;
2628
2729const 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 >
0 commit comments