createWallet.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. type CreateWalletProps = {
  11. goToNextPage: () => void;
  12. };
  13. const creditCard = '信用卡';
  14. const weChatAliPay = '微信支付/支付寶';
  15. const CreateWallet: React.FC<CreateWalletProps> = ({ goToNextPage }) => {
  16. const options = [{ label: creditCard }, { label: weChatAliPay }];
  17. const [isLoading, setIsLoading] = useState(false);
  18. const [isLoading2, setIsLoading2] = useState(false);
  19. const { signUpFormData, setSignUpFormData } = useSignUpStore();
  20. const handleSelectedChange = (selectedLabel: string) => {
  21. setSignUpFormData({ ...signUpFormData, paymentMethod: selectedLabel });
  22. setError('');
  23. };
  24. const handleNextWithSkip = async () => {
  25. setIsLoading2(true);
  26. try {
  27. const result = await authenticationService.signUp(customerData);
  28. if (result === true) {
  29. goToNextPage();
  30. } else {
  31. Alert.alert('註冊錯誤', '註冊過程中出現錯誤,請稍後再試');
  32. }
  33. } catch (error) {
  34. console.error('Sign up error:', error);
  35. setError('註冊過程中出現錯誤,請稍後再試。');
  36. } finally {
  37. setIsLoading2(false);
  38. }
  39. };
  40. const handleNext = async () => {
  41. if (
  42. signUpFormData.paymentMethod === '' ||
  43. signUpFormData.phone === '' ||
  44. signUpFormData.address === ''
  45. ) {
  46. setError('請確保所有資料都已填寫。');
  47. } else {
  48. setError('');
  49. setIsLoading(true);
  50. try {
  51. const result = await authenticationService.signUp(customerData);
  52. if (result === true) {
  53. goToNextPage();
  54. } else {
  55. Alert.alert('註冊錯誤', '註冊過程中出現錯誤,請稍後再試');
  56. }
  57. } catch (error) {
  58. setError('註冊過程中出現錯誤,請稍後再試。');
  59. console.error('Sign up error:', error);
  60. } finally {
  61. setIsLoading(false);
  62. }
  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 phoneFieldPlaceholder = signUpFormData.phone
  75. ? signUpFormData.phone
  76. : '輸入電話號碼';
  77. const [error, setError] = useState('');
  78. let customerData = {
  79. customerBaseInfo: {
  80. name: signUpFormData.nickName.trim(),
  81. email: signUpFormData.email,
  82. password: signUpFormData.password,
  83. gender: signUpFormData.gender,
  84. birthday: signUpFormData.birthDate,
  85. address: signUpFormData.address,
  86. phone: signUpFormData.phone,
  87. isUberDriver: signUpFormData.isUberDriver
  88. },
  89. customerCarInfo: {
  90. type_id: signUpFormData.selectedCarModel,
  91. brand_id: signUpFormData.selectedCarBrand,
  92. licensePlate: signUpFormData.licensePlate
  93. }
  94. };
  95. return (
  96. <>
  97. <View style={styles.container}>
  98. <Text style={styles.text}>請填妥以下資料</Text>
  99. <View
  100. style={{
  101. display: 'flex',
  102. flexDirection: 'column',
  103. gap: 10
  104. }}
  105. >
  106. <PhoneInput
  107. value={signUpFormData.phone}
  108. onChangeText={(phone) => {
  109. setSignUpFormData({
  110. ...signUpFormData,
  111. phone: phone
  112. });
  113. }}
  114. placeholder={phoneFieldPlaceholder}
  115. />
  116. <NormalInput
  117. placeholder="地址"
  118. onChangeText={(address) => {
  119. setSignUpFormData({
  120. ...signUpFormData,
  121. address: address
  122. });
  123. }}
  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={
  135. isLoading ? (
  136. <ActivityIndicator />
  137. ) : (
  138. <Text style={{ color: '#fff' }}>完成</Text>
  139. )
  140. }
  141. onPress={handleNext}
  142. extendedStyle={{}}
  143. />
  144. <NormalButton
  145. title={
  146. isLoading2 ? (
  147. <ActivityIndicator />
  148. ) : (
  149. <Text style={{ color: '#888888' }}>略過</Text>
  150. )
  151. }
  152. onPress={handleNextWithSkip}
  153. extendedStyle={{ backgroundColor: 'transparent' }}
  154. buttonPressedStyle={{ backgroundColor: 'transparent' }}
  155. />
  156. </View>
  157. </View>
  158. </>
  159. );
  160. };
  161. const styles = StyleSheet.create({
  162. container: {
  163. flex: 1,
  164. marginHorizontal: 20
  165. },
  166. text: {
  167. fontSize: 20,
  168. paddingBottom: 10
  169. },
  170. errorMessage: {
  171. fontSize: 14,
  172. color: '#ff0033',
  173. fontWeight: '400',
  174. marginLeft: 10,
  175. marginBottom: 10
  176. }
  177. });
  178. export default CreateWallet;