import { ActivityIndicator, ScrollView, Text, TextInput, View, RefreshControl } from "react-native";
import { AdjustmentsHorizontalIcon, BellIcon, ChevronDownIcon, MagnifyingGlassIcon } from "react-native-heroicons/outline"
import { MapPinIcon } from "react-native-heroicons/micro"
import { widthPercentageToDP as wp, heightPercentageToDP as hp } from 'react-native-responsive-screen';
import Category from "../components/Category";
import FlashSales from "../components/FlashSales";
import { TouchableOpacity } from "react-native";
import { useNavigation } from "@react-navigation/native";
import HomeCarousel from "../components/HomeCarousel";
import { useEffect, useState, useCallback } from "react";
import { fetchCategories, fetchProducts } from "../api";
export default function HomeScreen() {
const navigation = useNavigation()
const [page, setPage] = useState(0);
const [products, setProducts] = useState([]);
const [categories, setCategories] = useState([]);
const [loading, setLoading] = useState(true);
const [refreshing, setRefreshing] = useState(false);
const [error, setError] = useState(null);
const loadData = async () => {
try {
// Fetch both products and categories
const [productsData, categoriesData] = await Promise.all([
fetchProducts(),
fetchCategories()
]);
setProducts(productsData);
setCategories(categoriesData);
setError(null);
} catch (error) {
console.error('Error fetching data:', error);
setError(error.message || 'Failed to fetch data');
} finally {
setLoading(false);
setRefreshing(false);
}
};
useEffect(() => {
loadData();
}, []);
const onRefresh = useCallback(() => {
setRefreshing(true);
loadData();
}, []);
if(loading && !refreshing){
return (
)
}
if(error){
return (
{error}
{
setLoading(true);
loadData();
}}
className="mt-4 bg-primary py-2 px-4 rounded-lg"
>
Retry
)
}
const slider = [
{
title: "new collection",
description: "Discount 50% for the first transaction."
},
{
title: "kids collection",
description: "Discount 50% for the first transaction."
},
{
title: "traditional collection",
description: "Discount 50% for the first transaction."
},
{
title: "traditional collection",
description: "Discount 50% for the first transaction."
},
{
title: "traditional collection",
description: "Discount 50% for the first transaction."
},
]
return (
}
>
{/* header start */}
Location
Kohalpur-11,Banke
{ navigation.navigate('Notification') }} className="bg-gray-300 h-14 w-14 rounded-full flex justify-center items-center">
{/* header end */}
{/* search bar start */}
navigation.navigate('Search')}
className="flex-row items-center flex-1 border border-primary px-3 h-14 rounded-full"
>
Search products
{ navigation.navigate('Filter') }} className="bg-primary h-14 w-14 rounded-full flex justify-center items-center">
{/* search bar start */}
{/* ads component start */}
{Array.from({ length: slider.length }).map((_, index) => {
const isActive = index == page
return (
)
})}
{/* ads component end */}
{/* Pass categories data to the Category component */}
{products.length > 0 ? (
) : (
No products available
)}
);
}