addVehiclePageComponent.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { router } from 'expo-router';
  2. import { View, Text, ScrollView, Pressable, Image, Dimensions, StyleSheet, TextInput, Alert } from 'react-native';
  3. import { SafeAreaView } from 'react-native-safe-area-context';
  4. import { PreviousPageBlackSvg } from '../global/SVG';
  5. import NormalButton from '../global/normal_button';
  6. import { useEffect, useState } from 'react';
  7. import Checkbox from 'expo-checkbox';
  8. import useVehicleStore from '../../providers/vehicle_store';
  9. import { chargeStationService } from '../../service/chargeStationService';
  10. const AddVehiclePageComponent = () => {
  11. const [isChecked, setChecked] = useState(false);
  12. const [isLoading, setIsLoading] = useState(false);
  13. const { height: deviceHeight, width: deviceWidth } = Dimensions.get('window');
  14. const {
  15. vehicleBrand,
  16. vehicleModel,
  17. BrandID,
  18. ModelID,
  19. licensePlate,
  20. setVehicleBrand,
  21. setVehicleModel,
  22. setBrandID,
  23. setModelID,
  24. setLicensePlate
  25. } = useVehicleStore();
  26. const handleSubmit = async () => {
  27. setIsLoading(true);
  28. try {
  29. const result = await chargeStationService.addCar(licensePlate, BrandID, ModelID, isChecked);
  30. if (result) {
  31. router.push({
  32. pathname: 'addVehicleSuccessfulPage',
  33. params: { vehicleBrand: vehicleBrand, vehicleModel: vehicleModel }
  34. });
  35. } else {
  36. Alert.alert('新增車輛失敗', '請再試一次');
  37. }
  38. } catch (error) {
  39. console.log(error, 'unexpected');
  40. } finally {
  41. setIsLoading(false);
  42. }
  43. };
  44. return (
  45. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  46. <ScrollView showsVerticalScrollIndicator={false} className="flex-1 mx-[5%]">
  47. <View style={{ marginTop: 25 }}>
  48. <Pressable
  49. className="self-start"
  50. onPress={() => {
  51. if (router.canGoBack()) {
  52. router.back();
  53. } else {
  54. router.replace('/accountMainPage');
  55. }
  56. }}
  57. >
  58. <PreviousPageBlackSvg />
  59. </Pressable>
  60. <Text
  61. style={{
  62. fontSize: 30,
  63. marginTop: 25,
  64. marginBottom: 10
  65. }}
  66. >
  67. 新增車輛
  68. </Text>
  69. </View>
  70. <View
  71. style={{
  72. display: 'flex',
  73. flexDirection: 'column',
  74. gap: 10
  75. }}
  76. >
  77. <Pressable style={styles.button} onPress={() => router.push('setVehiclesOne')}>
  78. <TextInput
  79. style={styles.fakeTextInput}
  80. placeholder={vehicleBrand ? vehicleBrand : '車輛類型'}
  81. editable={false}
  82. pointerEvents="none"
  83. ></TextInput>
  84. <TextInput
  85. style={styles.fakeTextInput}
  86. placeholder={vehicleModel ? vehicleModel : '車輛型號'}
  87. editable={false}
  88. pointerEvents="none"
  89. ></TextInput>
  90. </Pressable>
  91. <TextInput
  92. style={styles.fakeTextInput}
  93. onChangeText={(e) => setLicensePlate(e)}
  94. placeholder="點擊輸入車輛牌照號碼"
  95. ></TextInput>
  96. </View>
  97. <View className="flex-row items-center">
  98. <Text className="mt-4 mb-4 text-lg">設置為預設車輛</Text>
  99. <Checkbox
  100. style={styles.checkbox}
  101. value={isChecked}
  102. onValueChange={setChecked}
  103. color={isChecked ? '#025c72' : undefined}
  104. />
  105. </View>
  106. <NormalButton
  107. title={
  108. <Text
  109. style={{
  110. fontWeight: '700',
  111. fontSize: 20,
  112. color: '#fff'
  113. }}
  114. >
  115. 新增
  116. </Text>
  117. }
  118. onPress={() => {
  119. handleSubmit();
  120. setVehicleBrand('');
  121. setVehicleModel('');
  122. setBrandID('');
  123. setModelID('');
  124. setLicensePlate('');
  125. router.push('addVehicleSuccessfulPage');
  126. }}
  127. />
  128. <Text> </Text>
  129. </ScrollView>
  130. </SafeAreaView>
  131. );
  132. };
  133. const styles = StyleSheet.create({
  134. button: { flex: 1, gap: 10, marginTop: 5 },
  135. fakeTextInput: {
  136. maxWidth: '100%',
  137. fontSize: 16,
  138. borderWidth: 1,
  139. padding: 20,
  140. borderRadius: 12,
  141. borderColor: '#bbbbbb'
  142. },
  143. checkbox: {
  144. margin: 8
  145. }
  146. });
  147. export default AddVehiclePageComponent;