forked from fahrPool/fahrPool
42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useTransition } from "react";
|
|
import { exportiereCsv } from "@/app/zeiterfassung/actions";
|
|
import { Button } from "@/components/ui/Button";
|
|
import { Alert } from "@/components/ui/Alert";
|
|
|
|
export function CsvExportButton({ von, bis, fahrerId }: { von: string; bis: string; fahrerId?: string }) {
|
|
const [fehler, setFehler] = useState<string | null>(null);
|
|
const [isPending, startTransition] = useTransition();
|
|
|
|
function handleClick() {
|
|
startTransition(async () => {
|
|
const ergebnis = await exportiereCsv({ von, bis }, fahrerId);
|
|
if (!ergebnis.success) {
|
|
setFehler(ergebnis.error);
|
|
return;
|
|
}
|
|
const blob = new Blob([ergebnis.csv], { type: "text/csv;charset=utf-8;" });
|
|
const url = URL.createObjectURL(blob);
|
|
const link = document.createElement("a");
|
|
link.href = url;
|
|
link.download = `zeiterfassung_${von.slice(0, 10)}_${bis.slice(0, 10)}.csv`;
|
|
link.click();
|
|
URL.revokeObjectURL(url);
|
|
});
|
|
}
|
|
|
|
return (
|
|
<div>
|
|
{fehler && (
|
|
<div className="mb-3">
|
|
<Alert>{fehler}</Alert>
|
|
</div>
|
|
)}
|
|
<Button type="button" fullWidth={false} variant="secondary" onClick={handleClick} disabled={isPending}>
|
|
CSV-Export
|
|
</Button>
|
|
</div>
|
|
);
|
|
}
|