myVehiclePageComponent.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import { router } from 'expo-router';
  2. import {
  3. View,
  4. Text,
  5. ScrollView,
  6. Pressable,
  7. Image,
  8. Dimensions,
  9. ImageBackground
  10. } from 'react-native';
  11. import { SafeAreaView } from 'react-native-safe-area-context';
  12. import { CrossLogoSvg, GreenStarSvg, RightArrowIconSvg } from '../global/SVG';
  13. import NormalButton from '../global/normal_button';
  14. import { useEffect, useState } from 'react';
  15. import { chargeStationService } from '../../service/chargeStationService';
  16. import { authenticationService } from '../../service/authService';
  17. const VehicleRowComponent = ({
  18. deviceWidth,
  19. deviceHeight,
  20. carBrand,
  21. carModel,
  22. licensePlate,
  23. carID,
  24. capacitance,
  25. capacitance_unit,
  26. createdAt
  27. }: {
  28. deviceWidth: number;
  29. deviceHeight: number;
  30. carBrand: string;
  31. carModel: string;
  32. licensePlate: string;
  33. carID: string;
  34. capacitance: number;
  35. capacitance_unit: string;
  36. createdAt: any;
  37. }) => (
  38. <Pressable
  39. onPress={() =>
  40. router.push({
  41. pathname: 'manageVehiclePage',
  42. params: {
  43. carID: carID,
  44. capacitance: capacitance,
  45. capacitance_unit: capacitance_unit,
  46. createdAt: createdAt,
  47. carModel: carModel,
  48. licensePlate: licensePlate
  49. }
  50. })
  51. }
  52. >
  53. <View
  54. className="rounded-xl overflow-hidden mb-3"
  55. style={{
  56. width: deviceWidth * 0.9,
  57. height: deviceHeight * 0.12
  58. }}
  59. >
  60. <ImageBackground
  61. source={require('../../assets/myVehicleBackground.png')}
  62. resizeMode="cover"
  63. className="flex-1 flex-row items-center justify-between "
  64. >
  65. <Image
  66. style={{
  67. marginTop: 15,
  68. marginLeft: 5,
  69. width: deviceWidth * 0.35,
  70. height: '100%'
  71. }}
  72. resizeMode="contain"
  73. source={require('../../assets/car1.png')}
  74. className=""
  75. />
  76. <View
  77. className="flex-col "
  78. style={{
  79. paddingRight: deviceHeight < 600 ? 80 : 100
  80. }}
  81. >
  82. <Text
  83. style={{
  84. fontSize: deviceHeight < 600 ? 18 : 22,
  85. fontWeight: '600'
  86. }}
  87. >
  88. {carBrand}
  89. </Text>
  90. <Text style={{ fontSize: deviceHeight < 600 ? 14 : 18 }}>
  91. {carModel}
  92. </Text>
  93. <Text
  94. style={{
  95. fontSize: deviceHeight < 600 ? 12 : 16,
  96. color: '#02677D'
  97. }}
  98. className=" text-[#02677D]"
  99. >
  100. {licensePlate}
  101. </Text>
  102. </View>
  103. <View className="pr-3">
  104. <RightArrowIconSvg />
  105. </View>
  106. </ImageBackground>
  107. </View>
  108. </Pressable>
  109. );
  110. const MyVehiclePageComponent = () => {
  111. const { height: deviceHeight, width: deviceWidth } =
  112. Dimensions.get('window');
  113. console.log(deviceHeight, deviceWidth);
  114. const [myVehicleData, setMyVehicleData] = useState([]);
  115. const [defaultCarInfo, setDefaultCarInfo] = useState(null);
  116. const [defaultCarId, setDefaultCarId] = useState(null);
  117. const extractDefaultCarInfo = (response) => {
  118. const defaultCar = response.cars.find(
  119. (car) => car.id === response.defaultCar.id
  120. );
  121. if (defaultCar) {
  122. return {
  123. name: defaultCar.name,
  124. license_plate: defaultCar.license_plate
  125. };
  126. }
  127. return null;
  128. };
  129. useEffect(() => {
  130. const fetchData = async () => {
  131. try {
  132. const result = await chargeStationService.getUserCars();
  133. // console.log(JSON.stringify(result.data));
  134. setMyVehicleData(result.data);
  135. } catch (error) {
  136. console.log(error);
  137. }
  138. };
  139. fetchData();
  140. }, []);
  141. useEffect(() => {
  142. const fetchDefaultCar = async () => {
  143. try {
  144. const result = await authenticationService.getUserInfo();
  145. const response = result.data;
  146. const carInfo = extractDefaultCarInfo(response);
  147. setDefaultCarInfo(carInfo);
  148. setDefaultCarId(response.defaultCar.id);
  149. console.log('i am car info' + defaultCarId);
  150. } catch (error) {
  151. console.log(error);
  152. }
  153. };
  154. fetchDefaultCar();
  155. }, []);
  156. return (
  157. <SafeAreaView
  158. className="flex-1 bg-white"
  159. edges={['top', 'right', 'left']}
  160. >
  161. <ScrollView
  162. showsVerticalScrollIndicator={false}
  163. className="flex-1 mx-[5%]"
  164. >
  165. <View style={{ marginTop: 25 }}>
  166. <Pressable
  167. className="self-start"
  168. onPress={() => {
  169. if (router.canGoBack()) {
  170. router.back();
  171. } else {
  172. router.replace('/accountMainPage');
  173. }
  174. }}
  175. >
  176. <CrossLogoSvg />
  177. </Pressable>
  178. <Text
  179. style={{
  180. fontSize: 45,
  181. marginTop: 25,
  182. marginBottom: 10
  183. }}
  184. >
  185. 我的車輛
  186. </Text>
  187. </View>
  188. <View className="items-center ">
  189. <Image
  190. source={require('../../assets/car.png')}
  191. resizeMode="contain"
  192. style={{
  193. width: deviceWidth * 0.8,
  194. height: deviceHeight * 0.25
  195. }}
  196. />
  197. </View>
  198. <View className="flex-row space-x-2 ">
  199. <View
  200. style={{
  201. width: 4,
  202. height: '100%',
  203. backgroundColor: '#02677D'
  204. }}
  205. />
  206. <View>
  207. <View className="flex-row items-center">
  208. <Text className="text-lg text-[#02677D]">
  209. 預設車輛
  210. </Text>
  211. <GreenStarSvg />
  212. </View>
  213. <Text className="text-2xl">{defaultCarInfo?.name}</Text>
  214. <Text className="text-lg text-[#888888]">
  215. {defaultCarInfo?.license_plate}
  216. </Text>
  217. </View>
  218. </View>
  219. <View className="w-full h-1 my-4 bg-[#DBE4E8]" />
  220. <Text className="text-xl mb-4">其他車輛</Text>
  221. {myVehicleData
  222. .filter((car) => car.id !== defaultCarId)
  223. .map((car, index) => (
  224. <VehicleRowComponent
  225. deviceHeight={deviceHeight}
  226. deviceWidth={deviceWidth}
  227. carBrand={car.car_brand.name}
  228. carModel={car.car_type.name}
  229. licensePlate={car.license_plate}
  230. carID={car.id}
  231. capacitance={car.car_type.capacitance}
  232. capacitance_unit={car.car_type.capacitance_unit}
  233. createdAt={car.car_type.createdAt}
  234. key={index}
  235. />
  236. ))}
  237. <NormalButton
  238. title={
  239. <Text
  240. style={{
  241. fontWeight: '700',
  242. fontSize: 20,
  243. color: '#fff'
  244. }}
  245. >
  246. 新增車輛
  247. </Text>
  248. }
  249. onPress={() => router.push('addVehiclePage')}
  250. />
  251. </ScrollView>
  252. </SafeAreaView>
  253. );
  254. };
  255. export default MyVehiclePageComponent;