forked from fahrPool/fahrPool
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useTransition } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { kommen, gehen } from "@/app/zeiterfassung/actions";
|
|
import { Button } from "@/components/ui/Button";
|
|
import { Alert } from "@/components/ui/Alert";
|
|
|
|
export function KommenGehenButton({ offeneSchichtId }: { offeneSchichtId: string | null }) {
|
|
const router = useRouter();
|
|
const [fehler, setFehler] = useState<string | null>(null);
|
|
const [isPending, startTransition] = useTransition();
|
|
|
|
function handleClick() {
|
|
startTransition(async () => {
|
|
const ergebnis = offeneSchichtId ? await gehen(offeneSchichtId) : await kommen();
|
|
if (!ergebnis.success) {
|
|
setFehler(ergebnis.error);
|
|
return;
|
|
}
|
|
setFehler(null);
|
|
router.refresh();
|
|
});
|
|
}
|
|
|
|
return (
|
|
<div>
|
|
{fehler && (
|
|
<div className="mb-3">
|
|
<Alert>{fehler}</Alert>
|
|
</div>
|
|
)}
|
|
<Button type="button" fullWidth={false} onClick={handleClick} disabled={isPending}>
|
|
{offeneSchichtId ? "Gehen" : "Kommen"}
|
|
</Button>
|
|
</div>
|
|
);
|
|
}
|