verification.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { View, Text, StyleSheet, TextInput, Pressable } from 'react-native';
  2. import { useState } from 'react';
  3. import PhoneInput from '../../../global/phone_input';
  4. import NumberInput from '../../../global/number_input';
  5. import NormalButton from '../../../global/normal_button';
  6. import useSignUpStore from '../../../../providers/signup_form_store';
  7. import NormalInput from '../../../global/normal_input';
  8. import { authenticationService } from '../../../../service/authService';
  9. type VerificationProps = {
  10. setScreen: React.Dispatch<React.SetStateAction<number>>;
  11. };
  12. const Verification: React.FC<VerificationProps> = ({ setScreen }) => {
  13. const { signUpFormData, setSignUpFormData } = useSignUpStore();
  14. const [error, setError] = useState('');
  15. const [otp, setOtp] = useState('');
  16. const [canSendOtp, setCanSendOtp] = useState(true);
  17. const [lockEmailInput, setLockEmailInput] = useState(false);
  18. const handleVerification = async () => {
  19. if (signUpFormData.email === '' || otp === '') {
  20. setError('請確保所有資料都已填寫。');
  21. } else {
  22. setError('');
  23. authenticationService.verifySignUpOtp(
  24. signUpFormData.email.toLowerCase(),
  25. otp,
  26. setScreen,
  27. setError
  28. );
  29. }
  30. };
  31. const handleSubmitOtp = () => {
  32. if (signUpFormData.email) {
  33. if (canSendOtp) {
  34. setCanSendOtp(false);
  35. setLockEmailInput(true);
  36. authenticationService.sendOtpToSignUpEmail(
  37. signUpFormData.email.toLowerCase()
  38. );
  39. setTimeout(() => {
  40. setCanSendOtp(true);
  41. setLockEmailInput(false);
  42. }, 60000);
  43. setError('');
  44. } else {
  45. setError('請等待一分鐘後再重新發送。');
  46. }
  47. } else {
  48. setError('請確保所有資料都已填寫。');
  49. }
  50. };
  51. const handleChangePhoneNumber = () => {
  52. setLockEmailInput(false);
  53. };
  54. const otpButtonText = lockEmailInput ? (
  55. <Text style={{ color: '#fff' }}>重新發送</Text>
  56. ) : (
  57. <Text style={{ color: '#fff' }}>發送</Text>
  58. );
  59. return (
  60. <>
  61. <View style={styles.container}>
  62. <Text style={styles.text}>請驗證您的電子郵件</Text>
  63. <NormalInput
  64. placeholder="請填寫您的電子郵件"
  65. onChangeText={(email) =>
  66. setSignUpFormData({
  67. ...signUpFormData,
  68. email: email
  69. })
  70. }
  71. editable={!lockEmailInput}
  72. extendedStyle={{ opacity: !lockEmailInput ? 1 : 0.5 }}
  73. />
  74. <View
  75. style={{
  76. display: 'flex',
  77. flexDirection: 'row',
  78. paddingVertical: 10,
  79. gap: 10
  80. }}
  81. >
  82. <NumberInput
  83. placeholder="OTP驗證碼"
  84. onChangeText={setOtp}
  85. extendedStyle={{ flex: 1 }}
  86. />
  87. <NormalButton
  88. title={otpButtonText}
  89. onPress={handleSubmitOtp}
  90. extendedStyle={{ flex: 1 / 2 }}
  91. />
  92. </View>
  93. <NormalButton
  94. title={<Text style={{ color: '#fff' }}>驗證</Text>}
  95. onPress={() => {
  96. handleVerification();
  97. }}
  98. extendedStyle={{}}
  99. />
  100. {error && <Text style={styles.errorMessage}>{error}</Text>}
  101. <Pressable onPress={handleChangePhoneNumber}>
  102. <Text
  103. style={[
  104. styles.footer,
  105. { opacity: lockEmailInput ? 1 : 0 }
  106. ]}
  107. >
  108. 修改電子郵件
  109. </Text>
  110. </Pressable>
  111. </View>
  112. </>
  113. );
  114. };
  115. const styles = StyleSheet.create({
  116. container: {
  117. flex: 1,
  118. marginHorizontal: 20
  119. },
  120. text: {
  121. fontSize: 20,
  122. paddingBottom: 10
  123. },
  124. errorMessage: {
  125. fontSize: 16,
  126. color: '#ff0033',
  127. fontWeight: '400',
  128. paddingVertical: 10
  129. },
  130. footer: { color: '#02677D', fontSize: 16, paddingVertical: 10 }
  131. });
  132. export default Verification;