Initial commit: NestJS NextJS pnpm monorepo
This commit is contained in:
commit
610dcf202b
175 files changed
+25172
No files matched your search
@@ -0,0 +1,76 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
import { Category, CategoryCreate, emptyCategory } from "../../types/catalog/category.types"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { InputTextR } from "../ui/inputTextR";
|
||||
import { InputCheckboxR } from "../ui/inputCheckboxR";
|
||||
import { categoryCreate, categoryUpdate } from '../../actions/category.actions'
|
||||
|
||||
|
||||
interface Params {
|
||||
initData?: Category
|
||||
debug?: boolean
|
||||
}
|
||||
|
||||
export const CategoryForm = ({ initData, debug }: Params) => {
|
||||
|
||||
const { register, handleSubmit, formState: { errors }, watch, getFieldState, reset } = useForm<CategoryCreate>({ defaultValues: emptyCategory, })
|
||||
const [modeUpdate, setModeUpdate] = useState(false);
|
||||
const [id, setId] = useState<string | undefined>();
|
||||
const [error, setError] = useState<string | undefined>();
|
||||
|
||||
useEffect(() => {
|
||||
if (initData) {
|
||||
const { _id, name, description, isConsumable } = initData;
|
||||
setId(_id);
|
||||
setModeUpdate(true);
|
||||
reset({ name, description, isConsumable })
|
||||
}
|
||||
}, []);
|
||||
|
||||
const onSubmit = async (data: CategoryCreate) => {
|
||||
setError(undefined);
|
||||
//const { name, isConsumable, description } = data;
|
||||
if (modeUpdate) {
|
||||
if(id){
|
||||
categoryUpdate(id,data);
|
||||
}else{
|
||||
setError('Etwas ist falsch, keine ID');
|
||||
}
|
||||
|
||||
} else {
|
||||
categoryCreate(data);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-120 flex flex-col p-4 pt-10 ps-10" >
|
||||
<div className="border-1 p-4">
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="max-w-md mx-auto">
|
||||
<InputTextR label="Kategorie Name" {...register("name", { required: "Pflichtfeld" })} error={errors.name?.message} />
|
||||
<InputTextR label="Beschreibung" {...register("description")} error={errors.description?.message} />
|
||||
<div className="mt-4 mb-4 p-2 border-1 border-gray-200 relative ">
|
||||
<div className="absolute -top-2 left-2 bg-white ps-1 pe-1 text-xs">Optionen</div>
|
||||
<InputCheckboxR label="Konsumierbar" {...register("isConsumable")} error={errors.isConsumable?.message} />
|
||||
</div>
|
||||
<div>
|
||||
{error && <div>{error}</div>}
|
||||
{errors.name && <div>{errors.name.message}</div>}
|
||||
</div>
|
||||
<button type="submit" className="bg-blue-500 text-white p-2 rounded mt-2 hover:bg-blue-600">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>
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user