createWallet.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. let customerData: any = {
  43. customerBaseInfo: {
  44. name: signUpFormData.nickName.trim(),
  45. email: signUpFormData.email,
  46. password: signUpFormData.password,
  47. gender: signUpFormData.gender,
  48. birthday: signUpFormData.birthDateDay + '/' + signUpFormData.birthDateMonth + '/11',
  49. address: signUpFormData.address,
  50. phone: parseInt(signUpFormData.phone, 10),
  51. isUberDriver: signUpFormData.isUberDriver,
  52. notify_session_id: expoPushToken?.data
  53. }
  54. };
  55. if (ModelID !== '' && BrandID !== '' && signUpFormData.licensePlate !== '') {
  56. customerData.customerCarInfo = {
  57. type_id: ModelID,
  58. brand_id: BrandID,
  59. licensePlate: signUpFormData.licensePlate
  60. };
  61. }
  62. const selectLabelShown = () => {
  63. if (signUpFormData.paymentMethod == null) {
  64. return null;
  65. } else if (signUpFormData.paymentMethod == creditCard) {
  66. return creditCard;
  67. } else if (signUpFormData.paymentMethod == weChatAliPay) {
  68. return weChatAliPay;
  69. }
  70. };
  71. const handleSelectedChange = (selectedLabel: string) => {
  72. setSignUpFormData({ ...signUpFormData, paymentMethod: selectedLabel });
  73. setError('');
  74. };
  75. const handleNextWithSkip = async () => {
  76. setIsLoading2(true);
  77. try {
  78. const result = await authenticationService.signUp(customerData);
  79. console.log('handleNextWithSkip in CreateWallet Page, i am sending this customerData', customerData);
  80. if (result === true) {
  81. goToNextPage();
  82. } else {
  83. Alert.alert('註冊錯誤', '註冊過程中出現錯誤,請稍後再試');
  84. }
  85. } catch (error) {
  86. console.error('Sign up error:', error);
  87. setError('註冊過程中出現錯誤,請稍後再試。');
  88. } finally {
  89. setIsLoading2(false);
  90. }
  91. };
  92. const handleNext = async () => {
  93. if (
  94. signUpFormData.email === '' ||
  95. signUpFormData.password === '' ||
  96. signUpFormData.phone === '' ||
  97. passwordConfirm === ''
  98. ) {
  99. setError('請確保所有資料都已填寫。');
  100. } else if (signUpFormData.password !== passwordConfirm) {
  101. Alert.alert('密碼錯誤', '兩次輸入的密碼不一致,請重新輸入。');
  102. } else {
  103. setError('');
  104. setIsLoading(true);
  105. try {
  106. const result = await authenticationService.signUp(customerData);
  107. console.log('handleNext in CreateWallet Page, i am sending this customerData', customerData);
  108. if (result) {
  109. goToNextPage();
  110. } else {
  111. Alert.alert('註冊錯誤', '註冊過程中出現錯誤,請稍後再試');
  112. }
  113. } catch (error) {
  114. setError('註冊過程中出現錯誤,請稍後再試。');
  115. console.error('Sign up error:', error);
  116. } finally {
  117. setIsLoading(false);
  118. }
  119. }
  120. };
  121. return (
  122. <>
  123. <View style={styles.container}>
  124. <Text style={styles.text}>請填妥以下資料</Text>
  125. <View
  126. style={{
  127. display: 'flex',
  128. flexDirection: 'column',
  129. gap: 10
  130. }}
  131. >
  132. <NormalInput
  133. placeholder="請填寫您的電子郵件"
  134. onChangeText={(email) =>
  135. setSignUpFormData({
  136. ...signUpFormData,
  137. email: email
  138. })
  139. }
  140. autoCapitalize="none"
  141. />
  142. <NormalInput
  143. placeholder="帳戶密碼"
  144. onChangeText={(text) => {
  145. setSignUpFormData({
  146. ...signUpFormData,
  147. password: text
  148. });
  149. }}
  150. secureTextEntry={!showPassword1}
  151. />
  152. <Pressable
  153. className="absolute right-4 top-0 bottom-0 justify-center"
  154. onPress={() => setShowPassword1(!showPassword1)}
  155. >
  156. <Ionicons name={showPassword1 ? 'eye-outline' : 'eye-off-outline'} size={24} color="#02677D" />
  157. </Pressable>
  158. <View className="mb-4">
  159. <NormalInput
  160. placeholder="請再次輸入帳戶密碼"
  161. onChangeText={(text) => setPasswordConfirm(text)}
  162. secureTextEntry={!showPassword2}
  163. />
  164. <Pressable
  165. className="absolute right-4 top-0 bottom-0 justify-center"
  166. onPress={() => setShowPassword2(!showPassword2)}
  167. >
  168. <Ionicons
  169. name={showPassword2 ? 'eye-outline' : 'eye-off-outline'}
  170. size={24}
  171. color="#02677D"
  172. />
  173. </Pressable>
  174. </View>
  175. {/* <SingleSelectButtonGroup
  176. options={options}
  177. onSelectionChange={handleSelectedChange}
  178. selectedOption={selectLabelShown()}
  179. /> */}
  180. </View>
  181. {error && <Text style={styles.errorMessage}>{error}</Text>}
  182. <View>
  183. <NormalButton
  184. title={isLoading ? <ActivityIndicator /> : <Text style={{ color: '#fff' }}>完成</Text>}
  185. onPress={handleNext}
  186. extendedStyle={{}}
  187. />
  188. {/* 收起按鈕 */}
  189. {/* <NormalButton
  190. title={isLoading2 ? <ActivityIndicator /> : <Text style={{ color: '#888888' }}>略過</Text>}
  191. onPress={handleNextWithSkip}
  192. extendedStyle={{ backgroundColor: 'transparent' }}
  193. buttonPressedStyle={{ backgroundColor: 'transparent' }}
  194. /> */}
  195. </View>
  196. </View>
  197. </>
  198. );
  199. };
  200. const styles = StyleSheet.create({
  201. container: {
  202. flex: 1,
  203. marginHorizontal: 20
  204. },
  205. text: {
  206. fontSize: 20,
  207. paddingBottom: 10
  208. },
  209. errorMessage: {
  210. fontSize: 14,
  211. color: '#ff0033',
  212. fontWeight: '400',
  213. marginLeft: 10,
  214. marginBottom: 10
  215. }
  216. });
  217. export default CreateWallet;