diff --git a/src/components/cardList/CardList.tsx b/src/components/cardList/CardList.tsx new file mode 100644 index 0000000..ff289e9 --- /dev/null +++ b/src/components/cardList/CardList.tsx @@ -0,0 +1,58 @@ +import React, { FC, useEffect, useState } from 'react' +import { Card } from '../Card' +import { FlatList, Text, TouchableOpacity } from 'react-native' +import { useFetchWithAuth } from '../../hooks/useFetchWithAuth' +import { useNavigation } from '@react-navigation/native'; + +interface Props { + endpoint: string +} + +export const CardList:FC = ({endpoint}) => { + + const navitation = useNavigation(); + + let defaultData:[] = []; + + const [entities, setEntities] = useState(defaultData); + + + const getInitData = async () => { + + const { data, error } = await useFetchWithAuth(endpoint); + + if (!error) { + setEntities(data); + } else { + console.log(error) + } + } + + useEffect(() => { + getInitData(); + }, [endpoint]) + + + return ( + <> + + + + navitation.navigate("CustomerFormScreen" as never)} + > + + {item["nombre"]} + + + + } + keyExtractor={item => item["id"]} + /> + + + + ) +} \ No newline at end of file diff --git a/src/hooks/postBasicAuth.ts b/src/hooks/postBasicAuth.ts index 04f5f67..ebe1611 100644 --- a/src/hooks/postBasicAuth.ts +++ b/src/hooks/postBasicAuth.ts @@ -19,7 +19,7 @@ const fecher = async (url: string, token: string) => { //url = "http://149.56.97.9:8080"+url; url = "http://192.168.1.21:8082"+url; - console.debug(url) + const response = await fecher(url, token) if (response.ok){ diff --git a/src/hooks/useFetchWithAuth.ts b/src/hooks/useFetchWithAuth.ts new file mode 100644 index 0000000..c78a830 --- /dev/null +++ b/src/hooks/useFetchWithAuth.ts @@ -0,0 +1,36 @@ +import AsyncStorage from '@react-native-async-storage/async-storage' + +const fecher = async (url: string, token: string) => { + return await fetch(url, { + method: "GET", + headers: { + "Content-Type": "application/json", + "Authorization":token + }, + }); +} + +export const useFetchWithAuth = async (url: string) => { + + url= "http://192.168.1.21:8082/api/"+url+"/"; + const token = await AsyncStorage.getItem("token") || ""; + let data; + let error; + try{ + const response = await fecher(url, token) + if (response.ok){ + data = await response.json(); + }else { + + error = "Servidor: "+ ((await response.json()).trace); + } + }catch (e){ + error = "Cliente: "+e; + } + + return { + data, + error + } + +} diff --git a/src/navigator/MainStackNavigator.tsx b/src/navigator/MainStackNavigator.tsx index d419cd2..100f3ec 100644 --- a/src/navigator/MainStackNavigator.tsx +++ b/src/navigator/MainStackNavigator.tsx @@ -23,7 +23,7 @@ export const MainStackNavitagor:FC = () => { headerShown: false, }} > - {isAuthenticated==true?( + {isAuthenticated==false?( <> diff --git a/src/screens/customer/CustomerListScreen.tsx b/src/screens/customer/CustomerListScreen.tsx index dda6cc0..f6f66a0 100644 --- a/src/screens/customer/CustomerListScreen.tsx +++ b/src/screens/customer/CustomerListScreen.tsx @@ -1,23 +1,38 @@ import { FC } from 'react' -import { Text } from 'react-native' +import { FlatList, Text, View } from 'react-native' import { Background } from '../../components/Background' import { Card } from '../../components/Card' -import { Input } from '../../components/Input' import { Button } from '../../components/Button' import { useNavigation } from '@react-navigation/native'; +import { CardList } from '../../components/cardList/CardList' interface Props { } export const CustomerListScreen:FC = () => { const navigation = useNavigation(); + return ( - - - Listado de clientes -