| | |
| | | import './index.less'; |
| | | import closeIcon from '../../../../assets/close-icon.png'; |
| | | import selectTypeLineIcon from '../../../../assets/select-type-line.png'; |
| | | import { queryRoutePreferences } from '../../../../api/begin-route'; |
| | | |
| | | export type RoutePreferenceOption = { |
| | | label: string; |
| | | value: string; |
| | | }; |
| | | |
| | | type Props = { |
| | | show: boolean; |
| | | value: string; |
| | | openCityId?: number; |
| | | onClose: () => void; |
| | | onSelect: (item: string) => void; |
| | | onSelect: (item: RoutePreferenceOption) => void; |
| | | }; |
| | | |
| | | const OPTIONS = [ |
| | | const DEFAULT_OPTIONS: RoutePreferenceOption[] = [ |
| | | { label: '智能推荐', value: '智能推荐' }, |
| | | { label: '时间优先', value: '时间优先' }, |
| | | { label: '低价优先', value: '低价优先' } |
| | | ]; |
| | | |
| | | export default function RoutePreferencePopup({ show, value, onClose, onSelect }: Props) { |
| | | const normalizeOptions = (res: any): RoutePreferenceOption[] => { |
| | | const list = Array.isArray(res) |
| | | ? res |
| | | : Array.isArray(res?.list) |
| | | ? res.list |
| | | : Array.isArray(res?.data) |
| | | ? res.data |
| | | : Array.isArray(res?.routePreferences) |
| | | ? res.routePreferences |
| | | : Array.isArray(res?.preferences) |
| | | ? res.preferences |
| | | : []; |
| | | |
| | | const normalized = list.map((item: any) => { |
| | | const label = item?.label || item?.name || item?.displayName || item?.preferenceName || item?.value; |
| | | const value = item?.value ?? item?.code ?? item?.preferenceCode ?? label; |
| | | if (!label || value === undefined || value === null) { |
| | | return null; |
| | | } |
| | | return { |
| | | label: String(label), |
| | | value: String(value) |
| | | }; |
| | | }).filter(Boolean) as RoutePreferenceOption[]; |
| | | |
| | | return normalized.length > 0 ? normalized : DEFAULT_OPTIONS; |
| | | }; |
| | | |
| | | export default function RoutePreferencePopup({ show, value, openCityId = 0, onClose, onSelect }: Props) { |
| | | const [visible, setVisible] = useState(false); |
| | | const [animationData, setAnimationData] = useState<any>(); |
| | | const [options, setOptions] = useState<RoutePreferenceOption[]>(DEFAULT_OPTIONS); |
| | | |
| | | useEffect(() => { |
| | | if (show && !visible) { |
| | |
| | | } |
| | | }, [show]); |
| | | |
| | | useEffect(() => { |
| | | if (!show) return; |
| | | let cancelled = false; |
| | | |
| | | queryRoutePreferences({ openCityId }).then((res: any) => { |
| | | if (cancelled) return; |
| | | setOptions(normalizeOptions(res)); |
| | | }).catch(() => { |
| | | if (cancelled) return; |
| | | setOptions(DEFAULT_OPTIONS); |
| | | }); |
| | | |
| | | return () => { |
| | | cancelled = true; |
| | | }; |
| | | }, [show, openCityId]); |
| | | |
| | | if (!visible && !show) return null; |
| | | |
| | | return ( |
| | |
| | | </View> |
| | | <View className='popup-list-wrap'> |
| | | <View className='popup-list-inner'> |
| | | {OPTIONS.map((item, index) => { |
| | | {options.map((item, index) => { |
| | | const isSelected = item.value === value; |
| | | return ( |
| | | <View |
| | | key={item.value || index} |
| | | className={`popup-item ${isSelected ? 'active' : ''}`} |
| | | onClick={() => { |
| | | onSelect(item.value); |
| | | onSelect(item); |
| | | onClose(); |
| | | }} |
| | | > |