chooseCarPageComponent.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { router } from 'expo-router';
  2. import {
  3. View,
  4. Text,
  5. ScrollView,
  6. Pressable,
  7. Image,
  8. Dimensions,
  9. StyleSheet,
  10. TextInput,
  11. Alert,
  12. ActivityIndicator
  13. } from 'react-native';
  14. import { SafeAreaView } from 'react-native-safe-area-context';
  15. import { PreviousPageBlackSvg } from '../global/SVG';
  16. import NormalButton from '../global/normal_button';
  17. import { useEffect, useState } from 'react';
  18. import Checkbox from 'expo-checkbox';
  19. import DropdownSelect from '../global/dropdown_select';
  20. import { chargeStationService } from '../../service/chargeStationService';
  21. const chooseCarPageComponent = () => {
  22. const [isChecked, setChecked] = useState(false);
  23. const { height: deviceHeight, width: deviceWidth } = Dimensions.get('window');
  24. const [brandNameDropdownOptions, setBrandNameDropdownOptions] = useState([]);
  25. const [isLoading, setIsLoading] = useState(false);
  26. const [licensePlate, setLicensePlate] = useState('');
  27. const [selectedBrandID, setSelectedBrandID] = useState('');
  28. const [selectedTypeID, setSelectedTypeID] = useState('');
  29. const [caName, setCarName] = useState('');
  30. // const [carImg, setCarImg] = useState('');
  31. const [brandData, setBrandData] = useState([]);
  32. const [brandTypeDropdownOptions, setBrandTypeDropdownOptions] = useState([
  33. {
  34. label: '車輛型號',
  35. value: '車輛型號'
  36. }
  37. ]);
  38. const handleSubmit = async () => {
  39. setIsLoading(true);
  40. try {
  41. const result = await chargeStationService.addCar(licensePlate, selectedBrandID, selectedTypeID, isChecked);
  42. if (result) {
  43. router.push({
  44. pathname: 'addVehicleSuccessfulPage',
  45. params: { selectedTypeID: selectedTypeID }
  46. });
  47. } else {
  48. Alert.alert('新增車輛失敗', '請再試一次');
  49. }
  50. } catch (error) {
  51. console.log(error, 'unexpected');
  52. } finally {
  53. setIsLoading(false);
  54. }
  55. };
  56. useEffect(() => {
  57. const fetchData = async () => {
  58. try {
  59. const result = await chargeStationService.fetchCarBrand();
  60. setBrandData(result.data);
  61. const brandInfo = result.data.map((item) => ({
  62. name: item.name,
  63. id: item.id,
  64. img_url: item.img_url
  65. }));
  66. // console.log(JSON.stringify(brandInfo, null, 2));
  67. const brandName = brandInfo.map((item) => item.name);
  68. console.log(brandName);
  69. const brandNameDropdownOptions = brandInfo.map((item) => ({
  70. label: item.name,
  71. value: item.id
  72. }));
  73. setBrandNameDropdownOptions(brandNameDropdownOptions);
  74. } catch (error) {
  75. console.log(error);
  76. }
  77. };
  78. fetchData();
  79. }, []);
  80. useEffect(() => {
  81. if (selectedBrandID) {
  82. const selectedBrand = brandData.find((brand) => brand.id === selectedBrandID);
  83. if (selectedBrand && selectedBrand.car_types) {
  84. const typeOptions = selectedBrand.car_types.map((carType) => ({
  85. label: carType.name,
  86. value: carType.id
  87. }));
  88. setBrandTypeDropdownOptions(typeOptions);
  89. } else {
  90. setBrandTypeDropdownOptions([]);
  91. }
  92. } else {
  93. setBrandTypeDropdownOptions([]);
  94. }
  95. }, [selectedBrandID, brandData]);
  96. return (
  97. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  98. <ScrollView showsVerticalScrollIndicator={false} className="flex-1 mx-[5%]">
  99. <View style={{ marginTop: 25 }}>
  100. <Pressable
  101. className="self-start"
  102. onPress={() => {
  103. if (router.canGoBack()) {
  104. router.back();
  105. } else {
  106. router.replace('/accountMainPage');
  107. }
  108. }}
  109. >
  110. <PreviousPageBlackSvg />
  111. </Pressable>
  112. <Text
  113. style={{
  114. fontSize: 30,
  115. marginTop: 25,
  116. marginBottom: 20
  117. }}
  118. >
  119. 新增車輛
  120. </Text>
  121. </View>
  122. <View
  123. style={{
  124. display: 'flex',
  125. flexDirection: 'column',
  126. gap: 10
  127. }}
  128. >
  129. <DropdownSelect
  130. dropdownOptions={brandNameDropdownOptions}
  131. placeholder={'車輛品牌'}
  132. onSelect={(ID) => {
  133. setSelectedBrandID(ID);
  134. console.log(ID);
  135. }}
  136. extendedStyle={{
  137. borderWidth: 1,
  138. padding: 20,
  139. borderRadius: 12,
  140. borderColor: '#bbbbbb'
  141. }}
  142. />
  143. <DropdownSelect
  144. dropdownOptions={brandTypeDropdownOptions}
  145. placeholder={'車輛品牌'}
  146. onSelect={(carTypeID) => setSelectedTypeID(carTypeID)}
  147. extendedStyle={{
  148. borderWidth: 1,
  149. padding: 20,
  150. borderRadius: 12,
  151. borderColor: '#bbbbbb'
  152. }}
  153. />
  154. <TextInput
  155. style={styles.fakeTextInput}
  156. onChangeText={(text) => {
  157. setLicensePlate(text.toUpperCase());
  158. // console.log(licensePlate);
  159. }}
  160. value={licensePlate}
  161. placeholder="車輛牌照號碼"
  162. placeholderTextColor="#888"
  163. autoCapitalize="characters"
  164. />
  165. </View>
  166. <View className="flex-row items-center">
  167. <Text className="mt-4 mb-4 text-lg">設置為預設車輛</Text>
  168. <Checkbox
  169. style={styles.checkbox}
  170. value={isChecked}
  171. onValueChange={setChecked}
  172. color={isChecked ? '#025c72' : undefined}
  173. />
  174. </View>
  175. <NormalButton
  176. title={
  177. <Text
  178. style={{
  179. fontWeight: '700',
  180. fontSize: 20,
  181. color: '#fff'
  182. }}
  183. >
  184. {isLoading ? <ActivityIndicator /> : '新增'}
  185. </Text>
  186. }
  187. onPress={() => {
  188. handleSubmit();
  189. }}
  190. disabled={isLoading}
  191. />
  192. <Text> </Text>
  193. </ScrollView>
  194. </SafeAreaView>
  195. );
  196. };
  197. const styles = StyleSheet.create({
  198. button: { flex: 1, gap: 10, marginTop: 5 },
  199. fakeTextInput: {
  200. fontSize: 16,
  201. borderWidth: 1,
  202. padding: 24,
  203. borderRadius: 12,
  204. borderColor: '#bbbbbb',
  205. color: '#888'
  206. },
  207. checkbox: {
  208. margin: 8
  209. }
  210. });
  211. export default chooseCarPageComponent;