chargingHurryUpPageComponent.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. console.log('startPayload', startPayload);
  48. const response = await chargeStationService.startCharging(startPayload);
  49. if (response) {
  50. console.log('handleStartCharge begins, response received:', response);
  51. setIsLoading(false);
  52. Alert.alert('啟動成功', '請稍後等待頁面自動跳轉至充電介面', [{ text: 'OK', onPress: () => {} }]);
  53. setTimeout(() => {
  54. router.push('chargingPage');
  55. }, 15000);
  56. } else {
  57. setIsLoading(false);
  58. Alert.alert('啟動失敗');
  59. console.log('啟動失敗');
  60. }
  61. } catch (error) {
  62. setIsLoading(false);
  63. console.log(error);
  64. Alert.alert('發生錯誤', '請稍後再試');
  65. }
  66. };
  67. return (
  68. <SafeAreaView edges={['top', 'left', 'right']} className="flex-1 bg-white">
  69. <ScrollView className="flex-1 mt-8 " nestedScrollEnabled={true} showsVerticalScrollIndicator={false}>
  70. <View className="mx-[5%]">
  71. <View className="">
  72. <Text className="text-5xl pt-1 pb-6">預約已經開始</Text>
  73. <Text className="text-base">到達充電站後,按下方按鈕開始充電。</Text>
  74. {loading ? (
  75. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  76. <ActivityIndicator color="#34657b" />
  77. </View>
  78. ) : (
  79. <>
  80. <Text className="text-2xl py-4 ">你的預約為:</Text>
  81. <View className="border-gray-200 border rounded-md">
  82. <View className="flex-1 mt-4 mx-[5%] ">
  83. <View className="flex-1 flex-row items-center pb-3">
  84. <View className="flex-1 flex-column">
  85. <Text style={styles.grayColor} className="text-base">
  86. 日期
  87. </Text>
  88. <Text style={styles.greenColor} className="text-4xl text-center pt-2">
  89. {convertToHKTime(reservationData.book_time)
  90. .hkDate.split('/')
  91. .reverse()
  92. .slice(1)
  93. .join('月')}
  94. </Text>
  95. </View>
  96. <View className="flex-1 flex-column">
  97. <Text style={styles.grayColor} className="text-base pl-7">
  98. 時間
  99. </Text>
  100. <Text style={styles.greenColor} className="text-4xl text-center pt-2">
  101. {convertToHKTime(reservationData.book_time).hkTime.slice(0, 5)}
  102. </Text>
  103. </View>
  104. </View>
  105. <View className="flex-1 flex-column justify-center space-y-1 pb-3">
  106. <Text style={styles.grayColor} className="text-base">
  107. 充電地點
  108. </Text>
  109. {loading ? (
  110. <ActivityIndicator size="small" />
  111. ) : (
  112. <>
  113. <Text style={styles.greenColor} className="text-2xl ">
  114. {stationInfo?.StationName || 'N/A'}
  115. </Text>
  116. <Text style={styles.grayColor} className="text-sm">
  117. {stationInfo?.Address || 'N/A'}
  118. </Text>
  119. </>
  120. )}
  121. </View>
  122. <View className="flex-1 flex-row pb-3 ">
  123. <View className="flex-column flex-1">
  124. <Text style={styles.grayColor} className="text-base">
  125. 方案
  126. </Text>
  127. <Text style={styles.greenColor} className="text-lg">
  128. {plan}
  129. </Text>
  130. <Text style={styles.grayColor} className="text-sm">
  131. {kwh}
  132. </Text>
  133. </View>
  134. <View className="flex-column flex-1">
  135. <Text style={styles.grayColor} className="text-base">
  136. 車輛
  137. </Text>
  138. <Text style={styles.greenColor} className="text-lg">
  139. {reservationData.car.car_brand.name}
  140. </Text>
  141. <Text style={styles.greenColor} className="text-lg">
  142. {reservationData.car.car_type.name}
  143. </Text>
  144. </View>
  145. </View>
  146. </View>
  147. </View>
  148. <View className="pt-6">
  149. <NormalButton
  150. title={<Text className="text-white text-lg">發動充電樁 - 開始充電</Text>}
  151. onPress={() => handleStartCharge()}
  152. />
  153. </View>
  154. <View className="pt-6">
  155. <NormalButton
  156. title={<Text className="text-white text-lg">返回主頁</Text>}
  157. onPress={() => router.push('mainPage')}
  158. />
  159. </View>
  160. </>
  161. )}
  162. </View>
  163. </View>
  164. </ScrollView>
  165. <Modal transparent={true} animationType="fade" visible={isLoading} onRequestClose={() => {}}>
  166. <View
  167. style={{
  168. flex: 1,
  169. justifyContent: 'center',
  170. alignItems: 'center',
  171. backgroundColor: 'rgba(0,0,0,0.5)'
  172. }}
  173. >
  174. <View style={{ backgroundColor: 'white', padding: 20, borderRadius: 10, alignItems: 'center' }}>
  175. <ActivityIndicator />
  176. <Text style={{ marginTop: 10 }}>正在啟動充電...</Text>
  177. </View>
  178. </View>
  179. </Modal>
  180. </SafeAreaView>
  181. );
  182. };
  183. const styles = StyleSheet.create({
  184. grayColor: {
  185. color: '#888888'
  186. },
  187. greenColor: {
  188. color: '#02677D'
  189. }
  190. });
  191. export default ChargingHurryUpPageComponent;