noChargingOngoingPageComponent.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, useFocusEffect } from 'expo-router';
  5. import { useCallback, useEffect, useState } from 'react';
  6. import { chargeStationService } from '../../service/chargeStationService';
  7. import { useTranslation } from '../../util/hooks/useTranslation';
  8. // const RETRY_DELAY = 2000; // 2 seconds
  9. // const MAX_RETRIES = 3; // Maximum number of retry attempts
  10. const NoChargingOngoingPageComponent = () => {
  11. const { t } = useTranslation(); // 使用翻译钩子
  12. const [newAvailableConnectors, setNewAvailableConnectors] = useState<any>([]);
  13. useFocusEffect(
  14. useCallback(() => {
  15. let isMounted = true; // Simple cleanup flag
  16. const fetchAllConnectors = async () => {
  17. try {
  18. const newAvailableConnectors = await chargeStationService.NewfetchAvailableConnectors();
  19. // Only update state if component is still mounted
  20. if (isMounted) {
  21. // Sort the connectors based on stationID
  22. const sortedConnectors = [...newAvailableConnectors].sort((a, b) => {
  23. // Custom sorting order
  24. const order: Record<string, number> = {
  25. '2405311022116801000': 1, // 觀塘偉業街
  26. '2411291610329331000': 2, // 黃竹坑
  27. '2501161430118231000': 3 // 沙頭角
  28. };
  29. return (order[a.stationID] || 999) - (order[b.stationID] || 999);
  30. });
  31. setNewAvailableConnectors(sortedConnectors);
  32. }
  33. } catch (error) {
  34. console.error('Fetch error:', error);
  35. }
  36. };
  37. fetchAllConnectors();
  38. // Simple cleanup - prevents state updates if component unmounts
  39. return () => {
  40. isMounted = false;
  41. };
  42. }, []) // Add any missing dependencies here if needed
  43. );
  44. const StationRow = ({ item }: { item: any }) => {
  45. let imgObj = null;
  46. if (item.image) {
  47. imgObj = { uri: item.image };
  48. } else {
  49. imgObj = require('../../assets/dummyStationPicture.png');
  50. }
  51. return (
  52. <Pressable
  53. onPress={() => {
  54. router.push({
  55. pathname: '/resultDetailPage',
  56. params: {
  57. chargeStationAddress: item.address,
  58. chargeStationID: item.stationID,
  59. chargeStationName: item.stationName,
  60. availableConnectors: item.availableConnectors,
  61. imageSource: item.image,
  62. stationLng: item.stationLng,
  63. stationLat: item.stationLat,
  64. pricemodel_id: item.pricemodel_id
  65. }
  66. });
  67. }}
  68. >
  69. <View className="flex flex-1 flex-row w-full ">
  70. <Image style={styles.image} source={imgObj} />
  71. <View className="flex flex-col gap-2 mt-5 mr-2 flex-1">
  72. <Text
  73. style={{
  74. fontWeight: '700',
  75. color: '#02677D',
  76. fontSize: 20,
  77. flexWrap: 'wrap',
  78. flexShrink: 1
  79. }}
  80. numberOfLines={0}
  81. >
  82. {item.stationName}
  83. </Text>
  84. <View className="flex flex-row justify-between space-x-2">
  85. <Text
  86. style={{
  87. fontWeight: '400',
  88. fontSize: 16,
  89. color: '#222222',
  90. flexWrap: 'wrap',
  91. flexShrink: 1
  92. }}
  93. numberOfLines={0}
  94. >
  95. {item.address}
  96. </Text>
  97. </View>
  98. <Text
  99. style={{
  100. fontWeight: '400',
  101. fontSize: 16,
  102. color: '#888888',
  103. flexWrap: 'wrap',
  104. flexShrink: 1
  105. }}
  106. numberOfLines={0}
  107. >
  108. {t('charging.no_ongoing.available_connectors')}: {item.availableConnectors}
  109. </Text>
  110. </View>
  111. </View>
  112. </Pressable>
  113. );
  114. };
  115. return (
  116. <SafeAreaView edges={['top', 'left', 'right']} className="flex-1 bg-white">
  117. <ScrollView className="flex-1 mt-8 " nestedScrollEnabled={true} showsVerticalScrollIndicator={false}>
  118. <View>
  119. <View>
  120. <Text className="text-5xl pt-1 pb-6 mx-[5%]">{t('charging.no_ongoing.title')}</Text>
  121. <View>
  122. <Text className="text-lg mx-[5%] mb-6">{t('charging.no_ongoing.subtitle')}</Text>
  123. <View>
  124. {newAvailableConnectors?.map((item: any, index: number) => (
  125. <View key={index}>
  126. <View className="border-b border-gray-200" />
  127. <StationRow item={item} key={index} />
  128. </View>
  129. ))}
  130. </View>
  131. </View>
  132. </View>
  133. </View>
  134. </ScrollView>
  135. </SafeAreaView>
  136. );
  137. };
  138. const styles = StyleSheet.create({
  139. grayColor: {
  140. color: '#888888'
  141. },
  142. greenColor: {
  143. color: '#02677D'
  144. },
  145. image: {
  146. width: 100,
  147. height: 100,
  148. margin: 15,
  149. borderRadius: 10
  150. }
  151. });
  152. export default NoChargingOngoingPageComponent;