createWallet.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. if (result === true) {
  42. goToNextPage();
  43. } else {
  44. Alert.alert('註冊錯誤', '註冊過程中出現錯誤,請稍後再試');
  45. }
  46. } catch (error) {
  47. console.error('Sign up error:', error);
  48. setError('註冊過程中出現錯誤,請稍後再試。');
  49. } finally {
  50. setIsLoading2(false);
  51. }
  52. };
  53. const handleNext = async () => {
  54. if (signUpFormData.paymentMethod === '' || signUpFormData.phone === '' || signUpFormData.address === '') {
  55. setError('請確保所有資料都已填寫。');
  56. } else {
  57. setError('');
  58. setIsLoading(true);
  59. try {
  60. const result = await authenticationService.signUp(customerData);
  61. if (result === true) {
  62. goToNextPage();
  63. } else {
  64. Alert.alert('註冊錯誤', '註冊過程中出現錯誤,請稍後再試');
  65. }
  66. } catch (error) {
  67. setError('註冊過程中出現錯誤,請稍後再試。');
  68. console.error('Sign up error:', error);
  69. } finally {
  70. setIsLoading(false);
  71. }
  72. }
  73. };
  74. const selectLabelShown = () => {
  75. if (signUpFormData.paymentMethod == null) {
  76. return null;
  77. } else if (signUpFormData.paymentMethod == creditCard) {
  78. return creditCard;
  79. } else if (signUpFormData.paymentMethod == weChatAliPay) {
  80. return weChatAliPay;
  81. }
  82. };
  83. const phoneFieldPlaceholder = signUpFormData.phone ? signUpFormData.phone : '輸入電話號碼';
  84. const [error, setError] = useState('');
  85. let customerData = {
  86. customerBaseInfo: {
  87. name: signUpFormData.nickName.trim(),
  88. email: signUpFormData.email,
  89. password: signUpFormData.password,
  90. gender: signUpFormData.gender,
  91. birthday: signUpFormData.birthDate,
  92. address: signUpFormData.address,
  93. phone: signUpFormData.phone,
  94. isUberDriver: signUpFormData.isUberDriver
  95. },
  96. customerCarInfo: {
  97. type_id: ModelID,
  98. brand_id: BrandID,
  99. licensePlate: licensePlate
  100. }
  101. };
  102. return (
  103. <>
  104. <View style={styles.container}>
  105. <Text style={styles.text}>請填妥以下資料</Text>
  106. <View
  107. style={{
  108. display: 'flex',
  109. flexDirection: 'column',
  110. gap: 10
  111. }}
  112. >
  113. <PhoneInput
  114. value={signUpFormData.phone}
  115. onChangeText={(phone) => {
  116. setSignUpFormData({
  117. ...signUpFormData,
  118. phone: phone
  119. });
  120. }}
  121. placeholder={phoneFieldPlaceholder}
  122. />
  123. <NormalInput
  124. placeholder="地址"
  125. onChangeText={(address) => {
  126. setSignUpFormData({
  127. ...signUpFormData,
  128. address: address
  129. });
  130. }}
  131. />
  132. <SingleSelectButtonGroup
  133. options={options}
  134. onSelectionChange={handleSelectedChange}
  135. selectedOption={selectLabelShown()}
  136. />
  137. </View>
  138. {error && <Text style={styles.errorMessage}>{error}</Text>}
  139. <View>
  140. <NormalButton
  141. title={isLoading ? <ActivityIndicator /> : <Text style={{ color: '#fff' }}>完成</Text>}
  142. onPress={handleNext}
  143. extendedStyle={{}}
  144. />
  145. <NormalButton
  146. title={isLoading2 ? <ActivityIndicator /> : <Text style={{ color: '#888888' }}>略過</Text>}
  147. onPress={handleNextWithSkip}
  148. extendedStyle={{ backgroundColor: 'transparent' }}
  149. buttonPressedStyle={{ backgroundColor: 'transparent' }}
  150. />
  151. </View>
  152. </View>
  153. </>
  154. );
  155. };
  156. const styles = StyleSheet.create({
  157. container: {
  158. flex: 1,
  159. marginHorizontal: 20
  160. },
  161. text: {
  162. fontSize: 20,
  163. paddingBottom: 10
  164. },
  165. errorMessage: {
  166. fontSize: 14,
  167. color: '#ff0033',
  168. fontWeight: '400',
  169. marginLeft: 10,
  170. marginBottom: 10
  171. }
  172. });
  173. export default CreateWallet;