chargingHurryUpPageComponent.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { View, Text, ScrollView, StyleSheet, ActivityIndicator, Alert, Modal } from 'react-native';
  2. import React, { useEffect, useState } from 'react';
  3. import { SafeAreaView } from 'react-native-safe-area-context';
  4. import NormalButton from '../global/normal_button';
  5. import { router } from 'expo-router';
  6. import { chargeStationService } from '../../service/chargeStationService';
  7. import { format, addHours } from 'date-fns';
  8. import { toZonedTime } from 'date-fns-tz';
  9. import { convertToHKTime } from '../../util/lib';
  10. const ChargingHurryUpPageComponent = ({ data = {} }) => {
  11. // ***************************************************************
  12. // 2 整個useEffect listen to data. 有新data就轉頁
  13. // ***************************************************************
  14. const reservationData = Array.isArray(data) ? data[0] : data;
  15. const [stationInfo, setStationInfo] = useState(null);
  16. const [loading, setLoading] = useState(false);
  17. const [isLoading, setIsLoading] = useState(false);
  18. const plan = reservationData.book_time === reservationData.end_time ? '充滿停機' : '按每度電結算';
  19. const kwh = reservationData.total_power === null ? '' : `${reservationData.total_power} kWh`;
  20. useEffect(() => {
  21. setLoading(true);
  22. if (
  23. reservationData &&
  24. reservationData.connector &&
  25. reservationData.connector.EquipmentID &&
  26. reservationData.connector.EquipmentID.StationID
  27. ) {
  28. try {
  29. const parsedSnapshot = JSON.parse(reservationData.connector.EquipmentID.StationID.snapshot);
  30. setStationInfo(parsedSnapshot);
  31. } catch (error) {
  32. console.error('Error parsing station snapshot:', error);
  33. }
  34. }
  35. setLoading(false);
  36. }, [reservationData]);
  37. const startPayload = {
  38. StartChargeSeq: reservationData.format_order_id,
  39. ConnectorID: reservationData.connector.ConnectorID,
  40. StopBy: 2,
  41. StopValue: 120,
  42. StartBalance: 400.99
  43. };
  44. const handleStartCharge = async () => {
  45. setIsLoading(true);
  46. try {
  47. const response = await chargeStationService.startCharging(startPayload);
  48. if (response) {
  49. setIsLoading(false);
  50. Alert.alert('啟動成功', '請稍後等待頁面自動跳轉至充電介面', [{ text: 'OK', onPress: () => {} }]);
  51. setTimeout(() => {
  52. router.push('chargingPage');
  53. }, 15000);
  54. } else {
  55. setIsLoading(false);
  56. Alert.alert('啟動失敗');
  57. }
  58. } catch (error) {
  59. setIsLoading(false);
  60. Alert.alert('發生錯誤', '請稍後再試');
  61. }
  62. };
  63. return (
  64. <SafeAreaView edges={['top', 'left', 'right']} className="flex-1 bg-white">
  65. <ScrollView className="flex-1 mt-8 " nestedScrollEnabled={true} showsVerticalScrollIndicator={false}>
  66. <View className="mx-[5%]">
  67. <View className="">
  68. <Text className="text-5xl pt-1 pb-6">預約已經開始</Text>
  69. <Text className="text-base">到達充電站後,按下方按鈕開始充電。</Text>
  70. {loading ? (
  71. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  72. <ActivityIndicator color="#34657b" />
  73. </View>
  74. ) : (
  75. <>
  76. <Text className="text-2xl py-4 ">你的預約為:</Text>
  77. <View className="border-gray-200 border rounded-md">
  78. <View className="flex-1 mt-4 mx-[5%] ">
  79. <View className="flex-1 flex-row items-center pb-3">
  80. <View className="flex-1 flex-column">
  81. <Text style={styles.grayColor} className="text-base">
  82. 日期
  83. </Text>
  84. <Text style={styles.greenColor} className="text-4xl text-center pt-2">
  85. {convertToHKTime(reservationData.book_time)
  86. .hkDate.split('/')
  87. .reverse()
  88. .slice(1)
  89. .join('月')}
  90. </Text>
  91. </View>
  92. <View className="flex-1 flex-column">
  93. <Text style={styles.grayColor} className="text-base pl-7">
  94. 時間
  95. </Text>
  96. <Text style={styles.greenColor} className="text-4xl text-center pt-2">
  97. {convertToHKTime(reservationData.book_time).hkTime.slice(0, 5)}
  98. </Text>
  99. </View>
  100. </View>
  101. <View className="flex-1 flex-column justify-center space-y-1 pb-3">
  102. <Text style={styles.grayColor} className="text-base">
  103. 充電地點
  104. </Text>
  105. {loading ? (
  106. <ActivityIndicator size="small" />
  107. ) : (
  108. <>
  109. <Text style={styles.greenColor} className="text-2xl ">
  110. {stationInfo?.StationName || 'N/A'}
  111. </Text>
  112. <Text style={styles.grayColor} className="text-sm">
  113. {stationInfo?.Address || 'N/A'}
  114. </Text>
  115. </>
  116. )}
  117. </View>
  118. <View className="flex-1 flex-row pb-3 ">
  119. <View className="flex-column flex-1">
  120. <Text style={styles.grayColor} className="text-base">
  121. 方案
  122. </Text>
  123. <Text style={styles.greenColor} className="text-lg">
  124. {plan}
  125. </Text>
  126. <Text style={styles.grayColor} className="text-sm">
  127. {kwh}
  128. </Text>
  129. </View>
  130. <View className="flex-column flex-1">
  131. <Text style={styles.grayColor} className="text-base">
  132. 車輛
  133. </Text>
  134. <Text style={styles.greenColor} className="text-lg">
  135. {reservationData.car.car_brand.name}
  136. </Text>
  137. <Text style={styles.greenColor} className="text-lg">
  138. {reservationData.car.car_type.name}
  139. </Text>
  140. </View>
  141. </View>
  142. </View>
  143. </View>
  144. <View className="pt-6">
  145. <NormalButton
  146. title={<Text className="text-white text-lg">發動充電樁 - 開始充電</Text>}
  147. onPress={() => handleStartCharge()}
  148. />
  149. </View>
  150. <View className="pt-6">
  151. <NormalButton
  152. title={<Text className="text-white text-lg">返回主頁</Text>}
  153. onPress={() => router.push('mainPage')}
  154. />
  155. </View>
  156. </>
  157. )}
  158. </View>
  159. </View>
  160. </ScrollView>
  161. <Modal transparent={true} animationType="fade" visible={isLoading} onRequestClose={() => {}}>
  162. <View
  163. style={{
  164. flex: 1,
  165. justifyContent: 'center',
  166. alignItems: 'center',
  167. backgroundColor: 'rgba(0,0,0,0.5)'
  168. }}
  169. >
  170. <View style={{ backgroundColor: 'white', padding: 20, borderRadius: 10, alignItems: 'center' }}>
  171. <ActivityIndicator />
  172. <Text style={{ marginTop: 10 }}>正在啟動充電...</Text>
  173. </View>
  174. </View>
  175. </Modal>
  176. </SafeAreaView>
  177. );
  178. };
  179. const styles = StyleSheet.create({
  180. grayColor: {
  181. color: '#888888'
  182. },
  183. greenColor: {
  184. color: '#02677D'
  185. }
  186. });
  187. export default ChargingHurryUpPageComponent;