import { View, Text, TouchableOpacity, ScrollView, ActivityIndicator } 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 } 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 orderState = ["active", "completed", "cancelled"]; const [initialValue, setValue] = useState(0); useEffect(() => { fetchOrders(); }, []); // 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, // Normalize status to lowercase to avoid case-sensitivity issues status: (order.status || '').toLowerCase(), // 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); } }; const renderOrderState = () => { if (loading) { return ( ); } if (error) { return ( {error} Retry ); } // Filter orders based on state const filteredOrders = orders.filter(order => { const status = order.status.toLowerCase(); if (initialValue === 0) { // Active orders are pending, processing return ['pending', 'processing'].includes(status); } else if (initialValue === 1) { // Completed orders return ['shipped', '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()} ); }