135 lines
4.0 KiB
TypeScript
135 lines
4.0 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react"
|
|
import { Controller, useForm } from "react-hook-form"
|
|
import { InputTextR } from "../ui/inputTextR";
|
|
import { emptyLocation, Location, LocationCreate, LocationMutaion, LocationTreeNode, LocationUpdate } from "../../types/inventory";
|
|
import { locationCreate, locationTree, locationUpdate } from "../../actions/location.actions";
|
|
|
|
import { useRouter } from "next/navigation"
|
|
import { sites } from "../../_config/sites";
|
|
import { LocationTreeView } from "./location.treeView";
|
|
|
|
interface Params {
|
|
initData?: Location
|
|
debug?: boolean
|
|
|
|
}
|
|
|
|
export const LocationForm = ({ initData, debug }: Params) => {
|
|
|
|
const [locationTreeData, setLocationTree] = useState<LocationTreeNode[]>()
|
|
|
|
useEffect(() => {
|
|
const load = async () => {
|
|
|
|
let d = await locationTree();
|
|
setLocationTree(d);
|
|
}
|
|
load();
|
|
|
|
if (initData) {
|
|
const { _id, name, description, parentLocationId } = initData;
|
|
setId(_id);
|
|
setModeUpdate(true);
|
|
reset({ name, description, parentLocationId })
|
|
}
|
|
|
|
}, [initData])
|
|
|
|
|
|
const router = useRouter();
|
|
const {
|
|
register,
|
|
handleSubmit,
|
|
formState: { errors },
|
|
watch,
|
|
control,
|
|
getFieldState,
|
|
|
|
reset }
|
|
= useForm<LocationMutaion>({ defaultValues: emptyLocation })
|
|
|
|
const [modeUpdate, setModeUpdate] = useState(false);
|
|
const [id, setId] = useState<string | undefined>();
|
|
const [error, setError] = useState<string | undefined>();
|
|
|
|
|
|
|
|
const onSubmit = async (data: LocationMutaion) => {
|
|
setError(undefined);
|
|
|
|
if (modeUpdate) {
|
|
if (id) {
|
|
locationUpdate(id, data);
|
|
} else {
|
|
setError('Etwas ist falsch, keine ID');
|
|
}
|
|
|
|
} else {
|
|
|
|
const res = await locationCreate(data);
|
|
/* if (!res.ok) {
|
|
setError("_id")
|
|
return
|
|
}
|
|
*/
|
|
router.push(sites.location.url)
|
|
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="w-120 flex flex-col p-4 pt-10 ps-10" >
|
|
{modeUpdate ? <div>Update</div> : <div>Anlegen</div>}
|
|
<div className="border-1 p-4">
|
|
|
|
<form onSubmit={handleSubmit(onSubmit)} className="max-w-md mx-auto">
|
|
<InputTextR
|
|
label="Lagerort Bezeichnung"
|
|
{...register("name", { required: "Pflichtfeld" })}
|
|
error={errors.name?.message}
|
|
/>
|
|
|
|
<InputTextR
|
|
label="Beschreibung"
|
|
{...register("description")}
|
|
error={errors.description?.message}
|
|
/>
|
|
{locationTreeData &&
|
|
<Controller
|
|
name="parentLocationId"
|
|
control={control}
|
|
rules={{ required: false }}
|
|
render={({ field }) => (
|
|
<LocationTreeView data={locationTreeData}
|
|
onSelect={(id) => field.onChange(id)}
|
|
/>
|
|
)}
|
|
/>
|
|
}
|
|
|
|
<div>
|
|
{error && <div>{error}</div>}
|
|
{errors.name && <div>{errors.name.message}</div>}
|
|
</div>
|
|
|
|
<button
|
|
className="bg-blue-500 text-white p-2 rounded mt-2 hover:bg-blue-600"
|
|
type="submit">
|
|
Speichern
|
|
</button>
|
|
</form>
|
|
</div>
|
|
|
|
{debug &&
|
|
<div>
|
|
<div>{watch("name")}</div>
|
|
<pre>touched:{getFieldState("name").isTouched ? 'true' : 'false'}</pre>
|
|
<pre>{JSON.stringify(watch(), null, 2)}</pre>
|
|
</div>
|
|
}
|
|
</div>
|
|
|
|
)
|
|
} |