manageVehiclePageComponent.tsx 8.5 KB

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