createWallet.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { View, Text, StyleSheet } 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 { SignUp, authenticationService } from '../../../../service/authService';
  9. import { CustomerData } from '../../../../types/signUpFormData';
  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 { signUpFormData, setSignUpFormData } = useSignUpStore();
  18. const handleSelectedChange = (selectedLabel: string) => {
  19. setSignUpFormData({ ...signUpFormData, paymentMethod: selectedLabel });
  20. setError('');
  21. };
  22. const handleNextWithSkip = () => {
  23. authenticationService.signUp(customerData);
  24. goToNextPage();
  25. };
  26. const handleNext = () => {
  27. if (
  28. signUpFormData.paymentMethod === '' ||
  29. signUpFormData.phone === '' ||
  30. signUpFormData.address === ''
  31. ) {
  32. setError('請確保所有資料都已填寫。');
  33. } else {
  34. setError('');
  35. authenticationService.signUp(customerData);
  36. // SignUp(customerData);
  37. goToNextPage();
  38. }
  39. };
  40. const selectLabelShown = () => {
  41. if (signUpFormData.paymentMethod == null) {
  42. return null;
  43. } else if (signUpFormData.paymentMethod == creditCard) {
  44. return creditCard;
  45. } else if (signUpFormData.paymentMethod == weChatAliPay) {
  46. return weChatAliPay;
  47. }
  48. };
  49. const phoneFieldPlaceholder = signUpFormData.phone
  50. ? signUpFormData.phone
  51. : '輸入電話號碼';
  52. const [error, setError] = useState('');
  53. let customerData: CustomerData;
  54. if (signUpFormData.selectedCarModel === '') {
  55. // Empty SelectedCarModel means user has clicked "略過"
  56. customerData = {
  57. customerBaseInfo: {
  58. name: signUpFormData.nickName.trim(),
  59. email: signUpFormData.email,
  60. password: signUpFormData.password,
  61. gender: signUpFormData.gender,
  62. birthday: signUpFormData.birthDate,
  63. address: signUpFormData.address,
  64. phone: signUpFormData.phone,
  65. isUberDriver: signUpFormData.isUberDriver
  66. }
  67. };
  68. } else {
  69. customerData = {
  70. customerBaseInfo: {
  71. name: signUpFormData.nickName.trim(),
  72. email: signUpFormData.email,
  73. password: signUpFormData.password,
  74. gender: signUpFormData.gender,
  75. birthday: signUpFormData.birthDate,
  76. address: signUpFormData.address,
  77. phone: signUpFormData.phone,
  78. isUberDriver: signUpFormData.isUberDriver
  79. },
  80. customerCarInfo: {
  81. type_id: signUpFormData.selectedCarModel,
  82. brand_id: signUpFormData.selectedCarBrand,
  83. licensePlate: 'placeholder'
  84. }
  85. };
  86. }
  87. return (
  88. <>
  89. <View style={styles.container}>
  90. <Text style={styles.text}>請填妥以下資料</Text>
  91. <View
  92. style={{
  93. display: 'flex',
  94. flexDirection: 'column',
  95. gap: 10
  96. }}
  97. >
  98. <PhoneInput
  99. value={signUpFormData.phone}
  100. onChangeText={(phone) => {
  101. setSignUpFormData({
  102. ...signUpFormData,
  103. phone: phone
  104. });
  105. }}
  106. placeholder={phoneFieldPlaceholder}
  107. />
  108. <NormalInput
  109. placeholder="地址"
  110. onChangeText={(address) => {
  111. setSignUpFormData({
  112. ...signUpFormData,
  113. address: address
  114. });
  115. }}
  116. />
  117. <SingleSelectButtonGroup
  118. options={options}
  119. onSelectionChange={handleSelectedChange}
  120. shouldShowRedOutline={error ? true : false}
  121. selectedOption={selectLabelShown()}
  122. />
  123. <NormalButton
  124. title={<Text style={{ color: '#fff' }}>完成</Text>}
  125. onPress={handleNext}
  126. extendedStyle={{}}
  127. />
  128. <NormalButton
  129. title={<Text style={{ color: '#888888' }}>略過</Text>}
  130. onPress={handleNextWithSkip}
  131. extendedStyle={{ backgroundColor: 'transparent' }}
  132. />
  133. </View>
  134. {error && <Text style={styles.errorMessage}>{error}</Text>}
  135. </View>
  136. </>
  137. );
  138. };
  139. const styles = StyleSheet.create({
  140. container: {
  141. flex: 1,
  142. marginHorizontal: 20
  143. },
  144. text: {
  145. fontSize: 20,
  146. paddingBottom: 10
  147. },
  148. errorMessage: {
  149. fontSize: 14,
  150. color: '#ff0033',
  151. fontWeight: '400',
  152. marginLeft: 10,
  153. marginTop: 10
  154. }
  155. });
  156. export default CreateWallet;