chooseCarPageComponent.tsx 8.0 KB

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