|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
import { FC } from 'react'
|
|
|
|
|
import { FC, useEffect } from 'react'
|
|
|
|
|
import { Text } from 'react-native'
|
|
|
|
|
import { NavBar } from '../../components/navbar/NavBar'
|
|
|
|
|
import { Background } from '../../components/Background'
|
|
|
|
@ -7,12 +7,70 @@ import { Button } from '../../components/Button';
|
|
|
|
|
import { Card } from '../../components/Card';
|
|
|
|
|
import { Input } from '../../components/Input';
|
|
|
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
|
interface Props {
|
|
|
|
|
import { NativeStackScreenProps } from '@react-navigation/native-stack';
|
|
|
|
|
import { RootStackParams } from './CustomerStack';
|
|
|
|
|
import { useFetchWithAuth } from '../../hooks/useFetchWithAuth';
|
|
|
|
|
import { saveWithAuth } from '../../hooks/saveWithAuth';
|
|
|
|
|
|
|
|
|
|
interface Props extends NativeStackScreenProps<RootStackParams, 'CustomerFormScreen'>{};
|
|
|
|
|
|
|
|
|
|
export const CustomerFormScreen = ({route , navigation }: Props) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { control, handleSubmit, getValues, setValue, formState: { errors } } = useForm({
|
|
|
|
|
defaultValues: {
|
|
|
|
|
id: 0,
|
|
|
|
|
nombre: '',
|
|
|
|
|
telefono: '',
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
let id:string = "0";
|
|
|
|
|
if (route.params?.id!= undefined) {
|
|
|
|
|
id = String(route.params.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getInitData = async () => {
|
|
|
|
|
if (!id || id=="0")
|
|
|
|
|
return;
|
|
|
|
|
const { data, error } = await useFetchWithAuth("cliente/" + id);
|
|
|
|
|
|
|
|
|
|
if (!error) {
|
|
|
|
|
|
|
|
|
|
if (data.id!=0){
|
|
|
|
|
setValue("id", data['id'])
|
|
|
|
|
setValue("nombre", data['nombre'])
|
|
|
|
|
setValue("telefono", data['telefono'])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
console.log(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const CustomerFormScreen:FC <Props> = () => {
|
|
|
|
|
const { control, handleSubmit, getValues, formState: { errors } } = useForm();
|
|
|
|
|
const navigation = useNavigation();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
getInitData();
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
const onSubmit = async (entity: any) => {
|
|
|
|
|
try {
|
|
|
|
|
let endpoint = "cliente";
|
|
|
|
|
//entity['compania'] = {id: entity['compania']}
|
|
|
|
|
console.log(entity)
|
|
|
|
|
const { data, error } = await saveWithAuth(endpoint, id, entity);
|
|
|
|
|
if (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
} else {
|
|
|
|
|
navigation.navigate("CustomerListScreen")
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log("Post error:");
|
|
|
|
|
console.table(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.debug(id)
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Background >
|
|
|
|
@ -22,7 +80,7 @@ export const CustomerFormScreen:FC <Props> = () => {
|
|
|
|
|
<Input name="telefono" label='Telefono' control={control} />
|
|
|
|
|
<Button
|
|
|
|
|
title='Guardar'
|
|
|
|
|
onClick={()=>{navigation.navigate("ProductScreen" as never) }}
|
|
|
|
|
onPress={handleSubmit(onSubmit)}
|
|
|
|
|
/>
|
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
|
|