carInformation.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // import { View, Text, StyleSheet, Pressable, TextInput } from 'react-native';
  2. // import NormalInput from '../../../global/normal_input';
  3. // import NormalButton from '../../../global/normal_button';
  4. // import { useEffect, useState } from 'react';
  5. // import useSignUpStore from '../../../../providers/signup_form_store';
  6. // import DropdownSelect from '../../../global/dropdown_select';
  7. // import { chargeStationService } from '../../../../service/chargeStationService';
  8. // import Checkbox from 'expo-checkbox';
  9. // type CarInformationProps = {
  10. // goToNextPage: () => void;
  11. // goToChooseCarPage: () => void;
  12. // };
  13. // const CarInformation: React.FC<CarInformationProps> = ({
  14. // goToNextPage,
  15. // goToChooseCarPage
  16. // }) => {
  17. // const { signUpFormData, setSignUpFormData } = useSignUpStore();
  18. // const [error, setError] = useState('');
  19. // const [isChecked, setChecked] = useState(false);
  20. // const [brandNameDropdownOptions, setBrandNameDropdownOptions] = useState(
  21. // []
  22. // );
  23. // const [brandData, setBrandData] = useState([]);
  24. // const [brandTypeDropdownOptions, setBrandTypeDropdownOptions] = useState([
  25. // {
  26. // label: '車輛型號',
  27. // value: '車輛型號'
  28. // }
  29. // ]);
  30. // useEffect(() => {
  31. // const fetchData = async () => {
  32. // try {
  33. // const result = await chargeStationService.fetchCarBrand();
  34. // setBrandData(result.data);
  35. // const brandInfo = result.data.map((item) => ({
  36. // name: item.name,
  37. // id: item.id,
  38. // img_url: item.img_url
  39. // }));
  40. // // console.log(JSON.stringify(brandInfo, null, 2));
  41. // const brandName = brandInfo.map((item) => item.name);
  42. // console.log(brandName);
  43. // const brandNameDropdownOptions = brandInfo.map((item) => ({
  44. // label: item.name,
  45. // value: item.id
  46. // }));
  47. // setBrandNameDropdownOptions(brandNameDropdownOptions);
  48. // } catch (error) {
  49. // console.log(error);
  50. // }
  51. // };
  52. // fetchData();
  53. // }, []);
  54. // useEffect(() => {
  55. // if (signUpFormData.selectedCarBrand) {
  56. // const selectedBrand = brandData.find(
  57. // (brand) => brand.id === signUpFormData.selectedCarBrand
  58. // );
  59. // if (selectedBrand && selectedBrand.car_types) {
  60. // const typeOptions = selectedBrand.car_types.map((carType) => ({
  61. // label: carType.name,
  62. // value: carType.id
  63. // }));
  64. // setBrandTypeDropdownOptions(typeOptions);
  65. // } else {
  66. // setBrandTypeDropdownOptions([]);
  67. // }
  68. // } else {
  69. // setBrandTypeDropdownOptions([]);
  70. // }
  71. // }, [signUpFormData.selectedCarBrand, brandData]);
  72. // const handleNext = () => {
  73. // if (
  74. // signUpFormData.selectedCarBrand === '0000' ||
  75. // signUpFormData.selectedCarSeries === '0000' ||
  76. // signUpFormData.licensePlate === '0000'
  77. // ) {
  78. // setError('請確保所有資料都已填寫。');
  79. // } else {
  80. // setError('');
  81. // goToNextPage();
  82. // }
  83. // };
  84. // return (
  85. // <>
  86. // <View style={styles.container}>
  87. // <Text style={styles.text}>您的車輛</Text>
  88. // <View
  89. // style={{
  90. // display: 'flex',
  91. // flexDirection: 'column',
  92. // gap: 10
  93. // }}
  94. // >
  95. // <View
  96. // style={{
  97. // display: 'flex',
  98. // flexDirection: 'column',
  99. // gap: 10
  100. // }}
  101. // >
  102. // <DropdownSelect
  103. // dropdownOptions={brandNameDropdownOptions}
  104. // placeholder={'車輛品牌'}
  105. // onSelect={(ID) => {
  106. // setSignUpFormData({
  107. // selectedCarBrand: ID
  108. // });
  109. // console.log(ID);
  110. // }}
  111. // extendedStyle={{
  112. // borderWidth: 1,
  113. // padding: 20,
  114. // borderRadius: 12,
  115. // borderColor: '#bbbbbb'
  116. // }}
  117. // />
  118. // <DropdownSelect
  119. // dropdownOptions={brandTypeDropdownOptions}
  120. // placeholder={'車輛品牌'}
  121. // onSelect={(carTypeID) =>
  122. // setSignUpFormData({
  123. // selectedCarSeries: carTypeID
  124. // })
  125. // }
  126. // extendedStyle={{
  127. // borderWidth: 1,
  128. // padding: 20,
  129. // borderRadius: 12,
  130. // borderColor: '#bbbbbb'
  131. // }}
  132. // />
  133. // <TextInput
  134. // style={styles.fakeTextInput}
  135. // onChangeText={(text) => {
  136. // setSignUpFormData({
  137. // licensePlate: text
  138. // });
  139. // // console.log(licensePlate);
  140. // }}
  141. // // value={signUpFormData.licensePlate}
  142. // placeholder="車輛牌照號碼"
  143. // placeholderTextColor="#888"
  144. // />
  145. // </View>
  146. // {/* <View className="flex-row items-center">
  147. // <Text className="mt-4 mb-4 text-lg">
  148. // 設置為預設車輛
  149. // </Text>
  150. // <Checkbox
  151. // style={styles.checkbox}
  152. // value={isChecked}
  153. // onValueChange={setChecked}
  154. // color={isChecked ? '#025c72' : undefined}
  155. // />
  156. // </View> */}
  157. // {/* <Pressable
  158. // style={styles.button}
  159. // onPress={goToChooseCarPage}
  160. // >
  161. // <TextInput
  162. // style={styles.fakeTextInput}
  163. // placeholder="車輛品牌"
  164. // editable={false}
  165. // pointerEvents="none"
  166. // ></TextInput>
  167. // <TextInput
  168. // style={styles.fakeTextInput}
  169. // placeholder="車輛型號"
  170. // editable={false}
  171. // pointerEvents="none"
  172. // ></TextInput>
  173. // <TextInput
  174. // style={styles.fakeTextInput}
  175. // placeholder="車輛號碼"
  176. // editable={false}
  177. // pointerEvents="none"
  178. // ></TextInput>
  179. // </Pressable> */}
  180. // <NormalButton
  181. // title={<Text style={{ color: '#fff' }}>下一步</Text>}
  182. // onPress={handleNext}
  183. // extendedStyle={{}}
  184. // />
  185. // {error && <Text style={styles.errorMessage}>{error}</Text>}
  186. // <NormalButton
  187. // title={<Text style={{ color: '#888888' }}>略過</Text>}
  188. // onPress={goToNextPage}
  189. // extendedStyle={{ backgroundColor: 'transparent' }}
  190. // />
  191. // </View>
  192. // </View>
  193. // </>
  194. // );
  195. // };
  196. // const styles = StyleSheet.create({
  197. // container: {
  198. // flex: 1,
  199. // marginHorizontal: 20
  200. // },
  201. // button: { flex: 1, gap: 10, marginTop: 5 },
  202. // fakeTextInput: {
  203. // maxWidth: '100%',
  204. // fontSize: 16,
  205. // borderWidth: 1,
  206. // padding: 20,
  207. // borderRadius: 12,
  208. // borderColor: '#bbbbbb'
  209. // },
  210. // text: {
  211. // fontSize: 20,
  212. // paddingBottom: 10
  213. // },
  214. // errorMessage: {
  215. // fontSize: 14,
  216. // color: '#ff0033',
  217. // fontWeight: '400',
  218. // marginLeft: 10,
  219. // marginTop: 10
  220. // },
  221. // checkbox: {
  222. // margin: 8
  223. // }
  224. // });
  225. // export default CarInformation;
  226. import { View, Text, StyleSheet, Pressable, TextInput } from 'react-native';
  227. import NormalInput from '../../../global/normal_input';
  228. import NormalButton from '../../../global/normal_button';
  229. import { useEffect, useState } from 'react';
  230. import useSignUpStore from '../../../../providers/signup_form_store';
  231. import DropdownSelect from '../../../global/dropdown_select';
  232. import { chargeStationService } from '../../../../service/chargeStationService';
  233. import Checkbox from 'expo-checkbox';
  234. import { router } from 'expo-router';
  235. import useVehicleStore from '../../../../providers/vehicle_store';
  236. type CarInformationProps = {
  237. goToNextPage: () => void;
  238. goToChooseCarPage: () => void;
  239. };
  240. const CarInformation: React.FC<CarInformationProps> = ({ goToNextPage, goToChooseCarPage }) => {
  241. const { signUpFormData, setSignUpFormData } = useSignUpStore();
  242. const [error, setError] = useState('');
  243. const [isChecked, setChecked] = useState(false);
  244. const [brandNameDropdownOptions, setBrandNameDropdownOptions] = useState([]);
  245. const [brandData, setBrandData] = useState([]);
  246. const [brandTypeDropdownOptions, setBrandTypeDropdownOptions] = useState([
  247. {
  248. label: '車輛型號',
  249. value: '車輛型號'
  250. }
  251. ]);
  252. const {
  253. vehicleBrand,
  254. vehicleModel,
  255. BrandID,
  256. ModelID,
  257. licensePlate,
  258. setVehicleBrand,
  259. setVehicleModel,
  260. setBrandID,
  261. setModelID,
  262. setLicensePlate
  263. } = useVehicleStore();
  264. useEffect(() => {
  265. const fetchData = async () => {
  266. try {
  267. const result = await chargeStationService.fetchCarBrand();
  268. setBrandData(result.data);
  269. const brandInfo = result.data.map((item) => ({
  270. name: item.name,
  271. id: item.id,
  272. img_url: item.img_url
  273. }));
  274. // console.log(JSON.stringify(brandInfo, null, 2));
  275. const brandName = brandInfo.map((item) => item.name);
  276. console.log(brandName);
  277. const brandNameDropdownOptions = brandInfo.map((item) => ({
  278. label: item.name,
  279. value: item.id
  280. }));
  281. setBrandNameDropdownOptions(brandNameDropdownOptions);
  282. } catch (error) {
  283. console.log(error);
  284. }
  285. };
  286. fetchData();
  287. }, []);
  288. useEffect(() => {
  289. if (signUpFormData.selectedCarBrand) {
  290. const selectedBrand = brandData.find((brand) => brand.id === signUpFormData.selectedCarBrand);
  291. if (selectedBrand && selectedBrand.car_types) {
  292. const typeOptions = selectedBrand.car_types.map((carType) => ({
  293. label: carType.name,
  294. value: carType.id
  295. }));
  296. setBrandTypeDropdownOptions(typeOptions);
  297. } else {
  298. setBrandTypeDropdownOptions([]);
  299. }
  300. } else {
  301. setBrandTypeDropdownOptions([]);
  302. }
  303. }, [signUpFormData.selectedCarBrand, brandData]);
  304. //HERE I need to get car information, and the final create wallet i need to get car information
  305. const handleNext = () => {
  306. if (
  307. BrandID === '' ||
  308. ModelID === '' ||
  309. signUpFormData.licensePlate === '0000' ||
  310. signUpFormData.licensePlate === ''
  311. ) {
  312. setError('請確保所有資料都已填寫。');
  313. } else {
  314. setError('');
  315. goToNextPage();
  316. }
  317. };
  318. return (
  319. <>
  320. <View style={styles.container}>
  321. <Text style={styles.text}>您的車輛</Text>
  322. <View
  323. style={{
  324. display: 'flex',
  325. flexDirection: 'column',
  326. gap: 10
  327. }}
  328. >
  329. <View
  330. style={{
  331. display: 'flex',
  332. flexDirection: 'column',
  333. gap: 10
  334. }}
  335. >
  336. <Pressable
  337. style={styles.button}
  338. onPress={() => router.push('(public)/registerChooseVehiclesOne')}
  339. >
  340. <TextInput
  341. style={styles.fakeTextInput}
  342. placeholder={vehicleBrand ? vehicleBrand : '車輛品牌'}
  343. editable={false}
  344. pointerEvents="none"
  345. ></TextInput>
  346. <TextInput
  347. style={styles.fakeTextInput}
  348. placeholder={vehicleModel ? vehicleModel : '車輛型號'}
  349. editable={false}
  350. pointerEvents="none"
  351. ></TextInput>
  352. </Pressable>
  353. <TextInput
  354. style={styles.fakeTextInput}
  355. onChangeText={(text) => {
  356. setSignUpFormData({
  357. licensePlate: text.toUpperCase()
  358. });
  359. // console.log(licensePlate);
  360. }}
  361. value={
  362. signUpFormData.licensePlate === '0000' || signUpFormData.licensePlate == ''
  363. ? ''
  364. : signUpFormData.licensePlate
  365. }
  366. // placeholder="車輛牌照號碼"
  367. placeholder={
  368. signUpFormData.licensePlate === '0000' || signUpFormData.licensePlate == ''
  369. ? '車輛牌照號碼'
  370. : signUpFormData.licensePlate
  371. }
  372. autoCapitalize="characters"
  373. placeholderTextColor="#bbbbbb"
  374. />
  375. </View>
  376. <NormalButton
  377. title={<Text style={{ color: '#fff' }}>下一步</Text>}
  378. onPress={handleNext}
  379. extendedStyle={{}}
  380. />
  381. {error && <Text style={styles.errorMessage}>{error}</Text>}
  382. <NormalButton
  383. title={<Text style={{ color: '#888888' }}>略過</Text>}
  384. onPress={goToNextPage}
  385. extendedStyle={{ backgroundColor: 'transparent' }}
  386. />
  387. </View>
  388. </View>
  389. </>
  390. );
  391. };
  392. const styles = StyleSheet.create({
  393. container: {
  394. flex: 1,
  395. marginHorizontal: 20
  396. },
  397. button: { flex: 1, gap: 10, marginTop: 5 },
  398. fakeTextInput: {
  399. maxWidth: '100%',
  400. fontSize: 16,
  401. borderWidth: 1,
  402. padding: 20,
  403. borderRadius: 12,
  404. borderColor: '#bbbbbb'
  405. },
  406. text: {
  407. fontSize: 20,
  408. paddingBottom: 10
  409. },
  410. errorMessage: {
  411. fontSize: 14,
  412. color: '#ff0033',
  413. fontWeight: '400',
  414. marginLeft: 10,
  415. marginTop: 10
  416. },
  417. checkbox: {
  418. margin: 8
  419. }
  420. });
  421. export default CarInformation;