add download button for files
This commit is contained in:
parent
14d038d066
commit
1aee8686fa
1 changed files with 127 additions and 25 deletions
|
@ -139,6 +139,25 @@ import { Icon } from "astro-icon/components";
|
|||
<div class="flex items-center gap-3">
|
||||
<h3 class="font-bold text-lg" id="modalTitle">Event Files</h3>
|
||||
</div>
|
||||
<div class="flex gap-2">
|
||||
<button
|
||||
id="downloadAllBtn"
|
||||
class="btn btn-primary btn-sm gap-2"
|
||||
onclick="window.downloadAllFiles()"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
class="h-4 w-4"
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
>
|
||||
<path
|
||||
fill-rule="evenodd"
|
||||
d="M3 17a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm3.293-7.707a1 1 0 011.414 0L9 10.586V3a1 1 0 112 0v7.586l1.293-1.293a1 1 0 111.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z"
|
||||
clip-rule="evenodd"></path>
|
||||
</svg>
|
||||
Download All
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-circle btn-ghost"
|
||||
onclick="eventDetailsModal.close()"
|
||||
|
@ -158,6 +177,7 @@ import { Icon } from "astro-icon/components";
|
|||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="filesContent" class="space-y-4">
|
||||
<!-- Files list will be populated here -->
|
||||
|
@ -713,9 +733,13 @@ import { Icon } from "astro-icon/components";
|
|||
event.files &&
|
||||
event.files.length > 0
|
||||
? `
|
||||
<button onclick="window.openDetailsModal(window['${eventDataId}'])" class="btn btn-ghost btn-xs gap-1">
|
||||
<Icon name="heroicons:folder-open" class="w-4 h-4" />
|
||||
Files (${event.files.length})
|
||||
<button onclick="window.openDetailsModal(window['${eventDataId}'])" class="btn btn-sm btn-primary w-[90px] inline-flex items-center justify-center">
|
||||
<div class="flex items-center gap-1">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path d="M5 19a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h4l2 2h4a2 2 0 0 1 2 2v1M5 19h14a2 2 0 0 0 2-2v-5a2 2 0 0 0-2-2H9a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2z" />
|
||||
</svg>
|
||||
<span>Files</span>
|
||||
</div>
|
||||
</button>
|
||||
`
|
||||
: "";
|
||||
|
@ -783,9 +807,13 @@ import { Icon } from "astro-icon/components";
|
|||
${
|
||||
event.files && event.files.length > 0
|
||||
? `
|
||||
<button onclick="window.openDetailsModal(window['${eventDataId}'])" class="btn btn-ghost btn-xs gap-1">
|
||||
<Icon name="heroicons:folder-open" class="w-4 h-4" />
|
||||
Files (${event.files.length})
|
||||
<button onclick="window.openDetailsModal(window['${eventDataId}'])" class="btn btn-sm btn-primary w-[90px] inline-flex items-center justify-center">
|
||||
<div class="flex items-center gap-1">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path d="M5 19a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h4l2 2h4a2 2 0 0 1 2 2v1M5 19h14a2 2 0 0 0 2-2v-5a2 2 0 0 0-2-2H9a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2z" />
|
||||
</svg>
|
||||
<span>Files</span>
|
||||
</div>
|
||||
</button>
|
||||
`
|
||||
: ""
|
||||
|
@ -1076,4 +1104,78 @@ import { Icon } from "astro-icon/components";
|
|||
|
||||
modal.showModal();
|
||||
};
|
||||
|
||||
// Add downloadAllFiles function
|
||||
declare global {
|
||||
interface Window {
|
||||
downloadAllFiles: () => Promise<void>;
|
||||
[key: string]: any;
|
||||
}
|
||||
}
|
||||
|
||||
window.downloadAllFiles = async function () {
|
||||
const downloadBtn = document.getElementById(
|
||||
"downloadAllBtn"
|
||||
) as HTMLButtonElement;
|
||||
if (!downloadBtn) return;
|
||||
|
||||
const originalBtnContent = downloadBtn.innerHTML;
|
||||
|
||||
try {
|
||||
// Show loading state
|
||||
downloadBtn.innerHTML =
|
||||
'<span class="loading loading-spinner loading-xs"></span> Preparing...';
|
||||
downloadBtn.disabled = true;
|
||||
|
||||
// Load JSZip dynamically
|
||||
// @ts-ignore - Dynamically importing JSZip from CDN
|
||||
const JSZip = (
|
||||
await import("https://cdn.jsdelivr.net/npm/jszip@3.10.1/+esm")
|
||||
).default;
|
||||
const zip = new JSZip();
|
||||
|
||||
// Get current event files
|
||||
const baseUrl = "https://pocketbase.ieeeucsd.org";
|
||||
const collectionId = "events";
|
||||
const recordId = currentEventId;
|
||||
|
||||
// Get the current event from the window object
|
||||
const eventDataId = `event_${currentEventId}`;
|
||||
const event = window[eventDataId] as Event;
|
||||
|
||||
if (!event || !event.files || event.files.length === 0) {
|
||||
throw new Error("No files available to download");
|
||||
}
|
||||
|
||||
// Download each file and add to zip
|
||||
const filePromises = event.files.map(async (filename: string) => {
|
||||
const fileUrl = `${baseUrl}/api/files/${collectionId}/${recordId}/${filename}`;
|
||||
const response = await fetch(fileUrl);
|
||||
const blob = await response.blob();
|
||||
zip.file(filename, blob);
|
||||
});
|
||||
|
||||
await Promise.all(filePromises);
|
||||
|
||||
// Generate and download zip
|
||||
const zipBlob = await zip.generateAsync({ type: "blob" });
|
||||
const downloadUrl = URL.createObjectURL(zipBlob);
|
||||
const link = document.createElement("a");
|
||||
link.href = downloadUrl;
|
||||
link.download = `${event.event_name}_files.zip`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(downloadUrl);
|
||||
|
||||
// Reset button state
|
||||
downloadBtn.innerHTML = originalBtnContent;
|
||||
downloadBtn.disabled = false;
|
||||
} catch (error) {
|
||||
console.error("Failed to download files:", error);
|
||||
createToast("Failed to download files. Please try again.", "error");
|
||||
downloadBtn.innerHTML = originalBtnContent;
|
||||
downloadBtn.disabled = false;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
|
Loading…
Reference in a new issue