manageVehiclePageComponent.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { router, useLocalSearchParams } from 'expo-router';
  2. import { View, Text, ScrollView, Pressable, Image, Dimensions, Alert, ActivityIndicator } from 'react-native';
  3. import { SafeAreaView } from 'react-native-safe-area-context';
  4. import { PreviousPageBlackSvg, StarSvg } from '../global/SVG';
  5. import NormalButton from '../global/normal_button';
  6. import { chargeStationService } from '../../service/chargeStationService';
  7. import { useState } from 'react';
  8. import { formatDate } from '../../util/lib';
  9. const ManageVehiclePageComponent = () => {
  10. const { height: deviceHeight, width: deviceWidth } = Dimensions.get('window');
  11. const [loading, setLoading] = useState(false);
  12. const [loading2, setLoading2] = useState(false);
  13. const params = useLocalSearchParams();
  14. const carID = params?.carID;
  15. const carCapacitance = params?.capacitance;
  16. const carCapacitanceUnit = params?.capacitance_unit;
  17. const carCreatedAt = params?.createdAt;
  18. const carModel = params?.carModel;
  19. const carImage = params.carImage;
  20. const licensePlate = params?.licensePlate;
  21. const handleDeleteCar = async (carID) => {
  22. const confirmDelete = () => {
  23. return new Promise((resolve) => {
  24. Alert.alert(
  25. 'Confirm Delete',
  26. 'Are you sure you want to delete this car?',
  27. [
  28. {
  29. text: 'No',
  30. onPress: () => resolve(false),
  31. style: 'cancel'
  32. },
  33. { text: 'Yes', onPress: () => resolve(true) }
  34. ],
  35. { cancelable: false }
  36. );
  37. });
  38. };
  39. const userConfirmed = await confirmDelete();
  40. if (userConfirmed) {
  41. try {
  42. setLoading2(true);
  43. const isDeleted = await chargeStationService.deleteCar(carID);
  44. if (isDeleted) {
  45. Alert.alert('Deletion Successful', 'Car has been deleted');
  46. router.push('/mainPage');
  47. } else {
  48. // Show an error message if deletion was not successful
  49. Alert.alert('Deletion Failed', 'Unable to delete the car. Please try again.');
  50. }
  51. } catch (error) {
  52. console.error('Error deleting car:', error);
  53. Alert.alert('Error', 'An error occurred while deleting the car.');
  54. } finally {
  55. setLoading2(false);
  56. }
  57. }
  58. };
  59. const handleSetDefaultCar = async (carID) => {
  60. setLoading(true);
  61. try {
  62. const isSetDefault = await chargeStationService.setDefaultCar(carID);
  63. if (isSetDefault) {
  64. Alert.alert('設置成功', '已成功設置預設車輛');
  65. router.push('/mainPage');
  66. } else {
  67. }
  68. } catch (error) {
  69. } finally {
  70. setLoading(false);
  71. }
  72. };
  73. return (
  74. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  75. <ScrollView showsVerticalScrollIndicator={false} className="flex-1 mx-[5%]">
  76. <View style={{ marginTop: 25, flexDirection: 'column' }}>
  77. <Pressable
  78. className="self-start"
  79. onPress={() => {
  80. if (router.canGoBack()) {
  81. router.back();
  82. } else {
  83. router.replace('/accountMainPage');
  84. }
  85. }}
  86. >
  87. <PreviousPageBlackSvg />
  88. </Pressable>
  89. </View>
  90. <Text
  91. style={{
  92. fontSize: 30,
  93. marginTop: 25,
  94. marginBottom: 10
  95. }}
  96. >
  97. 管理車輛
  98. </Text>
  99. <View className="items-center ">
  100. <Image
  101. // source={{ uri: carImage}}
  102. source={require('../../assets/car.png')}
  103. resizeMode="contain"
  104. style={{
  105. width: deviceWidth * 0.8,
  106. height: deviceHeight * 0.25
  107. }}
  108. />
  109. </View>
  110. <View className="flex-row space-x-2 ">
  111. <View
  112. style={{
  113. width: 4,
  114. height: '100%',
  115. backgroundColor: '#02677D'
  116. }}
  117. />
  118. <View>
  119. <Text className="text-2xl">{carModel}</Text>
  120. <Text className="text-lg text-[#888888]">{licensePlate}</Text>
  121. </View>
  122. </View>
  123. <NormalButton
  124. title={
  125. loading ? (
  126. <View
  127. style={{
  128. flexDirection: 'row',
  129. alignItems: 'center'
  130. }}
  131. >
  132. <ActivityIndicator size="small" color="#fff" />
  133. <Text
  134. style={{
  135. fontWeight: '500',
  136. fontSize: 20,
  137. color: '#fff',
  138. marginLeft: 10
  139. }}
  140. >
  141. 設置中...
  142. </Text>
  143. </View>
  144. ) : (
  145. <Text
  146. style={{
  147. fontWeight: '500',
  148. fontSize: 20,
  149. color: '#fff'
  150. }}
  151. >
  152. 設置為預設車輛 <StarSvg />
  153. </Text>
  154. )
  155. }
  156. onPress={() => {
  157. if (!loading) {
  158. handleSetDefaultCar(carID);
  159. }
  160. }}
  161. extendedStyle={{ marginTop: 24 }}
  162. disabled={loading}
  163. />
  164. <View className="w-full h-1 my-4 bg-[#DBE4E8]" />
  165. <View>
  166. <Text className="text-xl pb-4">車輛資訊</Text>
  167. <Text className="text-base text-[#888888]">加入日期</Text>
  168. <Text className="text-base mb-3">{formatDate(carCreatedAt)}</Text>
  169. <Text className="text-base text-[#888888]">總充電量</Text>
  170. <Text className="text-base mb-3">{carCapacitance + carCapacitanceUnit}</Text>
  171. <Text className="text-base text-[#888888]">上一次充電</Text>
  172. <Text className="text-base mb-3">待DATA</Text>
  173. <View className="my-4 pb-8">
  174. <NormalButton
  175. title={
  176. <Text
  177. style={{
  178. color: 'white',
  179. fontSize: 20,
  180. fontWeight: '800'
  181. }}
  182. >
  183. {loading2 ? '删除車輛中...' : '删除車輛'}
  184. </Text>
  185. }
  186. onPress={() => {
  187. handleDeleteCar(carID);
  188. }}
  189. extendedStyle={{
  190. backgroundColor: '#D40000'
  191. }}
  192. />
  193. </View>
  194. </View>
  195. </ScrollView>
  196. </SafeAreaView>
  197. );
  198. };
  199. export default ManageVehiclePageComponent;