import React, { useEffect, useState } from 'react';
import { View, Text, TouchableOpacity, ScrollView } from 'react-native';
import axios from 'axios';
const HomeScreen = () => {
const [categories, setCategories] = useState([]);
const [products, setProducts] = useState([]);
const [groupedProducts, setGroupedProducts] = useState({});
useEffect(() => {
// Kategorileri çek
axios.get('https://bego-api.testmagna.com/api/categories')
.then(response => {
setCategories(response.data);
})
.catch(error => {
console.error('Error fetching categories: ', error);
});
// Ürünleri çek
axios.get('https://bego-api.testmagna.com/api/products')
.then(response => {
setProducts(response.data);
})
.catch(error => {
console.error('Error fetching products: ', error);
});
}, []);
// Ürünleri kategorilerine göre grupla
useEffect(() => {
const grouped = {};
products.forEach(product => {
const categoryId = product.category_id;
if (!grouped[categoryId]) {
grouped[categoryId] = [];
}
grouped[categoryId].push(product);
});
setGroupedProducts(grouped);
}, [products]);
const renderCategoryItem = (category) => (
handleCategoryPress(category)}>
{category.title}
);
const renderProductItem = (product) => (
Ürün Adı: {product.urun_adi}
Ürün Kodu: {product.urun_kodu}
);
return (
{/* Kategori listesi */}
{categories.map(category => renderCategoryItem(category))}
{/* Kategorilere göre ürün listesi */}
{Object.keys(groupedProducts).map(categoryId => (
Kategori: {categoryId}
{groupedProducts[categoryId].map(product => renderProductItem(product))}
))}
);
};
export default HomeScreen;