Agrega producto OK

master
Freddy Heredia 2 years ago
parent 477d589aac
commit f81b0a9914

@ -11,23 +11,20 @@ interface Props {
const OneToMany: FC<Props> = ({}) => {
const { lines } = useContext(SalesOrderContext);
console.log("Lineas: "+JSON.stringify(lines))
return (
<ScrollView style={{flex: 1}}>
<ScrollView >
<FlatList
style={{flex: 1}}
contentContainerStyle={{ paddingBottom: 20 }}
data={lines}
extraData={lines}
renderItem={({ item }) =>
<View className='flex-1 items-center bg-red-200'>
<View >
<>
<Text className='text-xl text-center my-4'>{item.producto.nombre}</Text>
</>
</View>
}
keyExtractor={(item: SalesOrderLine) => String(item.producto.id)}
/>
{lines.map((line: SalesOrderLine) =>(<Text key={line.producto.id}>{line.producto.id}</Text>))}
</ScrollView>

@ -1,5 +1,5 @@
import React, { FC, useContext, useEffect, useState } from 'react'
import { FlatList, RefreshControl, Text, TouchableOpacity, View } from 'react-native'
import { FlatList, Text, TouchableOpacity, View } from 'react-native'
import { useNavigation } from '@react-navigation/native';
@ -32,16 +32,10 @@ export const ProductItem: FC<Props> = ({ entities, screenName, columnDisplay })
const newLine: SalesOrderLine = {
producto: { id: item['id'], nombre: item["nombre"] },
cantidad: 1,
precio: 0
precio: item["precio"]
}
add(newLine)
navigation.navigate(
{
name: screenName,
params: { productId: item['id'] },
merge: true,
} as never
);
navigation.pop();
}}
>
<CardItem>

@ -1,9 +1,9 @@
import { useContext, useEffect, useState } from 'react'
import { FlatList, Text, View } from 'react-native'
import { FlatList, Text } from 'react-native'
import { Button } from '../../components/Button';
import { Card } from '../../components/Card';
import { Input } from '../../components/Input';
import { useForm, useFieldArray } from 'react-hook-form';
import { useForm } from 'react-hook-form';
import { NativeStackScreenProps } from '@react-navigation/native-stack';
import { useFetchWithAuth } from '../../hooks/useFetchWithAuth';
import { saveWithAuth } from '../../hooks/saveWithAuth';
@ -15,11 +15,10 @@ import { SalesOrderContext } from './SalesOrderContex';
interface Props extends NativeStackScreenProps<RootStackParams, 'SalesOrderFormScreen'> { };
export const SalesOrderFormScreen = ({ route, navigation }: Props) => {
console.log("Apertura de screem :" + route.params?.productId)
const {lines, setLines, add} = useContext(SalesOrderContext);
const { control, handleSubmit, setValue, getValues, formState: { errors } } = useForm({
const { control, handleSubmit, setValue, formState: { errors } } = useForm({
defaultValues: {
id: 0,
numero: '',
@ -32,29 +31,6 @@ export const SalesOrderFormScreen = ({ route, navigation }: Props) => {
id = String(route.params.id);
}
useEffect(() => {
if (route.params?.productId != undefined && route.params?.productId > 0) {
const newId = route.params?.productId;
const exits = lines.filter(line => line.producto.id == newId)
if (exits.length == 0) {
const newLine: SalesOrderLine = {
producto: { id: newId, nombre: "test" },
cantidad: 1,
precio: 0
}
console.log("Nueva linea :" + route.params?.productId)
//append(newLine);
add(newLine);
//setValue("detalle", lines);
//console.table(fields)
}else{
console.log("Linea repetida");
console.log(exits)
}
}
}, [route.params?.productId]);
const getInitData = async () => {
if (!id || id == "0")
return;
@ -67,7 +43,6 @@ export const SalesOrderFormScreen = ({ route, navigation }: Props) => {
setValue("numero", data['numero'])
setValue("cliente", data['cliente'])
const lines: SalesOrderLine[] = data['detalle'];
console.log("Agregando lineas:")
setLines(lines);
}
@ -85,6 +60,7 @@ export const SalesOrderFormScreen = ({ route, navigation }: Props) => {
try {
let endpoint = "pedido";
entity['cliente'] = { id: entity['cliente'].id }
entity['detalle'] = lines;
const { data, error } = await saveWithAuth(endpoint, id, entity);
if (error) {
console.log(error);

Loading…
Cancel
Save