verification.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. type VerificationProps = {
  8. setScreen: React.Dispatch<React.SetStateAction<number>>;
  9. };
  10. const Verification: React.FC<VerificationProps> = ({ setScreen }) => {
  11. const { signUpFormData, setSignUpFormData } = useSignUpStore();
  12. const [error, setError] = useState("");
  13. const [otp, setOtp] = useState("");
  14. const [canSendOtp, setCanSendOtp] = useState(true);
  15. const [lockPhoneInput, setLockPhoneInput] = useState(false);
  16. const handleSignUpPhoneChange = (phone: string) => {
  17. setSignUpFormData({ phone });
  18. if (signUpFormData.phone.length + 1 === 8) {
  19. setSignUpFormData({ phoneVerificationStatus: true });
  20. } else {
  21. setSignUpFormData({ phoneVerificationStatus: false });
  22. }
  23. };
  24. const handleVerification = () => {
  25. if (signUpFormData.phone === "" || otp === "") {
  26. setError("請確保所有資料都已填寫。");
  27. } else if (signUpFormData.phoneVerificationStatus === false) {
  28. setError("請確保電話號碼長度正確");
  29. } else {
  30. setError("");
  31. setScreen((currentScreenNumber) => currentScreenNumber + 1);
  32. }
  33. };
  34. const handleSubmitOtp = () => {
  35. if (signUpFormData.phoneVerificationStatus) {
  36. if (canSendOtp) {
  37. setCanSendOtp(false);
  38. setLockPhoneInput(true);
  39. console.log(lockPhoneInput);
  40. //can only request otp every 60 seconds
  41. setTimeout(() => {
  42. setCanSendOtp(true);
  43. setLockPhoneInput(false);
  44. }, 60000);
  45. setError("");
  46. } else {
  47. setError("請等待一分鐘後再重新發送。");
  48. }
  49. } else {
  50. setError("請確保所有資料都已填寫。");
  51. }
  52. };
  53. const handleChangePhoneNumber = () => {
  54. setLockPhoneInput(false);
  55. };
  56. const otpButtonText = lockPhoneInput ? (
  57. <Text style={{ color: "#fff" }}>重新發送</Text>
  58. ) : (
  59. <Text style={{ color: "#fff" }}>發送</Text>
  60. );
  61. const phoneFieldPlaceholder = signUpFormData.phone ? signUpFormData.phone : "輸入電話號碼";
  62. return (
  63. <>
  64. <View style={styles.container}>
  65. <Text style={styles.text}>請驗證您的電話號碼</Text>
  66. <PhoneInput
  67. value={signUpFormData.phone}
  68. onChangeText={handleSignUpPhoneChange}
  69. placeholder={phoneFieldPlaceholder}
  70. editable={!lockPhoneInput}
  71. extendedStyle={{ opacity: !lockPhoneInput ? 1 : 0.5 }}
  72. />
  73. <View
  74. style={{
  75. display: "flex",
  76. flexDirection: "row",
  77. paddingVertical: 10,
  78. gap: 10,
  79. }}
  80. >
  81. <NumberInput placeholder="OTP驗證碼" onChangeText={setOtp} extendedStyle={{ flex: 1 }} />
  82. <NormalButton title={otpButtonText} onPress={handleSubmitOtp} extendedStyle={{ flex: 1 / 2 }} />
  83. </View>
  84. <NormalButton
  85. title={<Text style={{ color: "#fff" }}>驗證</Text>}
  86. onPress={() => {
  87. handleVerification();
  88. }}
  89. extendedStyle={{}}
  90. />
  91. {error && <Text style={styles.errorMessage}>{error}</Text>}
  92. <Pressable onPress={handleChangePhoneNumber}>
  93. <Text style={[styles.footer, { opacity: lockPhoneInput ? 1 : 0 }]}>修改電話號碼</Text>
  94. </Pressable>
  95. </View>
  96. </>
  97. );
  98. };
  99. const styles = StyleSheet.create({
  100. container: {
  101. flex: 1,
  102. marginHorizontal: 20,
  103. },
  104. text: {
  105. fontSize: 20,
  106. paddingBottom: 10,
  107. },
  108. errorMessage: {
  109. fontSize: 14,
  110. color: "#ff0033",
  111. fontWeight: "400",
  112. marginLeft: 10,
  113. marginTop: 10,
  114. },
  115. footer: { color: "#02677D", fontSize: 16, paddingVertical: 10 },
  116. });
  117. export default Verification;