import type { NextPage } from 'next'; import { MainLayout } from '../layout/MainLayout'; import { Input } from '../../components/Input'; import { Form, useForm } from 'react-hook-form'; import { Button } from '../../components/Button'; import Cookies from 'js-cookie'; import { useRouter } from 'next/router'; import { useEffect } from 'react'; import { Toolbar } from '../../components/Toolbar'; import { useFetchWithAuth } from '../../hooks/useFetchWithAuth'; const CompaniaPage: NextPage = () => { const router = useRouter(); const entityId: string = !!router.query.id ? router.query.id as string : "0"; const baseUrl = process.env.API_URL; const { control, register,reset, setValue, formState: { isSubmitSuccessful, errors } } = useForm( { defaultValues: { id: 0, ruc: '', nombre: '', direccion: '' } } ); const getInitData = async () => { if (!entityId || entityId=="0") return; const { data, error } = await useFetchWithAuth("compania/" + entityId); if (!error) { if (data.id!=0){ setValue("id", data['id']) setValue("ruc", data['ruc']) setValue("nombre", data['nombre']) setValue("direccion", data['direccion']) } } else { console.log(error) } } useEffect(() => { getInitData(); }, [router.query.id]) useEffect(() => { reset({ id:0, ruc: "", nombre: '', direccion: '' }); }, [isSubmitSuccessful]) return ( <>
) } export default CompaniaPage