import React, { useCallback, useEffect, useState } from 'react';
import { View, Text, ScrollView, Pressable, Image, Dimensions, ActivityIndicator } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { router } from 'expo-router';
import { CrossLogoSvg, GreenStarSvg, RightArrowIconSvg } from '../global/SVG';
import NormalButton from '../global/normal_button';
import { chargeStationService } from '../../service/chargeStationService';
import { authenticationService } from '../../service/authService';
const truncateText = (text: any, maxLength: any) => {
if (text.length <= maxLength) return text;
return text.substr(0, maxLength) + '...';
};
const VehicleRow = ({ car, deviceWidth, deviceHeight, onPress }) => (
{car.car_brand.name}
{truncateText(car.car_type.name, 10)}
{car?.license_plate}
);
const MyVehiclePageComponent = () => {
const { height: deviceHeight, width: deviceWidth } = Dimensions.get('window');
const [vehicles, setVehicles] = useState([]);
const [defaultCar, setDefaultCar] = useState(null);
const [processedUrls, setProcessedUrls] = useState({});
const [isLoading, setIsLoading] = useState(false);
const fetchData = useCallback(async () => {
setIsLoading(true);
try {
const [carsResult, userInfoResult] = await Promise.all([
chargeStationService.getUserCars(),
authenticationService.getUserInfo()
]);
let updatedVehicles = [...carsResult.data];
for (let i = 0; i < updatedVehicles.length; i++) {
const car = updatedVehicles[i];
const processedUrl = await chargeStationService.getProcessedImageUrl(car.car_type.type_image_url);
updatedVehicles[i] = {
...car,
processedImageUrl: processedUrl
};
}
setVehicles(updatedVehicles);
//all car data
//find default from all car data
const defaultCarId = userInfoResult.data.defaultCar.id;
const defaultCar = updatedVehicles.find((car) => car.id === defaultCarId);
setDefaultCar(defaultCar);
//find processed Image urls
} catch (error) {
console.error('Error fetching data:', error);
} finally {
setIsLoading(false);
}
}, []);
function repeat(char, times) {
return new Array(times + 1).join(char);
}
// Use the function
useEffect(() => {
fetchData();
}, [fetchData]);
const otherVehicles = vehicles.filter((car) => car.id !== defaultCar?.id);
// console.log(repeat('-', 50));
// console.log(otherVehicles);
return (
{isLoading ? (
) : (
<>
router.canGoBack() ? router.back() : router.replace('/accountMainPage')
}
>
我的車輛
{defaultCar && (
預設車輛
{defaultCar.car_type.name}
{defaultCar.license_plate}
)}
其他車輛
{otherVehicles.map((car) => (
router.push({
pathname: 'manageVehiclePage',
params: {
carID: car.id,
capacitance: car.car_type.capacitance,
capacitance_unit: car.car_type.capacitance_unit,
createdAt: car.car_type.createdAt,
carModel: car.car_type.name,
licensePlate: car.license_plate,
carImage: car.processedImageUrl
}
})
}
/>
))}
新增車輛}
onPress={() => router.push('addVehiclePage')}
/>
>
)}
//
//
//
// (router.canGoBack() ? router.back() : router.replace('/accountMainPage'))}
// >
//
//
// 我的車輛
//
// {isLoading ? (
//
//
//
// ) : (
// <>
//
//
//
// {defaultCar && (
//
//
//
//
// 預設車輛
//
//
// {defaultCar.car_type.name}
// {defaultCar.license_plate}
//
//
// )}
//
// 其他車輛
// {otherVehicles.map((car) => (
//
// router.push({
// pathname: 'manageVehiclePage',
// params: {
// carID: car.id,
// capacitance: car.car_type.capacitance,
// capacitance_unit: car.car_type.capacitance_unit,
// createdAt: car.car_type.createdAt,
// carModel: car.car_type.name,
// licensePlate: car.license_plate,
// carImage: car.processedImageUrl
// }
// })
// }
// />
// ))}
// 新增車輛}
// onPress={() => router.push('addVehiclePage')}
// />
// >
// )}
//
//
);
};
export default MyVehiclePageComponent;
function repeat(arg0: string, arg1: number): any {
throw new Error('Function not implemented.');
}