createWallet.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { View, Text, StyleSheet, ActivityIndicator, Pressable } from 'react-native';
  2. import NormalInput from '../../../global/normal_input';
  3. import NormalButton from '../../../global/normal_button';
  4. import { Ionicons } from '@expo/vector-icons';
  5. import { useState } from 'react';
  6. import SingleSelectButtonGroup from '../../../global/select_button';
  7. import useSignUpStore from '../../../../providers/signup_form_store';
  8. import PhoneInput from '../../../global/phone_input';
  9. import { authenticationService } from '../../../../service/authService';
  10. import { Alert } from 'react-native';
  11. import useVehicleStore from '../../../../providers/vehicle_store';
  12. import { usePushNotifications } from '../../../../app/hooks/usePushNotifications';
  13. type CreateWalletProps = {
  14. goToNextPage: () => void;
  15. };
  16. const creditCard = '信用卡';
  17. const weChatAliPay = '微信支付/支付寶';
  18. const CreateWallet: React.FC<CreateWalletProps> = ({ goToNextPage }) => {
  19. const { expoPushToken } = usePushNotifications();
  20. const options = [{ label: creditCard }, { label: weChatAliPay }];
  21. const [isLoading, setIsLoading] = useState(false);
  22. const [isLoading2, setIsLoading2] = useState(false);
  23. const { signUpFormData, setSignUpFormData } = useSignUpStore();
  24. const phoneFieldPlaceholder = signUpFormData.phone ? signUpFormData.phone : '輸入電話號碼';
  25. const [error, setError] = useState('');
  26. const [showPassword1, setShowPassword1] = useState(false);
  27. const [showPassword2, setShowPassword2] = useState(false);
  28. const [passwordConfirm, setPasswordConfirm] = useState('');
  29. const {
  30. vehicleBrand,
  31. vehicleModel,
  32. BrandID,
  33. ModelID,
  34. licensePlate,
  35. setVehicleBrand,
  36. setVehicleModel,
  37. setBrandID,
  38. setModelID,
  39. setLicensePlate
  40. } = useVehicleStore();
  41. // console.log('expoPushToken?.data', expoPushToken?.data);
  42. console.log('expoPushToken?.data', expoPushToken?.data);
  43. let customerData: any = {
  44. customerBaseInfo: {
  45. name: signUpFormData.nickName.trim(),
  46. email: signUpFormData.email,
  47. password: signUpFormData.password,
  48. gender: signUpFormData.gender,
  49. birthday: signUpFormData.birthDateDay + '/' + signUpFormData.birthDateMonth + '/11',
  50. address: signUpFormData.address,
  51. phone: parseInt(signUpFormData.phone, 10),
  52. isUberDriver: signUpFormData.isUberDriver,
  53. notify_session_id:
  54. expoPushToken?.data ||
  55. `ExponentPushToken[TestToken${Math.random().toString(36).substring(2, 8).toUpperCase()}]`
  56. }
  57. };
  58. if (ModelID !== '' && BrandID !== '' && signUpFormData.licensePlate !== '') {
  59. customerData.customerCarInfo = {
  60. type_id: ModelID,
  61. brand_id: BrandID,
  62. licensePlate: signUpFormData.licensePlate
  63. };
  64. }
  65. const selectLabelShown = () => {
  66. if (signUpFormData.paymentMethod == null) {
  67. return null;
  68. } else if (signUpFormData.paymentMethod == creditCard) {
  69. return creditCard;
  70. } else if (signUpFormData.paymentMethod == weChatAliPay) {
  71. return weChatAliPay;
  72. }
  73. };
  74. const handleSelectedChange = (selectedLabel: string) => {
  75. setSignUpFormData({ ...signUpFormData, paymentMethod: selectedLabel });
  76. setError('');
  77. };
  78. const handleNextWithSkip = async () => {
  79. setIsLoading2(true);
  80. try {
  81. const result = await authenticationService.signUp(customerData);
  82. console.log('handleNextWithSkip in CreateWallet Page, i am sending this customerData', customerData);
  83. if (result === true) {
  84. goToNextPage();
  85. } else {
  86. Alert.alert('註冊錯誤', '註冊過程中出現錯誤,請稍後再試');
  87. }
  88. } catch (error) {
  89. console.error('Sign up error:', error);
  90. setError('註冊過程中出現錯誤,請稍後再試。');
  91. } finally {
  92. setIsLoading2(false);
  93. }
  94. };
  95. const handleNext = async () => {
  96. if (
  97. signUpFormData.email === '' ||
  98. signUpFormData.password === '' ||
  99. signUpFormData.phone === '' ||
  100. passwordConfirm === ''
  101. ) {
  102. setError('請確保所有資料都已填寫。');
  103. } else if (signUpFormData.password !== passwordConfirm) {
  104. Alert.alert('密碼錯誤', '兩次輸入的密碼不一致,請重新輸入。');
  105. } else {
  106. setError('');
  107. setIsLoading(true);
  108. try {
  109. const result = await authenticationService.signUp(customerData);
  110. console.log('handleNext in CreateWallet Page, i am sending this customerData', customerData);
  111. if (result) {
  112. goToNextPage();
  113. } else {
  114. Alert.alert('註冊錯誤', '註冊過程中出現錯誤,請稍後再試');
  115. }
  116. } catch (error) {
  117. setError('註冊過程中出現錯誤,請稍後再試。');
  118. console.error('Sign up error:', error);
  119. } finally {
  120. setIsLoading(false);
  121. }
  122. }
  123. };
  124. return (
  125. <>
  126. <View style={styles.container}>
  127. <Text style={styles.text}>請填妥以下資料</Text>
  128. <View
  129. style={{
  130. display: 'flex',
  131. flexDirection: 'column',
  132. gap: 10
  133. }}
  134. >
  135. <NormalInput
  136. placeholder="請填寫您的電子郵件"
  137. onChangeText={(email) =>
  138. setSignUpFormData({
  139. ...signUpFormData,
  140. email: email
  141. })
  142. }
  143. autoCapitalize="none"
  144. />
  145. <NormalInput
  146. placeholder="帳戶密碼"
  147. onChangeText={(text) => {
  148. setSignUpFormData({
  149. ...signUpFormData,
  150. password: text
  151. });
  152. }}
  153. secureTextEntry={!showPassword1}
  154. />
  155. <Pressable
  156. className="absolute right-4 top-0 bottom-0 justify-center"
  157. onPress={() => setShowPassword1(!showPassword1)}
  158. >
  159. <Ionicons name={showPassword1 ? 'eye-outline' : 'eye-off-outline'} size={24} color="#02677D" />
  160. </Pressable>
  161. <View className="mb-4">
  162. <NormalInput
  163. placeholder="請再次輸入帳戶密碼"
  164. onChangeText={(text) => setPasswordConfirm(text)}
  165. secureTextEntry={!showPassword2}
  166. />
  167. <Pressable
  168. className="absolute right-4 top-0 bottom-0 justify-center"
  169. onPress={() => setShowPassword2(!showPassword2)}
  170. >
  171. <Ionicons
  172. name={showPassword2 ? 'eye-outline' : 'eye-off-outline'}
  173. size={24}
  174. color="#02677D"
  175. />
  176. </Pressable>
  177. </View>
  178. {/* <SingleSelectButtonGroup
  179. options={options}
  180. onSelectionChange={handleSelectedChange}
  181. selectedOption={selectLabelShown()}
  182. /> */}
  183. </View>
  184. {error && <Text style={styles.errorMessage}>{error}</Text>}
  185. <View>
  186. <NormalButton
  187. title={isLoading ? <ActivityIndicator /> : <Text style={{ color: '#fff' }}>完成</Text>}
  188. onPress={handleNext}
  189. extendedStyle={{}}
  190. />
  191. {/* 收起按鈕 */}
  192. {/* <NormalButton
  193. title={isLoading2 ? <ActivityIndicator /> : <Text style={{ color: '#888888' }}>略過</Text>}
  194. onPress={handleNextWithSkip}
  195. extendedStyle={{ backgroundColor: 'transparent' }}
  196. buttonPressedStyle={{ backgroundColor: 'transparent' }}
  197. /> */}
  198. </View>
  199. </View>
  200. </>
  201. );
  202. };
  203. const styles = StyleSheet.create({
  204. container: {
  205. flex: 1,
  206. marginHorizontal: 20
  207. },
  208. text: {
  209. fontSize: 20,
  210. paddingBottom: 10
  211. },
  212. errorMessage: {
  213. fontSize: 14,
  214. color: '#ff0033',
  215. fontWeight: '400',
  216. marginLeft: 10,
  217. marginBottom: 10
  218. }
  219. });
  220. export default CreateWallet;