createWallet.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { View, Text, StyleSheet, ActivityIndicator } from 'react-native';
  2. import NormalInput from '../../../global/normal_input';
  3. import NormalButton from '../../../global/normal_button';
  4. import { useState } from 'react';
  5. import SingleSelectButtonGroup from '../../../global/select_button';
  6. import useSignUpStore from '../../../../providers/signup_form_store';
  7. import PhoneInput from '../../../global/phone_input';
  8. import { authenticationService } from '../../../../service/authService';
  9. import { Alert } from 'react-native';
  10. import useVehicleStore from '../../../../providers/vehicle_store';
  11. type CreateWalletProps = {
  12. goToNextPage: () => void;
  13. };
  14. const creditCard = '信用卡';
  15. const weChatAliPay = '微信支付/支付寶';
  16. const CreateWallet: React.FC<CreateWalletProps> = ({ goToNextPage }) => {
  17. const options = [{ label: creditCard }, { label: weChatAliPay }];
  18. const [isLoading, setIsLoading] = useState(false);
  19. const [isLoading2, setIsLoading2] = useState(false);
  20. const { signUpFormData, setSignUpFormData } = useSignUpStore();
  21. const {
  22. vehicleBrand,
  23. vehicleModel,
  24. BrandID,
  25. ModelID,
  26. licensePlate,
  27. setVehicleBrand,
  28. setVehicleModel,
  29. setBrandID,
  30. setModelID,
  31. setLicensePlate
  32. } = useVehicleStore();
  33. const handleSelectedChange = (selectedLabel: string) => {
  34. setSignUpFormData({ ...signUpFormData, paymentMethod: selectedLabel });
  35. setError('');
  36. };
  37. const handleNextWithSkip = async () => {
  38. setIsLoading2(true);
  39. try {
  40. const result = await authenticationService.signUp(customerData);
  41. console.log('handleNextWithSkip in CreateWallet Page, i am sending this customerData', customerData);
  42. if (result === true) {
  43. goToNextPage();
  44. } else {
  45. Alert.alert('註冊錯誤', '註冊過程中出現錯誤,請稍後再試');
  46. }
  47. } catch (error) {
  48. console.error('Sign up error:', error);
  49. setError('註冊過程中出現錯誤,請稍後再試。');
  50. } finally {
  51. setIsLoading2(false);
  52. }
  53. };
  54. const handleNext = async () => {
  55. if (signUpFormData.paymentMethod === '' || signUpFormData.phone === '') {
  56. setError('請確保所有資料都已填寫。');
  57. } else {
  58. setError('');
  59. setIsLoading(true);
  60. try {
  61. const result = await authenticationService.signUp(customerData);
  62. console.log('handleNext in CreateWallet Page, i am sending this customerData', customerData);
  63. if (result) {
  64. goToNextPage();
  65. } else {
  66. Alert.alert('註冊錯誤', '註冊過程中出現錯誤,請稍後再試');
  67. }
  68. } catch (error) {
  69. setError('註冊過程中出現錯誤,請稍後再試。');
  70. console.error('Sign up error:', error);
  71. } finally {
  72. setIsLoading(false);
  73. }
  74. }
  75. };
  76. const selectLabelShown = () => {
  77. if (signUpFormData.paymentMethod == null) {
  78. return null;
  79. } else if (signUpFormData.paymentMethod == creditCard) {
  80. return creditCard;
  81. } else if (signUpFormData.paymentMethod == weChatAliPay) {
  82. return weChatAliPay;
  83. }
  84. };
  85. const phoneFieldPlaceholder = signUpFormData.phone ? signUpFormData.phone : '輸入電話號碼';
  86. const [error, setError] = useState('');
  87. let customerData = {
  88. customerBaseInfo: {
  89. name: signUpFormData.nickName.trim(),
  90. email: signUpFormData.email,
  91. password: signUpFormData.password,
  92. gender: signUpFormData.gender,
  93. birthday: signUpFormData.birthDateDay + '/' + signUpFormData.birthDateMonth + '/11',
  94. address: signUpFormData.address,
  95. phone: parseInt(signUpFormData.phone, 10),
  96. isUberDriver: signUpFormData.isUberDriver
  97. },
  98. customerCarInfo: {
  99. type_id: ModelID,
  100. brand_id: BrandID,
  101. licensePlate: signUpFormData.licensePlate
  102. }
  103. };
  104. return (
  105. <>
  106. <View style={styles.container}>
  107. <Text style={styles.text}>請填妥以下資料</Text>
  108. <View
  109. style={{
  110. display: 'flex',
  111. flexDirection: 'column',
  112. gap: 10
  113. }}
  114. >
  115. <PhoneInput
  116. value={signUpFormData.phone}
  117. onChangeText={(phone) => {
  118. setSignUpFormData({
  119. ...signUpFormData,
  120. phone: phone
  121. });
  122. }}
  123. placeholder={phoneFieldPlaceholder}
  124. />
  125. <SingleSelectButtonGroup
  126. options={options}
  127. onSelectionChange={handleSelectedChange}
  128. selectedOption={selectLabelShown()}
  129. />
  130. </View>
  131. {error && <Text style={styles.errorMessage}>{error}</Text>}
  132. <View>
  133. <NormalButton
  134. title={isLoading ? <ActivityIndicator /> : <Text style={{ color: '#fff' }}>完成</Text>}
  135. onPress={handleNext}
  136. extendedStyle={{}}
  137. />
  138. {/* 收起按鈕 */}
  139. {/* <NormalButton
  140. title={isLoading2 ? <ActivityIndicator /> : <Text style={{ color: '#888888' }}>略過</Text>}
  141. onPress={handleNextWithSkip}
  142. extendedStyle={{ backgroundColor: 'transparent' }}
  143. buttonPressedStyle={{ backgroundColor: 'transparent' }}
  144. /> */}
  145. </View>
  146. </View>
  147. </>
  148. );
  149. };
  150. const styles = StyleSheet.create({
  151. container: {
  152. flex: 1,
  153. marginHorizontal: 20
  154. },
  155. text: {
  156. fontSize: 20,
  157. paddingBottom: 10
  158. },
  159. errorMessage: {
  160. fontSize: 14,
  161. color: '#ff0033',
  162. fontWeight: '400',
  163. marginLeft: 10,
  164. marginBottom: 10
  165. }
  166. });
  167. export default CreateWallet;