import React, { useEffect, useState } from 'react'; import { View, Text, Image, Pressable, StyleSheet, ScrollView, ActivityIndicator } from 'react-native'; import { FlashList } from '@shopify/flash-list'; import { router } from 'expo-router'; import { SafeAreaView } from 'react-native-safe-area-context'; import useVehicleStore from '../../providers/vehicle_store'; import { chargeStationService } from '../../service/chargeStationService'; import { PreviousPageBlackSvg } from '../../component/global/SVG'; interface AlphabetSection { letter: string; brands: CarBrand[]; } interface CarBrand { id: number; name: string; img_url: string; car_types: any[]; } const brandLogos = { audi: require('../../assets/audi.png'), benz: require('../../assets/benz.png'), bmw: require('../../assets/bmw.png'), byd: require('../../assets/byd.png'), lexus: require('../../assets/lexus.png'), mg: require('../../assets/mg.png'), porsche: require('../../assets/porsche.png'), tesla: require('../../assets/tesla.png'), toyota: require('../../assets/toyota.png'), xiaomi: require('../../assets/xiaomi.png') // Add all other brands here }; const RegisterChooseVehiclesOne = () => { const [loading, setLoading] = useState(true); const [processedCarData, setProcessedCarData] = useState([]); const { vehicleBrand, vehicleModel, BrandID, ModelID, setVehicleBrand, setBrandID } = useVehicleStore(); useEffect(() => { const fetchCarBrand = async () => { try { const response = await chargeStationService.fetchCarBrand(); if (response) { setProcessedCarData(response.data); } } catch (error) { console.log(error); } finally { setLoading(false); } }; fetchCarBrand(); }, []); useEffect(() => { console.log('Zustand VehicleBrand', vehicleBrand); console.log('Zustand BrandID', BrandID); console.log('Zustand VehicleModel', vehicleModel); console.log('Zustand ModelID', ModelID); }, [processedCarData]); const renderBrandItem = ({ item: brand }: { item: CarBrand }) => ( { setVehicleBrand(brand.name); setBrandID(brand.id); router.push({ pathname: 'registerChooseVehiclesTwo', params: { brandId: brand.id.toString(), brandName: brand.name, carTypes: JSON.stringify(brand.car_types) } }); }} > {brand.name} ); const renderAlphabetSection = ({ item: section }: { item: AlphabetSection }) => ( {section.letter} {section.brands.map((brand) => renderBrandItem({ item: brand }))} ); return ( { if (router.canGoBack()) { router.back(); } else { router.replace('/accountMainPage'); } }} > 選擇品牌 {loading ? ( ) : ( a.name.localeCompare(b.name)) .reduce((acc, brand) => { const letter = brand.name[0].toUpperCase(); const existingSection = acc.find((section) => section.letter === letter); if (existingSection) { existingSection.brands.push(brand); } else { acc.push({ letter, brands: [brand] }); } return acc; }, [] as AlphabetSection[])} renderItem={renderAlphabetSection} keyExtractor={(item) => item.letter} /> )} ); }; const styles = StyleSheet.create({ letterHeader: { backgroundColor: '#E3F2F8', padding: 10, marginVertical: 10 }, letterText: { color: '#02677D', fontSize: 14, fontWeight: 'bold' }, brandRow: { flexDirection: 'row', flexWrap: 'wrap' }, brandItem: { width: '33.33%', alignItems: 'center', padding: 10 }, logo: { width: 80, height: 80 }, brandName: { marginTop: 5, textAlign: 'center' } }); export default RegisterChooseVehiclesOne; // interface AlphabetSection { // letter: string; // brands: CarBrand[]; // } // import React, { useEffect, useState } from 'react'; // import { View, Text, Image, Pressable, StyleSheet, ScrollView, ActivityIndicator } from 'react-native'; // import { FlashList } from '@shopify/flash-list'; // import { router } from 'expo-router'; // import { SafeAreaView } from 'react-native-safe-area-context'; // import useVehicleStore from '../../providers/vehicle_store'; // import { chargeStationService } from '../../service/chargeStationService'; // import { PreviousPageBlackSvg } from '../../component/global/SVG'; // const RegisterChooseVehiclesOne = () => { // const [loading, setLoading] = useState(true); // const [carData, setCarData] = useState(); // const [extractedCarNameAndImgUrl, setExtractedCarNameAndImgUrl] = useState(''); // const [processedCarData, setProcessedCarData] = useState([]); // const { // vehicleBrand, // vehicleModel, // BrandID, // ModelID, // licensePlate, // setVehicleBrand, // setVehicleModel, // setBrandID, // setModelID, // setLicensePlate // } = useVehicleStore(); // //this uses getCarBrand to create an array of object with 2 keys: name and img_url // useEffect(() => { // const fetchCarBrand = async () => { // try { // const response = await chargeStationService.fetchCarBrand(); // if (response) { // const processedData = await Promise.all( // response.data.map(async (car) => ({ // ...car, // logo: await chargeStationService.getProcessedCarImageUrl(car.img_url) // })) // ); // setProcessedCarData(processedData); // } // } catch (error) { // console.log(error); // } finally { // setLoading(false); // } // }; // fetchCarBrand(); // }, []); // useEffect(() => { // console.log('Zustand VehicleBrand', vehicleBrand); // console.log('Zustand BrandID', BrandID); // console.log('Zustand VehicleModel', vehicleModel); // console.log('Zustand ModelID', ModelID); // }, [processedCarData]); // const renderBrandItem = ({ item: brand }: { item }) => ( // { // setVehicleBrand(brand.name); // setBrandID(brand.id); // router.push({ // pathname: 'registerChooseVehiclesTwo', // params: { // brandId: brand.id.toString(), // brandName: brand.name, // carTypes: JSON.stringify(brand.car_types) // } // }); // }} // > // // {brand.name} // // ); // const renderAlphabetSection = ({ item: section }: { item: AlphabetSection }) => ( // // // {section.letter} // // {section.brands.map((brand) => renderBrandItem({ item: brand }))} // // ); // return ( // // // // { // if (router.canGoBack()) { // router.back(); // } else { // router.replace('/accountMainPage'); // } // }} // > // // // // 選擇品牌 // // // {loading ? ( // // // // ) : ( // // a.name.localeCompare(b.name)) // .reduce((acc, brand) => { // const letter = brand.name[0].toUpperCase(); // const existingSection = acc.find((section) => section.letter === letter); // if (existingSection) { // existingSection.brands.push(brand); // } else { // acc.push({ letter, brands: [brand] }); // } // return acc; // }, [] as AlphabetSection[])} // renderItem={renderAlphabetSection} // keyExtractor={(item) => item.letter} // /> // // )} // {/* // // { // const letter = brand.name[0].toUpperCase(); // const existingSection = acc.find((section) => section.letter === letter); // if (existingSection) { // existingSection.brands.push(brand); // } else { // acc.push({ letter, brands: [brand] }); // } // return acc; // }, [] as AlphabetSection[])} // renderItem={renderAlphabetSection} // keyExtractor={(item) => item.letter} // /> // */} // // // ); // }; // const styles = StyleSheet.create({ // letterHeader: { // backgroundColor: '#E3F2F8', // padding: 10, // marginVertical: 10 // }, // letterText: { // color: '#02677D', // fontSize: 14, // fontWeight: 'bold' // }, // brandRow: { // flexDirection: 'row', // flexWrap: 'wrap' // }, // brandItem: { // width: '33.33%', // alignItems: 'center', // padding: 10 // }, // logo: { // width: 80, // height: 80 // }, // brandName: { // marginTop: 5, // textAlign: 'center' // } // }); // export default RegisterChooseVehiclesOne;