verification.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(signUpFormData.email, otp, setScreen, setError);
  24. }
  25. };
  26. const handleSubmitOtp = () => {
  27. if (signUpFormData.email) {
  28. if (canSendOtp) {
  29. setCanSendOtp(false);
  30. setLockEmailInput(true);
  31. authenticationService.sendOtpToSignUpEmail(signUpFormData.email);
  32. setTimeout(() => {
  33. setCanSendOtp(true);
  34. setLockEmailInput(false);
  35. }, 60000);
  36. setError('');
  37. } else {
  38. setError('請等待一分鐘後再重新發送。');
  39. }
  40. } else {
  41. setError('請確保所有資料都已填寫。');
  42. }
  43. };
  44. const handleChangePhoneNumber = () => {
  45. setLockEmailInput(false);
  46. };
  47. const otpButtonText = lockEmailInput ? (
  48. <Text style={{ color: '#fff' }}>重新發送</Text>
  49. ) : (
  50. <Text style={{ color: '#fff' }}>發送</Text>
  51. );
  52. return (
  53. <>
  54. <View style={styles.container}>
  55. <Text style={styles.text}>請驗證您的電子郵件</Text>
  56. <NormalInput
  57. placeholder="請填寫您的電子郵件"
  58. onChangeText={(email) =>
  59. setSignUpFormData({
  60. ...signUpFormData,
  61. email: email
  62. })
  63. }
  64. editable={!lockEmailInput}
  65. extendedStyle={{ opacity: !lockEmailInput ? 1 : 0.5 }}
  66. autoCapitalize="none"
  67. />
  68. <View
  69. style={{
  70. display: 'flex',
  71. flexDirection: 'row',
  72. paddingVertical: 10,
  73. gap: 10
  74. }}
  75. >
  76. <NumberInput placeholder="OTP驗證碼" onChangeText={setOtp} extendedStyle={{ flex: 1 }} />
  77. <NormalButton title={otpButtonText} onPress={handleSubmitOtp} extendedStyle={{ flex: 1 / 2 }} />
  78. </View>
  79. <NormalButton
  80. title={<Text style={{ color: '#fff' }}>驗證</Text>}
  81. onPress={() => {
  82. handleVerification();
  83. }}
  84. extendedStyle={{}}
  85. />
  86. {error && <Text style={styles.errorMessage}>{error}</Text>}
  87. <Pressable onPress={handleChangePhoneNumber}>
  88. <Text style={[styles.footer, { opacity: lockEmailInput ? 1 : 0 }]}>修改電子郵件</Text>
  89. </Pressable>
  90. </View>
  91. </>
  92. );
  93. };
  94. const styles = StyleSheet.create({
  95. container: {
  96. flex: 1,
  97. marginHorizontal: 20
  98. },
  99. text: {
  100. fontSize: 20,
  101. paddingBottom: 10
  102. },
  103. errorMessage: {
  104. fontSize: 16,
  105. color: '#ff0033',
  106. fontWeight: '400',
  107. paddingVertical: 10
  108. },
  109. footer: { color: '#02677D', fontSize: 16, paddingVertical: 10 }
  110. });
  111. export default Verification;