280 lines
8.1 KiB
TypeScript
280 lines
8.1 KiB
TypeScript
"use client";
|
|
import { formatComponentName } from "@/lib/componentName";
|
|
|
|
import { useEffect, useState } from "react"
|
|
import { Anlage } from "@/types/rk";
|
|
import Toast from "@/components/ui/Toast";
|
|
import { UnitResponse } from "@/types/anlagen/units";
|
|
import { useUnit } from "@/hooks/rk2/useUnit";
|
|
import { api_units_create, api_units_update } from "@/actions/rk2/api-units";
|
|
import { ComponentForm } from "./componentForm";
|
|
import { GroupResponse } from "@/types/anlagen/groups";
|
|
import { Button } from "@/components/ui/button";
|
|
import { ComponentDefaults, ComponentResponse, ComponentTypeDefinition } from "@/types/anlagen/components";
|
|
import { useComponent } from "@/hooks/rk2/useComponent";
|
|
import { ComponentIcon, Divide, Save, TableOfContents } from "lucide-react";
|
|
import { Card, CardContent, CardFooder, CardHeader, CardTitle } from "@/components/ui/card";
|
|
import { List, ListItem, ListItemAction, ListItemContent, ListItemDescription, ListItemIcon, ListItemTitle } from "@/components/ui/list";
|
|
import { Label } from "@/components/ui/label";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Select } from "@/components/ui/select";
|
|
|
|
|
|
|
|
interface Params {
|
|
initialComponentId?: string;
|
|
anlage: Anlage,
|
|
unitIntial?: UnitResponse
|
|
selectedGroup?: GroupResponse
|
|
onDisplaySelectGroup: () => void
|
|
componentTypes: ComponentTypeDefinition[]
|
|
componentDefaults: ComponentDefaults[]
|
|
debug?: boolean
|
|
}
|
|
|
|
export const UnitCreateForm = ({ anlage, unitIntial, onDisplaySelectGroup, selectedGroup, componentTypes, componentDefaults, debug, initialComponentId }: Params) => {
|
|
const [id, setId] = useState<string>();
|
|
const [selectedComponent, setSelectedComponent] = useState<ComponentResponse | undefined>(() => unitIntial?.components.find((component) => component.id === initialComponentId));
|
|
|
|
|
|
const { emptyUnit, unitType, setValue, unitData, string_to_UnitType } = useUnit({ unit: unitIntial });
|
|
const { componentNames, findCompType, createComponent, removeComponent } = useComponent({ componentTypes, componentDefaults });
|
|
|
|
const [toast, setToast] = useState<string | null>(null);
|
|
|
|
|
|
useEffect(() => {
|
|
console.log('init');
|
|
if (unitIntial) {
|
|
const { id, ...rest } = unitIntial;
|
|
setId(id);
|
|
} else {
|
|
}
|
|
}, [unitIntial]);
|
|
|
|
//--- Parent Group hat sich geändert
|
|
useEffect(() => {
|
|
selectedGroup && setValue("groupId", selectedGroup.id);
|
|
}, [selectedGroup])
|
|
|
|
|
|
//--- Submit --> Create oder Update
|
|
const onSubmit = async () => {
|
|
|
|
const { id: id1, ...sendData } = unitData;
|
|
|
|
let response = id ? await api_units_update(id, sendData) : await api_units_create(sendData);
|
|
|
|
if (response.success) {
|
|
setId(response.data!.id);
|
|
setToast("Erfolgreich gespeichert!");
|
|
} else {
|
|
setToast("Fehler beim Speichern!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const handlerAppendComponent = (type: string) => {
|
|
const comp = findCompType(type);
|
|
if (!comp || !id) return;
|
|
console.log(comp)
|
|
createComponent(id, { name: "", type: comp.type })
|
|
}
|
|
|
|
|
|
return (
|
|
<>
|
|
<div className="relative inline-block">
|
|
{toast && (
|
|
<Toast
|
|
message={toast}
|
|
onClose={() => setToast(null)}
|
|
/>
|
|
)}
|
|
</div>
|
|
<Card variant={"primary"}>
|
|
<CardHeader >Unit</CardHeader>
|
|
<CardContent className="ps-12 pt-4">
|
|
|
|
<div className="flex gap-4">
|
|
|
|
|
|
<div className="flex gap-1 items-center ">
|
|
<div className="text-xs">{selectedGroup ? selectedGroup.name : "noGroup"}</div>
|
|
<Button type="button" variant={"secondary"} size={"xs"} onClick={() => onDisplaySelectGroup()}><TableOfContents /></Button>
|
|
</div>
|
|
|
|
|
|
<div className="flex flex-col w-100">
|
|
<Label size={"xs"} >Prefix</Label>
|
|
<Input sizeVariant={"xs"} type="text" value={unitData.prefix} onChange={(e) => setValue('prefix', e.target.value)} />
|
|
</div>
|
|
|
|
|
|
<div className="flex flex-col w-100">
|
|
<Label size={"xs"} >BMK</Label>
|
|
<Input sizeVariant={"xs"} type="text" value={unitData.bmk} onChange={(e) => setValue('bmk', e.target.value)} />
|
|
</div>
|
|
|
|
|
|
<div className="flex flex-col w-150">
|
|
<Label size={"xs"} >Name</Label>
|
|
<Input sizeVariant={"xs"} type="text" value={unitData.name} onChange={(e) => setValue('name', e.target.value)} />
|
|
</div>
|
|
|
|
<div className="flex flex-col w-100">
|
|
<Label size={"xs"} >Postfix</Label>
|
|
<Input sizeVariant={"xs"} type="text" value={unitData.postfix} onChange={(e) => setValue('postfix', e.target.value)} />
|
|
</div>
|
|
|
|
<div className="flex flex-col w-150">
|
|
<Label size={"xs"} >Unit Typ</Label>
|
|
<Select sizeVariant={"xs"} value={unitData.type} onChange={(e) => setValue('type', string_to_UnitType(e.target.value))} >
|
|
{unitType.map((t) => <option key={t.id} value={t.id}>{t.value}</option>)}
|
|
</Select>
|
|
</div>
|
|
<Button type="button" size={"lg"} onClick={() => onSubmit()} ><Save /> </Button>
|
|
|
|
</div>
|
|
|
|
<div className="w-30 flex flex-row">
|
|
<Label size={"xs"} >Info</Label>
|
|
<Input sizeVariant={"xs"} type="text" value={unitData.info} onChange={(e) => setValue('info', e.target.value)} />
|
|
</div>
|
|
|
|
</CardContent>
|
|
<CardFooder></CardFooder>
|
|
</Card >
|
|
|
|
{selectedComponent && <div>
|
|
<Button onClick={() => setSelectedComponent(undefined)}>close</Button>
|
|
</div>
|
|
}
|
|
|
|
|
|
{/* Componente */}
|
|
|
|
|
|
{/*--- Show addable Components */}
|
|
{
|
|
!selectedComponent && <section className="flex gap-4">
|
|
{id &&
|
|
<Card className="w-60" variant={"primary"}>
|
|
<CardHeader variant={"ghost"}>Komponenten +</CardHeader>
|
|
<List variant="transparent" border={"none"} divide={false} size={"sm"}>
|
|
{componentNames.map((comp) => (
|
|
<ListItem className="" key={comp.id}>
|
|
<Button onClick={() => handlerAppendComponent(comp.id)} type="button" key={comp.id} variant={"select"} size={"xs"} >{formatComponentName(comp)}</Button>
|
|
</ListItem>
|
|
))}
|
|
</List>
|
|
</Card >
|
|
}
|
|
|
|
|
|
{/*--- Show Components List */}
|
|
{id && unitData.components &&
|
|
<Card positons="center" className="w-full">
|
|
<CardHeader>Komponenten</CardHeader>
|
|
<CardContent className="p-2">
|
|
|
|
<List className="w-120" >
|
|
{unitData.components.map((c, index) => <ListItem hover={true} size={"sm"} key={c.id}>
|
|
<ListItemIcon>
|
|
<ComponentIcon />
|
|
</ListItemIcon>
|
|
|
|
<ListItemContent className="w-100">
|
|
<ListItemTitle>
|
|
{c.type}
|
|
</ListItemTitle>
|
|
|
|
<ListItemDescription>
|
|
{formatComponentName(c)}
|
|
</ListItemDescription>
|
|
</ListItemContent>
|
|
|
|
<ListItemAction>
|
|
<Button onClick={() => setSelectedComponent(() => c)} type="button" size={"xs"} variant={"secondary"}>Details</Button>
|
|
<Button onClick={() => removeComponent(c.id)} type="button" size={"xs"} variant={"secondary"}>Löschen</Button>
|
|
</ListItemAction>
|
|
|
|
</ListItem>)}
|
|
|
|
|
|
</List>
|
|
<div>
|
|
|
|
</div>
|
|
</CardContent>
|
|
|
|
</Card >
|
|
}
|
|
|
|
</section>
|
|
}
|
|
{/* --- Components */}
|
|
|
|
|
|
|
|
<ComponentForm
|
|
component={selectedComponent}
|
|
componentTypes={componentTypes}
|
|
componentDefaults={componentDefaults}
|
|
onClose={() => setSelectedComponent(() => undefined)}
|
|
/>
|
|
|
|
|
|
{
|
|
true &&
|
|
<Card positons={"start"}>
|
|
<CardHeader variant={"danger"} >Debug</CardHeader>
|
|
<CardContent className="ps-8">
|
|
<pre>{JSON.stringify(unitData, null, 2)}</pre>
|
|
|
|
</CardContent>
|
|
</Card>
|
|
}
|
|
|
|
</>
|
|
|
|
)
|
|
}
|
|
/*
|
|
{fields.length === 0 && (<p>Noch keine Components vorhanden.</p>)}
|
|
|
|
{selectedComponent && (
|
|
|
|
<ComponentForm
|
|
index={selectedComponent.index}
|
|
register={register}
|
|
control={control}
|
|
remove={(index) => handlerRemoveItem(index)}
|
|
setValue={setValue}
|
|
componentTypes={componentTypes}
|
|
componentDefaults={componentDefaults}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{false && fields.map(
|
|
(field, index) => (
|
|
<ComponentForm
|
|
key={field.id}
|
|
index={index}
|
|
register={register}
|
|
control={control}
|
|
remove={(index) => handlerRemoveItem(index)}
|
|
setValue={setValue}
|
|
componentTypes={componentTypes}
|
|
componentDefaults={componentDefaults}
|
|
|
|
/>
|
|
)
|
|
)}
|
|
|
|
|
|
*/
|