manageVehiclePageComponent.tsx 8.8 KB

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