bindingPhoneNumberPageStepTwo.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import { View, Text, StyleSheet, Pressable, Alert } from 'react-native';
  2. import { useEffect, useState } from 'react';
  3. import { forgetPasswordFormData, HandleForgetPasswordFormDataChange } from '../../../../types/signup';
  4. import NumberInput from '../../../global/number_input';
  5. import NormalButton from '../../../global/normal_button';
  6. import NormalInput from '../../../global/normal_input';
  7. import { authenticationService } from '../../../../service/authService';
  8. import PhoneInput from '../../../global/phone_input';
  9. import * as SecureStore from 'expo-secure-store';
  10. import { usePushNotifications } from '../../../../app/hooks/usePushNotifications';
  11. const BindingPhoneNumberPageStepTwo = ({ bindingFormData, setBindingFormData, setScreen }: any) => {
  12. const [authError, setAuthError] = useState('');
  13. const [error, setError] = useState('');
  14. const [canSendOtp, setCanSendOtp] = useState(true);
  15. const [lockEmailInput, setLockEmailInput] = useState(false);
  16. const [data, setData] = useState('');
  17. const [isLoading, setIsLoading] = useState(false);
  18. const [isLoading2, setIsLoading2] = useState(false);
  19. const [countdown, setCountdown] = useState(0);
  20. const { expoPushToken } = usePushNotifications();
  21. useEffect(() => {
  22. let timer: NodeJS.Timeout;
  23. if (countdown > 0) {
  24. timer = setInterval(() => {
  25. setCountdown((prev) => prev - 1);
  26. }, 1000);
  27. } else {
  28. setLockEmailInput(false); // Add this line to unlock input when countdown reaches 0
  29. }
  30. return () => {
  31. if (timer) clearInterval(timer);
  32. };
  33. }, [countdown]);
  34. // const handleVerification = async () => {
  35. // setIsLoading(true);
  36. // if (!forgetPasswordFormData.otp && !forgetPasswordFormData.email) {
  37. // setAuthError('請確保所有資料都已填寫');
  38. // } else if (!forgetPasswordFormData.otp) {
  39. // setAuthError('請輸入OTP驗證碼');
  40. // } else {
  41. // setAuthError('');
  42. // try {
  43. // const result =
  44. // await authenticationService.verifyingOtpForgetPassword(
  45. // forgetPasswordFormData.email.toLowerCase(),
  46. // forgetPasswordFormData.otp,
  47. // setForgetPasswordFormData,
  48. // setData
  49. // );
  50. // if (result === false) {
  51. // setAuthError('驗證OTP時發生錯誤,請稍後再試');
  52. // }
  53. // } catch (error) {
  54. // console.error('Error verifying OTP:', error);
  55. // setAuthError('驗證OTP時發生錯誤,請稍後再試');
  56. // }
  57. // }
  58. // setIsLoading(false);
  59. // };
  60. // const handleFormDataChange: HandleForgetPasswordFormDataChange = (
  61. // field,
  62. // value
  63. // ) => {
  64. // setForgetPasswordFormData((prevFormData) => ({
  65. // ...prevFormData,
  66. // [field]: value
  67. // }));
  68. // };
  69. // const handleSubmitOtp = () => {
  70. // if (forgetPasswordFormData.email) {
  71. // if (canSendOtp) {
  72. // const lowerCaseEmail =
  73. // forgetPasswordFormData.email.toLowerCase();
  74. // setCanSendOtp(false);
  75. // setLockEmailInput(true);
  76. // authenticationService.sendForgetPasswordOtp(lowerCaseEmail);
  77. // //can only request otp every 60 seconds
  78. // setTimeout(() => {
  79. // setCanSendOtp(true);
  80. // setLockEmailInput(false);
  81. // }, 60000);
  82. // setAuthError('');
  83. // } else {
  84. // setAuthError('請等待一分鐘後再重新發送。');
  85. // }
  86. // } else {
  87. // setAuthError('請確保所有資料都已填寫。');
  88. // }
  89. // };
  90. // const handleFinishResetPassword = async () => {
  91. // if (
  92. // forgetPasswordFormData.newPassword !==
  93. // forgetPasswordFormData.confirmedNewPassword
  94. // ) {
  95. // setError('請確保新密碼和確認密碼相同');
  96. // } else {
  97. // setError('');
  98. // setIsLoading2(true);
  99. // try {
  100. // const success = await authenticationService.changePassword(
  101. // forgetPasswordFormData.confirmedNewPassword,
  102. // data
  103. // );
  104. // if (success) {
  105. // setScreen(1);
  106. // } else {
  107. // setError('密碼重置失敗,請稍後再試');
  108. // }
  109. // } catch (error) {
  110. // console.error('Error changing password:', error);
  111. // setError('發生錯誤,請稍後再試');
  112. // } finally {
  113. // setIsLoading2(false);
  114. // }
  115. // }
  116. // };
  117. const handleConfirmBindingPhone = async () => {
  118. setIsLoading2(true);
  119. try {
  120. const result = await authenticationService.confirmBindingPhone(
  121. bindingFormData.phoneNumber,
  122. bindingFormData.otp,
  123. expoPushToken?.data ||
  124. `ExponentPushToken[TestToken${Math.random().toString(36).substring(2, 8).toUpperCase()}]`
  125. );
  126. if (result.status == 200) {
  127. setScreen(2);
  128. } else {
  129. setError('綁定手機號碼失敗,請檢查您輸入的OTP並再試一次');
  130. }
  131. } catch (error) {
  132. Alert.alert('綁定手機號碼失敗,請檢查您輸入的OTP並再試一次');
  133. } finally {
  134. setIsLoading2(false);
  135. }
  136. };
  137. const handleRequestOtp = async () => {
  138. try {
  139. //this check if phone same,
  140. const checkPhoneSame = await authenticationService.checkPhoneSame(bindingFormData.phoneNumber);
  141. if (checkPhoneSame?.status == 422) {
  142. //this means phone not same
  143. Alert.alert('確認您的手機號碼', '您以後將使用現在輸入的號碼進行登入。請按下確認表示您已知悉。', [
  144. {
  145. text: '確認並發出驗證碼',
  146. onPress: async () => {
  147. try {
  148. const token = await SecureStore.getItemAsync('accessToken');
  149. const changePhoneResult = await authenticationService.changePhone(
  150. bindingFormData.phoneNumber,
  151. token
  152. );
  153. if (changePhoneResult == true) {
  154. //this means change phone success
  155. try {
  156. const requestOtp = await authenticationService.checkPhoneSame(
  157. bindingFormData.phoneNumber
  158. );
  159. if (requestOtp.status == 200) {
  160. //this means request otp success
  161. setLockEmailInput(true);
  162. setCountdown(60);
  163. } else {
  164. setError('發送OTP失敗,請聯絡工作人員');
  165. }
  166. } catch (error) {}
  167. } else {
  168. //this means change phone failed
  169. Alert.alert('修改手機號碼失敗,請稍後再試');
  170. }
  171. } catch (error) {}
  172. }
  173. }
  174. ]);
  175. } else {
  176. //this means phone same
  177. if (checkPhoneSame?.status == 200) {
  178. //this means request otp success
  179. setLockEmailInput(true);
  180. setCountdown(60); // Start 60 second countdown
  181. } else {
  182. setError('發送OTP失敗,請稍後再試');
  183. }
  184. }
  185. } catch (error) {}
  186. };
  187. return (
  188. <>
  189. <View style={styles.container}>
  190. <View style={styles.bottomContainer}>
  191. <Text style={styles.text}>請輸入手機號碼,此手機號碼將用作登入用途</Text>
  192. <PhoneInput
  193. value={bindingFormData?.phoneNumber || ''}
  194. onChangeText={(phoneNumber) => {
  195. setBindingFormData({
  196. ...bindingFormData,
  197. phoneNumber: phoneNumber
  198. });
  199. }}
  200. placeholder="請輸入手機號碼"
  201. editable={!lockEmailInput}
  202. extendedStyle={{ opacity: !lockEmailInput ? 1 : 0.5 }}
  203. />
  204. {/* <NormalInput
  205. placeholder="請輸入手機號碼"
  206. onChangeText={(phoneNumber) =>
  207. setBindingFormData({
  208. ...bindingFormData,
  209. phoneNumber: phoneNumber
  210. })
  211. }
  212. // editable={!lockEmailInput}
  213. // extendedStyle={{ opacity: !lockEmailInput ? 1 : 0.5 }}
  214. /> */}
  215. <View
  216. style={{
  217. display: 'flex',
  218. flexDirection: 'row',
  219. paddingVertical: 10,
  220. gap: 10
  221. }}
  222. >
  223. <NumberInput
  224. placeholder="OTP驗證碼"
  225. onChangeText={(t) => setBindingFormData({ ...bindingFormData, otp: t })}
  226. // editable={!forgetPasswordFormData.otpAuthCompleted}
  227. extendedStyle={{
  228. flex: 1
  229. // opacity: !forgetPasswordFormData.otpAuthCompleted ? 1 : 0.5
  230. }}
  231. />
  232. <NormalButton
  233. title={
  234. <Text style={{ color: '#fff' }}>
  235. {lockEmailInput ? `已發送 (${countdown}s)` : '發送'}
  236. </Text>
  237. }
  238. onPress={handleRequestOtp}
  239. extendedStyle={{ flex: 1 / 2, opacity: lockEmailInput ? 0.5 : 1 }}
  240. disabled={lockEmailInput}
  241. />
  242. </View>
  243. <NormalButton
  244. title={<Text style={{ color: '#fff' }}>{isLoading2 ? '處理中...' : '確認'}</Text>}
  245. onPress={handleConfirmBindingPhone}
  246. // extendedStyle={
  247. // forgetPasswordFormData.otpAuthCompleted == true ? { backgroundColor: '#70787C' } : {}
  248. // }
  249. />
  250. {/* {lockEmailInput && (
  251. <Pressable
  252. // disabled={forgetPasswordFormData.otpAuthCompleted}
  253. onPress={() => setLockEmailInput(false)}
  254. >
  255. <Text
  256. style={[
  257. styles.footer
  258. // forgetPasswordFormData.otpAuthCompleted && {
  259. // opacity: 0.5
  260. // }
  261. ]}
  262. >
  263. 重新整理
  264. </Text>
  265. </Pressable>
  266. )} */}
  267. {/* {authError && <Text style={styles.errorMessage}>{authError}</Text>}
  268. {lockEmailInput && (
  269. <Pressable
  270. disabled={forgetPasswordFormData.otpAuthCompleted}
  271. onPress={() => setLockEmailInput(false)}
  272. >
  273. <Text
  274. style={[
  275. styles.footer,
  276. forgetPasswordFormData.otpAuthCompleted && {
  277. opacity: 0.5
  278. }
  279. ]}
  280. >
  281. 修改電子郵件
  282. </Text>
  283. </Pressable>
  284. )}
  285. {forgetPasswordFormData.otpAuthCompleted && (
  286. <View
  287. style={[
  288. styles.hiddenPasswordFields,
  289. forgetPasswordFormData.otpAuthCompleted ? styles.opacityFull : styles.opacityZero
  290. ]}
  291. >
  292. <NormalInput
  293. placeholder="新密碼"
  294. onChangeText={(t) => handleFormDataChange('newPassword', t)}
  295. secureTextEntry={true}
  296. textContentType={'oneTimeCode'}
  297. />
  298. <NormalInput
  299. placeholder="確認密碼"
  300. onChangeText={(t) => handleFormDataChange('confirmedNewPassword', t)}
  301. secureTextEntry={true}
  302. textContentType={'oneTimeCode'}
  303. />
  304. <NormalButton
  305. title={<Text style={{ color: '#fff' }}>{isLoading ? '重置中...' : '重置'}</Text>}
  306. onPress={handleFinishResetPassword}
  307. extendedStyle={{}}
  308. />
  309. </View>
  310. )} */}
  311. {error && <Text style={styles.errorMessage}>{error}</Text>}
  312. </View>
  313. </View>
  314. </>
  315. );
  316. };
  317. const styles = StyleSheet.create({
  318. container: {
  319. flex: 1,
  320. marginHorizontal: 20
  321. },
  322. titleText: {
  323. fontSize: 24,
  324. fontWeight: '300'
  325. },
  326. bottomContainer: {
  327. flex: 3,
  328. paddingBottom: 100
  329. },
  330. breakline: {
  331. width: 24,
  332. height: 1,
  333. backgroundColor: '#000000',
  334. marginVertical: 17
  335. },
  336. text: {
  337. fontSize: 18,
  338. paddingBottom: 10
  339. },
  340. hiddenPasswordFields: {
  341. gap: 10,
  342. paddingTop: 10
  343. },
  344. opacityZero: {
  345. opacity: 0
  346. },
  347. opacityFull: {
  348. opacity: 1
  349. },
  350. errorMessage: {
  351. fontSize: 14,
  352. color: '#ff0033',
  353. fontWeight: '400',
  354. marginLeft: 10,
  355. marginTop: 10
  356. },
  357. footer: { color: '#02677D', fontSize: 16, paddingVertical: 10 }
  358. });
  359. export default BindingPhoneNumberPageStepTwo;