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 [selectedCategory, setSelectedCategory] = useState(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
useEffect(() => {
const fetchCategories = async () => {
try {
setLoading(true);
const response = await axios.get('https://bego-api.testmagna.com/api/categories');
setCategories(response.data);
setLoading(false);
} catch (error) {
setError('Error fetching categories');
setLoading(false);
}
};
fetchCategories();
}, []);
const handleCategoryPress = async (category) => {
try {
setLoading(true);
const response = await axios.get(`https://bego-api.testmagna.com/api/categories/${category.id}/products`);
setSelectedCategory({ ...category, products: response.data });
setLoading(false);
} catch (error) {
setError('Error fetching products for the selected category');
setLoading(false);
}
};
const handleGoBack = () => {
setError(null);
setSelectedCategory(null);
};
const renderCategoryItem = (category) => (
handleCategoryPress(category)}>
{category.title}
);
const renderProductItem = (product) => (
Ürün Adı: {product.urun_adi}
Ürün Kodu: {product.urun_kodu}
);
return (
{loading ? (
Loading...
) : error ? (
{error}
Geri
) : (
categories.map(category => renderCategoryItem(category))
)}
{selectedCategory && (
Seçilen Kategori: {selectedCategory.title}
{selectedCategory.products.map(product => renderProductItem(product))}
Geri
)}
);
};
export default HomeScreen;