addVehiclePageComponent.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.toUpperCase())}
  94. placeholder="點擊輸入車輛牌照號碼"
  95. autoCapitalize="characters"
  96. ></TextInput>
  97. </View>
  98. <View className="flex-row items-center">
  99. <Text className="mt-4 mb-4 text-lg">設置為預設車輛</Text>
  100. <Checkbox
  101. style={styles.checkbox}
  102. value={isChecked}
  103. onValueChange={setChecked}
  104. color={isChecked ? '#025c72' : undefined}
  105. />
  106. </View>
  107. <NormalButton
  108. title={
  109. <Text
  110. style={{
  111. fontWeight: '700',
  112. fontSize: 20,
  113. color: '#fff'
  114. }}
  115. >
  116. 新增
  117. </Text>
  118. }
  119. onPress={() => {
  120. handleSubmit();
  121. setVehicleBrand('');
  122. setVehicleModel('');
  123. setBrandID('');
  124. setModelID('');
  125. setLicensePlate('');
  126. router.push('addVehicleSuccessfulPage');
  127. }}
  128. />
  129. <Text> </Text>
  130. </ScrollView>
  131. </SafeAreaView>
  132. );
  133. };
  134. const styles = StyleSheet.create({
  135. button: { flex: 1, gap: 10, marginTop: 5 },
  136. fakeTextInput: {
  137. maxWidth: '100%',
  138. fontSize: 16,
  139. borderWidth: 1,
  140. padding: 20,
  141. borderRadius: 12,
  142. borderColor: '#bbbbbb'
  143. },
  144. checkbox: {
  145. margin: 8
  146. }
  147. });
  148. export default AddVehiclePageComponent;