chargingHurryUpPageComponent.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. console.log(reservationData.car.car_brand.name);
  37. return (
  38. <SafeAreaView edges={['top', 'left', 'right']} className="flex-1 bg-white">
  39. <ScrollView className="flex-1 mt-8 " nestedScrollEnabled={true} showsVerticalScrollIndicator={false}>
  40. <View className="mx-[5%]">
  41. <View className="">
  42. <Text className="text-5xl pt-1 pb-6">預約已經開始</Text>
  43. <Text className="text-base">
  44. 請盡快前往充電站!逾時超過十五分鐘將當作放棄預約。已收取之費用並不會退還。
  45. </Text>
  46. {loading ? (
  47. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  48. <ActivityIndicator color="#34657b" />
  49. </View>
  50. ) : (
  51. <>
  52. <Text className="text-2xl py-4 ">你的預約為:</Text>
  53. <View className="border-gray-200 border rounded-md">
  54. <View className="flex-1 mt-4 mx-[5%] ">
  55. <View className="flex-1 flex-row items-center pb-3">
  56. <View className="flex-1 flex-column">
  57. <Text style={styles.grayColor} className="text-base">
  58. 日期
  59. </Text>
  60. <Text style={styles.greenColor} className="text-4xl text-center pt-2">
  61. {convertToHKTime(reservationData.book_time)
  62. .hkDate.split('/')
  63. .reverse()
  64. .slice(1)
  65. .join('月')}
  66. </Text>
  67. </View>
  68. <View className="flex-1 flex-column">
  69. <Text style={styles.grayColor} className="text-base pl-7">
  70. 時間
  71. </Text>
  72. <Text style={styles.greenColor} className="text-4xl text-center pt-2">
  73. {convertToHKTime(reservationData.book_time).hkTime.slice(0, 5)}
  74. </Text>
  75. </View>
  76. </View>
  77. <View className="flex-1 flex-column justify-center space-y-1 pb-3">
  78. <Text style={styles.grayColor} className="text-base">
  79. 充電地點
  80. </Text>
  81. {loading ? (
  82. <ActivityIndicator size="small" />
  83. ) : (
  84. <>
  85. <Text style={styles.greenColor} className="text-2xl ">
  86. {stationInfo?.StationName || 'N/A'}
  87. </Text>
  88. <Text style={styles.grayColor} className="text-sm">
  89. {stationInfo?.Address || 'N/A'}
  90. </Text>
  91. </>
  92. )}
  93. </View>
  94. <View className="flex-1 flex-row pb-3 ">
  95. <View className="flex-column flex-1">
  96. <Text style={styles.grayColor} className="text-base">
  97. 方案
  98. </Text>
  99. <Text style={styles.greenColor} className="text-lg">
  100. {plan}
  101. </Text>
  102. <Text style={styles.grayColor} className="text-sm">
  103. {kwh}
  104. </Text>
  105. </View>
  106. <View className="flex-column flex-1">
  107. <Text style={styles.grayColor} className="text-base">
  108. 車輛
  109. </Text>
  110. <Text style={styles.greenColor} className="text-lg">
  111. {reservationData.car.car_brand.name}
  112. </Text>
  113. <Text style={styles.greenColor} className="text-lg">
  114. {reservationData.car.car_type.name}
  115. </Text>
  116. </View>
  117. </View>
  118. </View>
  119. </View>
  120. <View className="pt-6">
  121. <NormalButton
  122. title={<Text className="text-white text-lg">返回主頁</Text>}
  123. onPress={() => router.push('mainPage')}
  124. />
  125. </View>
  126. </>
  127. )}
  128. </View>
  129. </View>
  130. </ScrollView>
  131. </SafeAreaView>
  132. );
  133. };
  134. const styles = StyleSheet.create({
  135. grayColor: {
  136. color: '#888888'
  137. },
  138. greenColor: {
  139. color: '#02677D'
  140. }
  141. });
  142. export default ChargingHurryUpPageComponent;