diff --git a/src/app/pedidos/producto/[id]/page.tsx b/src/app/pedidos/producto/[id]/page.tsx new file mode 100644 index 0000000..eaf2d43 --- /dev/null +++ b/src/app/pedidos/producto/[id]/page.tsx @@ -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 ( + <> +
+ +
+
+ + + +
+
+ + ) +} + +export default Page; \ No newline at end of file