sandbox-react/jszip-dev/src/components/use-download.jsx

53 lines
1.3 KiB
JavaScript

import JSZip from "jszip";
import {getBlob, getBlobAsync} from '../utils/download';
import config from '../config';
const useDownload = () => {
async function handleZip(images) {
const zip = new JSZip();
//TODO Cleanup!
/*
let url = `${config.API}csv-fare-zones`;
let file = 'fare-zones.csv';
const blobVersions = await getBlobAsync(url,file);
//file(name, data)
zip.file(file, blobVersions);
url = `${config.API}csv-price-levels`;
file = 'price-levels.csv';
const blobPriceLevels = await getBlobAsync(url,file);
//file(name, data)
zip.file(file, blobPriceLevels);
*/
//TODO Cleanup!
// Add Images to the zip file
for (let i = 0; i < images.length; i++) {
const url = images[i].url;
console.log('handleZip() url: ', url);
const file = images[i].file;
console.log('handleZip() file: ', file);
const response = await fetch(url);
const blob = await response.blob();
zip.file(file, blob);
}
// Generate the zip file
const zipData = await zip.generateAsync({
type: "blob",
streamFiles: true,
});
// Create a download link for the zip file
const link = document.createElement("a");
link.href = window.URL.createObjectURL(zipData);
link.download = "name-zip-folder.zip";
link.click();
};
return { handleZip };
};
export { useDownload };