verification.tsx 3.1 KB

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