No Description

ProductItem.tsx 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React from 'react';
  2. import {useState,useEffect} from 'react';
  3. import {Image,View,Text} from 'react-native';
  4. import styles from './styles';
  5. import fetch_json from '../../fetch_json';
  6. interface ProductItemProps{
  7. item: {
  8. id_producto: string,
  9. producto: string;
  10. imagen: string;
  11. comerciante: string;
  12. precio: number;
  13. insignias: string[];
  14. }
  15. }
  16. export default function ProductItems(props: ProductItemProps) {
  17. const [jsonInsignias, setInsignias] = useState([]);
  18. const url = 'http://136.145.231.41/raices_api/api/nosotros/read.php?id=1';
  19. async function fetch_() {
  20. try {
  21. const response = await fetch(url);
  22. const json = await response.json();
  23. setInsignias(json);
  24. } catch (error) {
  25. //console.error(error);
  26. };
  27. }
  28. useEffect(() => {
  29. //fetch_();
  30. fetch_json(url, setInsignias);
  31. });
  32. const {item} = props;
  33. return(
  34. <View style={styles.root} key={item.id_producto}>
  35. <Image style={styles.imagen} source={{uri: item.imagen}} />
  36. <View>
  37. {item.insignias.map((id)=>{
  38. let imagen = '';
  39. for (let i = 0; i < jsonInsignias.length; i++) {
  40. if (jsonInsignias[i].id == id) {
  41. imagen = jsonInsignias[i].imagen;
  42. break;
  43. }
  44. }
  45. if (imagen.length == 0) {
  46. //console.log(id + 'empty imagen');
  47. return;
  48. }
  49. else {
  50. return (
  51. <Image key={id} style = {styles.insignia} source={{uri: imagen}} />
  52. )
  53. }
  54. })}
  55. <Text style={styles.producto} numberOfLines={3}> {item.producto} </Text>
  56. <Text style={styles.comerciante} numberOfLines={1}> by {item.comerciante} </Text>
  57. <Text style={styles.price}> For ${item.precio}</Text>
  58. </View>
  59. </View>
  60. );
  61. }