bindingPhoneNumberPageStepTwo.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. );
  125. if (result.status == 200) {
  126. setScreen(2);
  127. } else {
  128. setError('綁定手機號碼失敗,請檢查您輸入的OTP並再試一次');
  129. }
  130. } catch (error) {
  131. Alert.alert('綁定手機號碼失敗,請檢查您輸入的OTP並再試一次');
  132. } finally {
  133. setIsLoading2(false);
  134. }
  135. };
  136. const handleRequestOtp = async () => {
  137. try {
  138. //this check if phone same,
  139. const checkPhoneSame = await authenticationService.checkPhoneSame(bindingFormData.phoneNumber);
  140. console.log('checkPhoneSame', checkPhoneSame);
  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. console.log(error);
  168. }
  169. } else {
  170. //this means change phone failed
  171. Alert.alert('修改手機號碼失敗,請稍後再試');
  172. }
  173. } catch (error) {
  174. console.log(error);
  175. }
  176. }
  177. }
  178. ]);
  179. } else {
  180. //this means phone same
  181. if (checkPhoneSame?.status == 200) {
  182. //this means request otp success
  183. setLockEmailInput(true);
  184. setCountdown(60); // Start 60 second countdown
  185. } else {
  186. setError('發送OTP失敗,請稍後再試');
  187. }
  188. }
  189. } catch (error) {
  190. console.log(error);
  191. }
  192. };
  193. return (
  194. <>
  195. <View style={styles.container}>
  196. <View style={styles.bottomContainer}>
  197. <Text style={styles.text}>請輸入手機號碼,此手機號碼將用作登入用途</Text>
  198. <PhoneInput
  199. value={bindingFormData?.phoneNumber || ''}
  200. onChangeText={(phoneNumber) => {
  201. setBindingFormData({
  202. ...bindingFormData,
  203. phoneNumber: phoneNumber
  204. });
  205. }}
  206. placeholder="請輸入手機號碼"
  207. editable={!lockEmailInput}
  208. extendedStyle={{ opacity: !lockEmailInput ? 1 : 0.5 }}
  209. />
  210. {/* <NormalInput
  211. placeholder="請輸入手機號碼"
  212. onChangeText={(phoneNumber) =>
  213. setBindingFormData({
  214. ...bindingFormData,
  215. phoneNumber: phoneNumber
  216. })
  217. }
  218. // editable={!lockEmailInput}
  219. // extendedStyle={{ opacity: !lockEmailInput ? 1 : 0.5 }}
  220. /> */}
  221. <View
  222. style={{
  223. display: 'flex',
  224. flexDirection: 'row',
  225. paddingVertical: 10,
  226. gap: 10
  227. }}
  228. >
  229. <NumberInput
  230. placeholder="OTP驗證碼"
  231. onChangeText={(t) => setBindingFormData({ ...bindingFormData, otp: t })}
  232. // editable={!forgetPasswordFormData.otpAuthCompleted}
  233. extendedStyle={{
  234. flex: 1
  235. // opacity: !forgetPasswordFormData.otpAuthCompleted ? 1 : 0.5
  236. }}
  237. />
  238. <NormalButton
  239. title={
  240. <Text style={{ color: '#fff' }}>
  241. {lockEmailInput ? `已發送 (${countdown}s)` : '發送'}
  242. </Text>
  243. }
  244. onPress={handleRequestOtp}
  245. extendedStyle={{ flex: 1 / 2, opacity: lockEmailInput ? 0.5 : 1 }}
  246. disabled={lockEmailInput}
  247. />
  248. </View>
  249. <NormalButton
  250. title={<Text style={{ color: '#fff' }}>{isLoading2 ? '處理中...' : '確認'}</Text>}
  251. onPress={handleConfirmBindingPhone}
  252. // extendedStyle={
  253. // forgetPasswordFormData.otpAuthCompleted == true ? { backgroundColor: '#70787C' } : {}
  254. // }
  255. />
  256. {/* {lockEmailInput && (
  257. <Pressable
  258. // disabled={forgetPasswordFormData.otpAuthCompleted}
  259. onPress={() => setLockEmailInput(false)}
  260. >
  261. <Text
  262. style={[
  263. styles.footer
  264. // forgetPasswordFormData.otpAuthCompleted && {
  265. // opacity: 0.5
  266. // }
  267. ]}
  268. >
  269. 重新整理
  270. </Text>
  271. </Pressable>
  272. )} */}
  273. {/* {authError && <Text style={styles.errorMessage}>{authError}</Text>}
  274. {lockEmailInput && (
  275. <Pressable
  276. disabled={forgetPasswordFormData.otpAuthCompleted}
  277. onPress={() => setLockEmailInput(false)}
  278. >
  279. <Text
  280. style={[
  281. styles.footer,
  282. forgetPasswordFormData.otpAuthCompleted && {
  283. opacity: 0.5
  284. }
  285. ]}
  286. >
  287. 修改電子郵件
  288. </Text>
  289. </Pressable>
  290. )}
  291. {forgetPasswordFormData.otpAuthCompleted && (
  292. <View
  293. style={[
  294. styles.hiddenPasswordFields,
  295. forgetPasswordFormData.otpAuthCompleted ? styles.opacityFull : styles.opacityZero
  296. ]}
  297. >
  298. <NormalInput
  299. placeholder="新密碼"
  300. onChangeText={(t) => handleFormDataChange('newPassword', t)}
  301. secureTextEntry={true}
  302. textContentType={'oneTimeCode'}
  303. />
  304. <NormalInput
  305. placeholder="確認密碼"
  306. onChangeText={(t) => handleFormDataChange('confirmedNewPassword', t)}
  307. secureTextEntry={true}
  308. textContentType={'oneTimeCode'}
  309. />
  310. <NormalButton
  311. title={<Text style={{ color: '#fff' }}>{isLoading ? '重置中...' : '重置'}</Text>}
  312. onPress={handleFinishResetPassword}
  313. extendedStyle={{}}
  314. />
  315. </View>
  316. )} */}
  317. {error && <Text style={styles.errorMessage}>{error}</Text>}
  318. </View>
  319. </View>
  320. </>
  321. );
  322. };
  323. const styles = StyleSheet.create({
  324. container: {
  325. flex: 1,
  326. marginHorizontal: 20
  327. },
  328. titleText: {
  329. fontSize: 24,
  330. fontWeight: '300'
  331. },
  332. bottomContainer: {
  333. flex: 3,
  334. paddingBottom: 100
  335. },
  336. breakline: {
  337. width: 24,
  338. height: 1,
  339. backgroundColor: '#000000',
  340. marginVertical: 17
  341. },
  342. text: {
  343. fontSize: 18,
  344. paddingBottom: 10
  345. },
  346. hiddenPasswordFields: {
  347. gap: 10,
  348. paddingTop: 10
  349. },
  350. opacityZero: {
  351. opacity: 0
  352. },
  353. opacityFull: {
  354. opacity: 1
  355. },
  356. errorMessage: {
  357. fontSize: 14,
  358. color: '#ff0033',
  359. fontWeight: '400',
  360. marginLeft: 10,
  361. marginTop: 10
  362. },
  363. footer: { color: '#02677D', fontSize: 16, paddingVertical: 10 }
  364. });
  365. export default BindingPhoneNumberPageStepTwo;