manageVehiclePageComponent.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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={{ uri: carImage}}
  105. source={require('../../assets/car.png')}
  106. resizeMode="contain"
  107. style={{
  108. width: deviceWidth * 0.8,
  109. height: deviceHeight * 0.25
  110. }}
  111. />
  112. </View>
  113. <View className="flex-row space-x-2 ">
  114. <View
  115. style={{
  116. width: 4,
  117. height: '100%',
  118. backgroundColor: '#02677D'
  119. }}
  120. />
  121. <View>
  122. <Text className="text-2xl">{carModel}</Text>
  123. <Text className="text-lg text-[#888888]">{licensePlate}</Text>
  124. </View>
  125. </View>
  126. <NormalButton
  127. title={
  128. loading ? (
  129. <View
  130. style={{
  131. flexDirection: 'row',
  132. alignItems: 'center'
  133. }}
  134. >
  135. <ActivityIndicator size="small" color="#fff" />
  136. <Text
  137. style={{
  138. fontWeight: '500',
  139. fontSize: 20,
  140. color: '#fff',
  141. marginLeft: 10
  142. }}
  143. >
  144. 設置中...
  145. </Text>
  146. </View>
  147. ) : (
  148. <Text
  149. style={{
  150. fontWeight: '500',
  151. fontSize: 20,
  152. color: '#fff'
  153. }}
  154. >
  155. 設置為預設車輛 <StarSvg />
  156. </Text>
  157. )
  158. }
  159. onPress={() => {
  160. if (!loading) {
  161. handleSetDefaultCar(carID);
  162. }
  163. }}
  164. extendedStyle={{ marginTop: 24 }}
  165. disabled={loading}
  166. />
  167. <View className="w-full h-1 my-4 bg-[#DBE4E8]" />
  168. <View>
  169. <Text className="text-xl pb-4">車輛資訊</Text>
  170. <Text className="text-base text-[#888888]">加入日期</Text>
  171. <Text className="text-base mb-3">{formatDate(carCreatedAt)}</Text>
  172. <Text className="text-base text-[#888888]">總充電量</Text>
  173. <Text className="text-base mb-3">{carCapacitance + carCapacitanceUnit}</Text>
  174. <Text className="text-base text-[#888888]">上一次充電</Text>
  175. <Text className="text-base mb-3">待DATA</Text>
  176. <View className="my-4 pb-8">
  177. <NormalButton
  178. title={
  179. <Text
  180. style={{
  181. color: 'white',
  182. fontSize: 20,
  183. fontWeight: '800'
  184. }}
  185. >
  186. {loading2 ? '删除車輛中...' : '删除車輛'}
  187. </Text>
  188. }
  189. onPress={() => {
  190. handleDeleteCar(carID);
  191. }}
  192. extendedStyle={{
  193. backgroundColor: '#D40000'
  194. }}
  195. />
  196. </View>
  197. </View>
  198. </ScrollView>
  199. </SafeAreaView>
  200. );
  201. };
  202. export default ManageVehiclePageComponent;