parent
7ee82313d5
commit
f5286514a6
@ -0,0 +1,86 @@
|
|||||||
|
'use client'
|
||||||
|
import { Input } from '@/components/Input';
|
||||||
|
import { useForm } from 'react-hook-form';
|
||||||
|
import Button from '@/components/Button';
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { Toolbar } from '@/components/Toolbar';
|
||||||
|
import useFetchWithAuth from '@/hooks/useFetchWithAuth';
|
||||||
|
import saveWithAuth from '@/hooks/saveWithAuth';
|
||||||
|
|
||||||
|
const Page = ({ params }: { params: { id: string } }) => {
|
||||||
|
|
||||||
|
const router = useRouter();
|
||||||
|
const entityId =params.id;
|
||||||
|
const [producto, setproducto] = useState({
|
||||||
|
id: 0,
|
||||||
|
nombre: '',
|
||||||
|
precio: 0,
|
||||||
|
});
|
||||||
|
const { register, setValue,getValues,handleSubmit, formState: { errors } } = useForm(
|
||||||
|
{
|
||||||
|
defaultValues: {
|
||||||
|
...producto
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const getInitData = async () => {
|
||||||
|
if (!entityId || entityId=="0")
|
||||||
|
return;
|
||||||
|
const { data, error } = await useFetchWithAuth("producto/" + entityId);
|
||||||
|
|
||||||
|
if (!error) {
|
||||||
|
|
||||||
|
if (data.id!=0){
|
||||||
|
setValue("id", data['id'])
|
||||||
|
setValue("nombre", data['nombre'])
|
||||||
|
setValue("precio", Number(data['precio']))
|
||||||
|
setproducto(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
console.log(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const onSubmit = async (entity: any) => {
|
||||||
|
try {
|
||||||
|
let endpoint = "producto";
|
||||||
|
|
||||||
|
const { data, error } = await saveWithAuth(endpoint, entityId, entity);
|
||||||
|
if (error) {
|
||||||
|
console.log(error);
|
||||||
|
} else {
|
||||||
|
router.push("pedidos/"+endpoint+"/"+ data.id)
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Post error:");
|
||||||
|
console.table(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
getInitData();
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className='grow '>
|
||||||
|
<Toolbar pathForm='pedidos/producto/0' pathList='pedidos/producto/' currentEntity={getValues("nombre")} entityName='producto'/>
|
||||||
|
<div className='h-96 w-4/6 mx-auto p-4 border-solid border-gray-300 border-2 rounded-xl '>
|
||||||
|
<form onSubmit={handleSubmit(onSubmit)}>
|
||||||
|
<Input id='id' label='' register={register} type='hidden'/>
|
||||||
|
<Input id='nombre' label='Nombre' register={register} type='text'/>
|
||||||
|
<Input id='precio' label='Precio' register={register} type='number'/>
|
||||||
|
<Button className="btn-sm my-2" type='submit' label='Guardar' />
|
||||||
|
{errors?.root?.server && <p>Form submit failed.</p>}
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Page;
|
Loading…
Reference in new issue