noChargingOngoingPageComponent.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import { View, Text, ScrollView, StyleSheet, ActivityIndicator, Pressable, Image } from 'react-native';
  2. import { SafeAreaView } from 'react-native-safe-area-context';
  3. import NormalButton from '../global/normal_button';
  4. import { router } from 'expo-router';
  5. import { useEffect, useState } from 'react';
  6. import { chargeStationService } from '../../service/chargeStationService';
  7. const RETRY_DELAY = 2000; // 2 seconds
  8. const MAX_RETRIES = 3; // Maximum number of retry attempts
  9. const NoChargingOngoingPageComponent = () => {
  10. //fetch charge station info
  11. const [chargeStationInfo, setChargeStationInfo] = useState([]);
  12. const [availableConnectors, setAvailableConnectors] = useState({});
  13. // useEffect(() => {
  14. // const fetchChargeStationInfo = async () => {
  15. // try {
  16. // const chargeStationInfo = await chargeStationService.fetchAllChargeStations();
  17. // console.log('chargeStationInfo in noChargingOngoingPageComponent', chargeStationInfo);
  18. // const onlyWaiYipStreetStation = chargeStationInfo.filter(
  19. // (station) => station.id == '2405311022116801000'
  20. // );
  21. // setChargeStationInfo(onlyWaiYipStreetStation);
  22. // // setChargeStationInfo(chargeStationInfo);
  23. // } catch (error) {
  24. // console.error('Error fetching charge station info:', error);
  25. // }
  26. // };
  27. // fetchChargeStationInfo();
  28. // }, []);
  29. useEffect(() => {
  30. const fetchChargeStationInfo = async () => {
  31. try {
  32. const chargeStationInfo = await chargeStationService.fetchAllChargeStations();
  33. setChargeStationInfo(chargeStationInfo);
  34. } catch (error) {
  35. console.error('Error fetching charge station info:', error);
  36. }
  37. };
  38. fetchChargeStationInfo();
  39. }, []);
  40. const fetchConnectorWithRetry = async (stationId, retryCount = 0) => {
  41. try {
  42. const response = await chargeStationService.fetchAvailableConnectors(stationId);
  43. if (response === 500 || !response) {
  44. throw new Error('Server error');
  45. }
  46. return response;
  47. } catch (error) {
  48. if (retryCount < MAX_RETRIES) {
  49. console.log(`Retrying fetch for station ${stationId}, attempt ${retryCount + 1}`);
  50. await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
  51. return fetchConnectorWithRetry(stationId, retryCount + 1);
  52. }
  53. console.error(`Failed to fetch connectors for station ${stationId} after ${MAX_RETRIES} attempts:`, error);
  54. return null;
  55. }
  56. };
  57. useEffect(() => {
  58. const fetchAllConnectors = async () => {
  59. try {
  60. const connectorData = {};
  61. for (const station of chargeStationInfo) {
  62. const stationConnectors = await fetchConnectorWithRetry(station.id);
  63. if (stationConnectors !== null) {
  64. connectorData[station.id] = stationConnectors;
  65. // Update state immediately for each successful fetch
  66. setAvailableConnectors((prev) => ({
  67. ...prev,
  68. [station.id]: stationConnectors
  69. }));
  70. }
  71. }
  72. } catch (error) {
  73. console.error('Error fetching connectors:', error);
  74. }
  75. };
  76. if (chargeStationInfo.length > 0) {
  77. fetchAllConnectors();
  78. }
  79. }, [chargeStationInfo]);
  80. const StationRow = ({ item }) => (
  81. <Pressable
  82. onPress={() => {
  83. router.push({
  84. pathname: '/resultDetailPage',
  85. params: {
  86. chargeStationAddress: item.snapshot.Address,
  87. chargeStationID: item.id,
  88. chargeStationName: item.snapshot.StationName,
  89. availableConnectors: availableConnectors[item.id]
  90. }
  91. });
  92. }}
  93. >
  94. <View className="flex flex-1 flex-row w-full ">
  95. <Image style={styles.image} source={require('../../assets/dummyStationPicture.png')} />
  96. <View className="flex flex-col gap-2 mt-5 mr-2">
  97. <Text
  98. style={{
  99. fontWeight: '700',
  100. color: '#02677D',
  101. fontSize: 20
  102. }}
  103. >
  104. {item.snapshot.StationName}
  105. </Text>
  106. <View className="flex flex-row justify-between space-x-2">
  107. <Text
  108. style={{
  109. fontWeight: '400',
  110. fontSize: 16,
  111. color: '#222222'
  112. }}
  113. >
  114. {item.snapshot.Address}
  115. </Text>
  116. {/* <Text
  117. style={{
  118. fontWeight: '400',
  119. fontSize: 16,
  120. color: '#222222'
  121. }}
  122. >
  123. 已付金額:{' '}
  124. {item.actual_fee
  125. ? item.actual_fee % 1 === 0
  126. ? `$${item.actual_fee}`
  127. : `$${item.actual_fee.toFixed(1)}`
  128. : ''}
  129. </Text> */}
  130. </View>
  131. {!availableConnectors[item.id] && (
  132. <Text
  133. style={{
  134. fontWeight: '400',
  135. fontSize: 16,
  136. color: '#888888'
  137. }}
  138. >
  139. 正在查詢可用充電槍數目...
  140. </Text>
  141. )}
  142. {availableConnectors[item.id] && (
  143. <Text
  144. style={{
  145. fontWeight: '400',
  146. fontSize: 16,
  147. color: '#888888'
  148. }}
  149. >
  150. 現時可用充電槍數目: {availableConnectors[item.id]}
  151. </Text>
  152. )}
  153. </View>
  154. </View>
  155. </Pressable>
  156. );
  157. return (
  158. <SafeAreaView edges={['top', 'left', 'right']} className="flex-1 bg-white">
  159. <ScrollView className="flex-1 mt-8 " nestedScrollEnabled={true} showsVerticalScrollIndicator={false}>
  160. <View className="">
  161. <View className="">
  162. <Text className="text-5xl pt-1 pb-6 mx-[5%]">暫無正在進行的充電</Text>
  163. <View>
  164. <Text className="text-lg mx-[5%] mb-6">立刻前往Crazy Charge 充電站充電吧!</Text>
  165. <View className="">
  166. {chargeStationInfo?.map((item, index) => (
  167. <View key={index}>
  168. <View className=" border-b border-gray-200" />
  169. <StationRow item={item} key={index} />
  170. </View>
  171. ))}
  172. {/* <NormalButton
  173. title={<Text className="text-white text-lg">前往預約頁面</Text>}
  174. onPress={() => router.push('/bookingMenuPage')}
  175. /> */}
  176. </View>
  177. </View>
  178. </View>
  179. </View>
  180. </ScrollView>
  181. </SafeAreaView>
  182. );
  183. };
  184. const styles = StyleSheet.create({
  185. grayColor: {
  186. color: '#888888'
  187. },
  188. greenColor: {
  189. color: '#02677D'
  190. },
  191. image: {
  192. width: 100,
  193. height: 100,
  194. margin: 15,
  195. borderRadius: 10
  196. }
  197. });
  198. export default NoChargingOngoingPageComponent;
  199. // const styles = StyleSheet.create({
  200. // container: {
  201. // flexDirection: 'row',
  202. // width: '100%',
  203. // flex: 1
  204. // },
  205. // textContainer: {
  206. // flex: 1,
  207. // flexDirection: 'column',
  208. // gap: 8,
  209. // marginTop: 20,
  210. // marginRight: 8 // Add right margin to prevent text from touching the edge
  211. // }
  212. // });