72 lines
2.3 KiB
TypeScript
72 lines
2.3 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 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 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>
|
|
);
|
|
}
|