chargingHurryUpPageComponent.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { View, Text, ScrollView, StyleSheet, ActivityIndicator } 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 plan = reservationData.book_time === reservationData.end_time ? '充滿停機' : '按每度電結算';
  18. const kwh = reservationData.total_power === null ? '' : `${reservationData.total_power} kWh`;
  19. useEffect(() => {
  20. setLoading(true);
  21. if (
  22. reservationData &&
  23. reservationData.connector &&
  24. reservationData.connector.EquipmentID &&
  25. reservationData.connector.EquipmentID.StationID
  26. ) {
  27. try {
  28. const parsedSnapshot = JSON.parse(reservationData.connector.EquipmentID.StationID.snapshot);
  29. setStationInfo(parsedSnapshot);
  30. } catch (error) {
  31. console.error('Error parsing station snapshot:', error);
  32. }
  33. }
  34. setLoading(false);
  35. }, [reservationData]);
  36. const startPayload = {
  37. StartChargeSeq: reservationData.format_order_id,
  38. ConnectorID: reservationData.connector.ConnectorID,
  39. StopBy: 0,
  40. StopValue: 0,
  41. StartBalance: 400.99
  42. };
  43. const handleStartCharge = async () => {
  44. try {
  45. console.log('startPayload', startPayload);
  46. const response = await chargeStationService.startCharging(startPayload);
  47. if (response) {
  48. console.log('handleStartCharge begins, response received', startPayload);
  49. router.push('chargingPage');
  50. } else {
  51. console.log('啟動失敗');
  52. }
  53. } catch (error) {
  54. console.log(error);
  55. }
  56. };
  57. return (
  58. <SafeAreaView edges={['top', 'left', 'right']} className="flex-1 bg-white">
  59. <ScrollView className="flex-1 mt-8 " nestedScrollEnabled={true} showsVerticalScrollIndicator={false}>
  60. <View className="mx-[5%]">
  61. <View className="">
  62. <Text className="text-5xl pt-1 pb-6">預約已經開始</Text>
  63. <Text className="text-base">到達充電站後,按下方按鈕開始充電。</Text>
  64. {loading ? (
  65. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  66. <ActivityIndicator color="#34657b" />
  67. </View>
  68. ) : (
  69. <>
  70. <Text className="text-2xl py-4 ">你的預約為:</Text>
  71. <View className="border-gray-200 border rounded-md">
  72. <View className="flex-1 mt-4 mx-[5%] ">
  73. <View className="flex-1 flex-row items-center pb-3">
  74. <View className="flex-1 flex-column">
  75. <Text style={styles.grayColor} className="text-base">
  76. 日期
  77. </Text>
  78. <Text style={styles.greenColor} className="text-4xl text-center pt-2">
  79. {convertToHKTime(reservationData.book_time)
  80. .hkDate.split('/')
  81. .reverse()
  82. .slice(1)
  83. .join('月')}
  84. </Text>
  85. </View>
  86. <View className="flex-1 flex-column">
  87. <Text style={styles.grayColor} className="text-base pl-7">
  88. 時間
  89. </Text>
  90. <Text style={styles.greenColor} className="text-4xl text-center pt-2">
  91. {convertToHKTime(reservationData.book_time).hkTime.slice(0, 5)}
  92. </Text>
  93. </View>
  94. </View>
  95. <View className="flex-1 flex-column justify-center space-y-1 pb-3">
  96. <Text style={styles.grayColor} className="text-base">
  97. 充電地點
  98. </Text>
  99. {loading ? (
  100. <ActivityIndicator size="small" />
  101. ) : (
  102. <>
  103. <Text style={styles.greenColor} className="text-2xl ">
  104. {stationInfo?.StationName || 'N/A'}
  105. </Text>
  106. <Text style={styles.grayColor} className="text-sm">
  107. {stationInfo?.Address || 'N/A'}
  108. </Text>
  109. </>
  110. )}
  111. </View>
  112. <View className="flex-1 flex-row pb-3 ">
  113. <View className="flex-column flex-1">
  114. <Text style={styles.grayColor} className="text-base">
  115. 方案
  116. </Text>
  117. <Text style={styles.greenColor} className="text-lg">
  118. {plan}
  119. </Text>
  120. <Text style={styles.grayColor} className="text-sm">
  121. {kwh}
  122. </Text>
  123. </View>
  124. <View className="flex-column flex-1">
  125. <Text style={styles.grayColor} className="text-base">
  126. 車輛
  127. </Text>
  128. <Text style={styles.greenColor} className="text-lg">
  129. {reservationData.car.car_brand.name}
  130. </Text>
  131. <Text style={styles.greenColor} className="text-lg">
  132. {reservationData.car.car_type.name}
  133. </Text>
  134. </View>
  135. </View>
  136. </View>
  137. </View>
  138. <View className="pt-6">
  139. <NormalButton
  140. title={<Text className="text-white text-lg">發動充電樁 - 開始充電</Text>}
  141. onPress={() => handleStartCharge()}
  142. />
  143. </View>
  144. <View className="pt-6">
  145. <NormalButton
  146. title={<Text className="text-white text-lg">返回主頁</Text>}
  147. onPress={() => router.push('mainPage')}
  148. />
  149. </View>
  150. </>
  151. )}
  152. </View>
  153. </View>
  154. </ScrollView>
  155. </SafeAreaView>
  156. );
  157. };
  158. const styles = StyleSheet.create({
  159. grayColor: {
  160. color: '#888888'
  161. },
  162. greenColor: {
  163. color: '#02677D'
  164. }
  165. });
  166. export default ChargingHurryUpPageComponent;