copy unit
This commit is contained in:
1 parent
f769ff7cc0
commit
c15e69f7b5
4 files changed
+159
-1
No files matched your search
@@ -0,0 +1,80 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { api_units_copy } from "@/actions/rk2/api-units";
|
||||
import Modal from "@/components/ui/Modal";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { GroupSelect } from "../groups/groupSelect";
|
||||
import type { GroupResponse } from "@/types/anlagen/groups";
|
||||
import type { UnitCopy, UnitResponse } from "@/types/anlagen/units";
|
||||
|
||||
export function UnitCopyDialog({ unit, groups, onClose }: {
|
||||
unit: UnitResponse; groups: GroupResponse[]; onClose: () => void;
|
||||
}) {
|
||||
const router = useRouter();
|
||||
const [data, setData] = useState<UnitCopy>({
|
||||
name: unit.name, groupId: unit.groupId || null, prefix: unit.prefix ?? '', bmk: unit.bmk ?? '', postfix: unit.postfix ?? '',
|
||||
});
|
||||
const [selectGroup, setSelectGroup] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const group = groups.find((item) => item.id === data.groupId);
|
||||
|
||||
const submit = async (event: React.FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault();
|
||||
if (busy) return;
|
||||
setBusy(true);
|
||||
setError('');
|
||||
try {
|
||||
const result = await api_units_copy(unit.id, data);
|
||||
if (!result.success) {
|
||||
setError(Array.isArray(result.errors) ? result.errors.join(', ') : String(result.errors));
|
||||
setBusy(false);
|
||||
return;
|
||||
}
|
||||
router.push(`/rk2/units/edit/${encodeURIComponent(result.data.id)}`);
|
||||
router.refresh();
|
||||
onClose();
|
||||
} catch {
|
||||
setError('Kopieren fehlgeschlagen. Bitte vor erneutem Kopieren die Unit-Liste prüfen.');
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
return <Modal onClose={() => { if (!busy) onClose(); }}>
|
||||
<div className="w-full max-w-lg">
|
||||
<h2 className="mb-4 text-lg font-semibold">Unit kopieren</h2>
|
||||
<p className="mb-4 text-sm">Komponenten und Signale werden kopiert. IO-Zuordnungen bleiben leer. Kopiert wird der gespeicherte Stand.</p>
|
||||
<form onSubmit={submit} className="flex flex-col gap-3">
|
||||
<fieldset disabled={busy} className="flex flex-col gap-3">
|
||||
<div>
|
||||
<Label>Gruppe</Label>
|
||||
<div className="flex items-center gap-2">
|
||||
<span>{group?.name ?? 'Ohne Gruppe'}</span>
|
||||
<Button type="button" variant="secondary" onClick={() => setSelectGroup(!selectGroup)}>Gruppe wählen</Button>
|
||||
<Button type="button" variant="secondary" onClick={() => setData((current) => ({ ...current, groupId: null }))}>Ohne Gruppe</Button>
|
||||
</div>
|
||||
{selectGroup && <div className="mt-2 max-h-64 overflow-auto border p-2">
|
||||
<GroupSelect groups={groups} onSelect={(selected) => {
|
||||
setData((current) => ({ ...current, groupId: selected.id }));
|
||||
setSelectGroup(false);
|
||||
}} />
|
||||
</div>}
|
||||
</div>
|
||||
{(['prefix', 'bmk', 'name', 'postfix'] as const).map((key) => <div key={key}>
|
||||
<Label htmlFor={`copy-unit-${key}`}>{({ prefix: 'Prefix', bmk: 'BMK', name: 'Name', postfix: 'Postfix' })[key]}</Label>
|
||||
<Input id={`copy-unit-${key}`} value={data[key]} required={key === 'name'} onChange={(event) => setData((current) => ({ ...current, [key]: event.target.value }))} />
|
||||
</div>)}
|
||||
</fieldset>
|
||||
{error && <p role="alert" className="text-red-600">{error}</p>}
|
||||
<div className="flex gap-2">
|
||||
<Button type="submit" disabled={busy || !data.name.trim()}>{busy ? 'Kopie wird erstellt …' : 'Kopie erstellen'}</Button>
|
||||
<Button type="button" variant="secondary" disabled={busy} onClick={onClose}>Abbrechen</Button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</Modal>;
|
||||
}
|
||||
@@ -9,6 +9,10 @@ import { useGroups } from "@/hooks/rk2/useGroups";
|
||||
import { UnitCreateForm } from "./unit_createForm";
|
||||
import { GroupSelect } from "../groups/groupSelect";
|
||||
import { ComponentDefaults, ComponentTypeDefinition } from "@/types/anlagen/components";
|
||||
import { UnitCopyDialog } from "./unitCopyDialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { api_units_delete } from "@/actions/rk2/api-units";
|
||||
|
||||
|
||||
|
||||
@@ -28,6 +32,30 @@ export const UnitEdit = ({ anlage, groups, selectedUnit, componentTypes,componen
|
||||
|
||||
const { getGroupById } = useGroups({ groups });
|
||||
const [showSelectGroup, setShowSelectGroup] = useState(false);
|
||||
const [showCopy, setShowCopy] = useState(false);
|
||||
const [showDelete, setShowDelete] = useState(false);
|
||||
const [deleting, setDeleting] = useState(false);
|
||||
const [deleteError, setDeleteError] = useState('');
|
||||
const router = useRouter();
|
||||
|
||||
const deleteUnit = async () => {
|
||||
if (!selectedUnit || deleting) return;
|
||||
setDeleting(true);
|
||||
setDeleteError('');
|
||||
try {
|
||||
const result = await api_units_delete(selectedUnit.id);
|
||||
if (!result.success) {
|
||||
setDeleteError(result.errors.join(', '));
|
||||
setDeleting(false);
|
||||
return;
|
||||
}
|
||||
router.push('/rk2/units');
|
||||
router.refresh();
|
||||
} catch {
|
||||
setDeleteError('Löschen fehlgeschlagen. Bitte die Unit-Liste prüfen.');
|
||||
setDeleting(false);
|
||||
}
|
||||
};
|
||||
const [selectedGroup, setSelectedGroup] = useState<GroupResponse>();
|
||||
|
||||
|
||||
@@ -55,6 +83,18 @@ export const UnitEdit = ({ anlage, groups, selectedUnit, componentTypes,componen
|
||||
|
||||
return (
|
||||
<div className="" >
|
||||
{selectedUnit && <Button className="mb-4" type="button" variant="secondary" onClick={() => setShowCopy(true)}>Unit kopieren</Button>}
|
||||
{showCopy && selectedUnit && <UnitCopyDialog unit={selectedUnit} groups={groups} onClose={() => setShowCopy(false)} />}
|
||||
{selectedUnit && <Button className="mb-4 ml-2" type="button" variant="secondary" onClick={() => { setDeleteError(''); setShowDelete(true); }}>Unit löschen</Button>}
|
||||
{showDelete && selectedUnit && <Modal onClose={() => { if (!deleting) setShowDelete(false); }}>
|
||||
<h2 className="mb-4 text-lg font-semibold">Unit löschen?</h2>
|
||||
<p className="mb-4">„{selectedUnit.name}“ mit allen Komponenten und Signalen endgültig löschen? Die IO-Datensätze bleiben erhalten.</p>
|
||||
{deleteError && <p role="alert" className="mb-4 text-red-600">{deleteError}</p>}
|
||||
<div className="flex gap-2">
|
||||
<Button type="button" disabled={deleting} onClick={deleteUnit}>{deleting ? 'Wird gelöscht …' : 'Endgültig löschen'}</Button>
|
||||
<Button type="button" variant="secondary" disabled={deleting} onClick={() => setShowDelete(false)}>Abbrechen</Button>
|
||||
</div>
|
||||
</Modal>}
|
||||
|
||||
<UnitCreateForm anlage={anlage} selectedGroup={selectedGroup} onDisplaySelectGroup={() => setShowSelectGroup(true)} unitIntial={selectedUnit} componentTypes={componentTypes} componentDefaults={componentDefaults} initialComponentId={initialComponentId}/>
|
||||
|
||||
|
||||
Reference in new issue
Block a user