@@ -0,0 +1,54 @@
|
||||
"use client";
|
||||
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
|
||||
type Scenario = {
|
||||
id: string;
|
||||
name: string;
|
||||
isBaseline: boolean;
|
||||
};
|
||||
|
||||
type ScenarioActionsProps = {
|
||||
planId: string;
|
||||
scenarios: Scenario[];
|
||||
};
|
||||
|
||||
export function ScenarioActions({ planId, scenarios }: ScenarioActionsProps) {
|
||||
const router = useRouter();
|
||||
const [loading, setLoading] = useState(false);
|
||||
const baseline = scenarios.find((s) => s.isBaseline) ?? scenarios[0];
|
||||
|
||||
async function createScenario() {
|
||||
const name = prompt("Name des neuen Szenarios:", "Alternativszenario");
|
||||
if (!name) return;
|
||||
|
||||
setLoading(true);
|
||||
const res = await fetch(`/api/plans/${planId}/scenarios`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
name,
|
||||
copyFromScenarioId: baseline?.id,
|
||||
}),
|
||||
});
|
||||
setLoading(false);
|
||||
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
router.push(`/plans/${planId}/scenarios/${data.id}`);
|
||||
router.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={createScenario}
|
||||
className="btn-secondary"
|
||||
disabled={loading}
|
||||
>
|
||||
{loading ? "Erstellen…" : "+ Szenario kopieren"}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user