freshyo/apps/user-ui/components/OrderStatusTracker.tsx
2026-10-18 21:51:01 +05:30

78 lines
2.5 KiB
TypeScript

import { View } from 'react-native';
import { tw, MyText } from 'common-ui';
const STAGES = ['Placed', 'Packed', 'Delivered'] as const;
export type OrderStage = (typeof STAGES)[number];
/**
* Maps a backend deliveryStatus to the furthest stage reached:
* pending / processing -> 0 (Placed)
* packaged -> 1 (Packed)
* success / delivered -> 2 (Delivered)
* Anything else (incl. cancelled) falls back to 0.
*/
export function deliveryStageIndex(deliveryStatus?: string | null): number {
switch ((deliveryStatus || '').toLowerCase()) {
case 'packaged':
return 1;
case 'success':
case 'delivered':
return 2;
default:
return 0;
}
}
interface OrderStatusTrackerProps {
deliveryStatus?: string | null;
activeColor?: string;
inactiveColor?: string;
}
export default function OrderStatusTracker({
deliveryStatus,
activeColor = '#10B981',
inactiveColor = '#E2E8F0',
}: OrderStatusTrackerProps) {
const cancelled = (deliveryStatus || '').toLowerCase() === 'cancelled';
const currentIndex = cancelled ? -1 : deliveryStageIndex(deliveryStatus);
return (
<View testID="order-status-tracker" style={tw`flex-row`}>
{STAGES.map((label, i) => {
const reached = i <= currentIndex;
const dotColor = reached ? activeColor : inactiveColor;
const leftLine = i > 0 && i <= currentIndex ? activeColor : inactiveColor;
const rightLine = i < STAGES.length - 1 && i < currentIndex ? activeColor : inactiveColor;
return (
<View key={label} style={tw`flex-1 items-center`}>
<View style={tw`flex-row items-center w-full`}>
<View
testID={`order-status-line-left-${label.toLowerCase()}`}
style={[tw`flex-1 h-0.5 rounded-full`, { backgroundColor: leftLine }]}
/>
<View
testID={`order-status-dot-${label.toLowerCase()}`}
style={[tw`w-3 h-3 rounded-full`, { backgroundColor: dotColor }]}
/>
<View
testID={`order-status-line-right-${label.toLowerCase()}`}
style={[tw`flex-1 h-0.5 rounded-full`, { backgroundColor: rightLine }]}
/>
</View>
<MyText
style={[
tw`text-[9px] font-bold mt-1`,
{ color: reached && !cancelled ? activeColor : '#94A3B8' },
]}
>
{label}
</MyText>
</View>
);
})}
</View>
);
}