111 lines
4.5 KiB
TypeScript
111 lines
4.5 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useRef } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
|
|
export function DownloadLightboxForm({ assetIds }: { assetIds: string[] }) {
|
|
const [loading, setLoading] = useState(false);
|
|
const dialogRef = useRef<HTMLDialogElement>(null);
|
|
const [purpose, setPurpose] = useState("");
|
|
const router = useRouter();
|
|
|
|
async function handleBulkDownload(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
if (!purpose.trim() || assetIds.length === 0) return;
|
|
|
|
setLoading(true);
|
|
|
|
try {
|
|
const response = await fetch("/api/lightbox/bulk-download", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ assetIds, purpose }),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error("Download fehlgeschlagen");
|
|
}
|
|
|
|
// Download the ZIP file
|
|
const blob = await response.blob();
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement("a");
|
|
a.href = url;
|
|
a.download = `leuchtkasten-${new Date().toISOString().split("T")[0]}.zip`;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
document.body.removeChild(a);
|
|
URL.revokeObjectURL(url);
|
|
|
|
dialogRef.current?.close();
|
|
setPurpose("");
|
|
router.refresh(); // Refresh to update usage history
|
|
} catch (err) {
|
|
alert("Es gab einen Fehler beim Bulk-Download. Bitte versuche es später erneut.");
|
|
console.error(err);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<button
|
|
type="button"
|
|
onClick={() => dialogRef.current?.showModal()}
|
|
disabled={assetIds.length === 0}
|
|
className="primary-action m-0"
|
|
>
|
|
Alle herunterladen
|
|
</button>
|
|
|
|
<dialog ref={dialogRef} className="rounded-xl p-0 backdrop:bg-black/50 shadow-2xl border border-border bg-card text-card-foreground overflow-hidden max-w-md w-full">
|
|
<div className="p-6">
|
|
<h2 className="text-xl font-bold mb-4">Verwendungszweck angeben</h2>
|
|
<p className="text-sm text-muted-foreground mb-6">
|
|
Bitte gib einen gemeinsamen Verwendungszweck für alle {assetIds.length} Bilder an. Diese werden in einem ZIP-Archiv heruntergeladen.
|
|
</p>
|
|
|
|
<form onSubmit={handleBulkDownload}>
|
|
<div className="space-y-4">
|
|
<div>
|
|
<label htmlFor="purpose" className="block text-sm font-medium mb-1">
|
|
Verwendungszweck
|
|
</label>
|
|
<input
|
|
id="purpose"
|
|
type="text"
|
|
required
|
|
placeholder="z.B. Webseite, Social Media"
|
|
value={purpose}
|
|
onChange={(e) => setPurpose(e.target.value)}
|
|
className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
|
|
disabled={loading}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="mt-8 flex justify-end gap-3">
|
|
<button
|
|
type="button"
|
|
onClick={() => dialogRef.current?.close()}
|
|
className="inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 border border-input bg-background hover:bg-accent hover:text-accent-foreground h-10 px-4 py-2"
|
|
disabled={loading}
|
|
>
|
|
Abbrechen
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
className="inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 bg-primary text-primary-foreground hover:bg-primary/90 h-10 px-4 py-2"
|
|
disabled={loading || !purpose.trim()}
|
|
>
|
|
{loading ? "Wird verpackt..." : "Download starten (ZIP)"}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</dialog>
|
|
</>
|
|
);
|
|
}
|