import { View, Text, TouchableOpacity, ScrollView, ActivityIndicator, RefreshControl } from 'react-native' import React, { useState, useEffect } from 'react' import TabBar from '../themes/TabBar' import { ChevronLeftIcon } from 'react-native-heroicons/outline' import { widthPercentageToDP as wp, heightPercentageToDP as hp } from 'react-native-responsive-screen'; import ActiveOrder from '../components/ActiveOrder' import CompletedOrder from '../components/CompletedOrder' import CancelledOrder from '../components/CancelledOrder' import { useNavigation, useFocusEffect } from '@react-navigation/native' import { getOrders } from '../api' export default function OrderScreen() { const navigation = useNavigation(); const [orders, setOrders] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [refreshing, setRefreshing] = useState(false); const orderState = ["active", "completed", "cancelled"]; const [initialValue, setValue] = useState(0); // Process order data to ensure consistent structure const processOrderData = (orderData) => { if (!Array.isArray(orderData)) { console.warn('Expected orders to be an array, got:', typeof orderData); return []; } return orderData.map(order => ({ ...order, // Keep status as is (case-sensitive) status: order.status || 'Pending', // Ensure items array exists items: Array.isArray(order.items) ? order.items : [], // Ensure order has an ID id: order.id || Math.floor(Math.random() * 10000) })); }; const fetchOrders = async () => { try { setLoading(true); const orderData = await getOrders(); const processedOrders = processOrderData(orderData); setOrders(processedOrders); setError(null); } catch (error) { console.error('Error fetching orders:', error); setError('Failed to load orders'); } finally { setLoading(false); } }; // Initial fetch useEffect(() => { fetchOrders(); }, []); // Refresh when screen is focused useFocusEffect( React.useCallback(() => { fetchOrders(); }, []) ); // Handle pull-to-refresh const onRefresh = async () => { setRefreshing(true); try { await fetchOrders(); } catch (error) { console.error('Error refreshing orders:', error); } finally { setRefreshing(false); } }; const renderOrderState = () => { if (loading && !refreshing) { return ( ); } if (error) { return ( {error} Retry ); } // Filter orders based on state const filteredOrders = orders.filter(order => { const status = order.status; if (initialValue === 0) { // Active orders are pending, processing, and shipped return ['Pending', 'Processing', 'Shipped'].includes(status); } else if (initialValue === 1) { // Completed orders return ['Delivered'].includes(status); } else { // Cancelled orders return status === 'Cancelled'; } }); if (filteredOrders.length === 0) { return ( No {orderState[initialValue]} orders found ); } switch (initialValue) { case 0: return ; case 1: return ; case 2: return ; default: return This Order State is not Valid; } }; return ( } titleStyle={{ color: "black", fontWeight: "bold", fontSize: 20 }} prefixStyle={{ backgroundColor: "transparent" }} suffixStyle={{ backgroundColor: "white", borderWidth: 1, borderColor: "black" }} containerStyle="" suffixAction={() => navigation.goBack()} /> { orderState.map((item, index) => { const isActive = initialValue == index; return ( setValue(index)} key={index} className={`flex-1 flex-row justify-center items-center py-4 border-b-[4px] rounded-[2px] ${isActive ? " border-primary" : "border-white"} `} > {item} ); }) } } > {renderOrderState()} ); }