@@ -10,7 +10,9 @@ import type { TFuncKey } from 'i18next';
1010import { useCallback , useContext , useEffect , useState } from 'react' ;
1111import { useTranslation } from 'react-i18next' ;
1212
13+ import LoadingContext from '@ac/Providers/LoadingContextProvider/LoadingContext' ;
1314import PageContext from '@ac/Providers/PageContextProvider/PageContext' ;
15+ import useApi from '@ac/hooks/use-api' ;
1416import SecondaryPageLayout from '@ac/layouts/SecondaryPageLayout' ;
1517
1618import styles from './index.module.scss' ;
@@ -59,10 +61,11 @@ const CodeVerification = ({
5961 const { t } = useTranslation ( ) ;
6062 const { getAccessToken } = useLogto ( ) ;
6163 const { setToast, setVerificationId } = useContext ( PageContext ) ;
64+ const { loading } = useContext ( LoadingContext ) ;
65+ const sendCodeRequest = useApi ( sendCode ) ;
66+ const verifyCodeRequest = useApi ( verifyCode ) ;
6267 const [ codeInput , setCodeInput ] = useState < string [ ] > ( [ ] ) ;
6368 const [ errorMessage , setErrorMessage ] = useState < string > ( ) ;
64- const [ isSending , setIsSending ] = useState ( false ) ;
65- const [ isVerifying , setIsVerifying ] = useState ( false ) ;
6669 const [ countdown , setCountdown ] = useState ( 0 ) ;
6770 const [ pendingVerificationRecord , setPendingVerificationRecord ] = useState < {
6871 recordId : string ;
@@ -96,7 +99,7 @@ const CodeVerification = ({
9699 } , [ countdown ] ) ;
97100
98101 const handleSendCode = useCallback ( async ( ) => {
99- if ( ! identifier || isSending ) {
102+ if ( ! identifier || loading ) {
100103 return ;
101104 }
102105
@@ -106,33 +109,30 @@ const CodeVerification = ({
106109 return ;
107110 }
108111
109- setIsSending ( true ) ;
112+ const [ error , result ] = await sendCodeRequest ( accessToken , identifier ) ;
110113
111- try {
112- const result = await sendCode ( accessToken , identifier ) ;
113-
114- setPendingVerificationRecord ( {
115- recordId : result . verificationRecordId ,
116- expiresAt : result . expiresAt ,
117- } ) ;
118- setCodeInput ( [ ] ) ;
119- setErrorMessage ( undefined ) ;
120- setHasSentCode ( true ) ;
121- startCountdown ( ) ;
122- } catch {
114+ if ( error || ! result ) {
123115 setToast ( t ( 'account_center.email_verification.error_send_failed' ) ) ;
124- } finally {
125- setIsSending ( false ) ;
116+ return ;
126117 }
127- } , [ getAccessToken , identifier , isSending , sendCode , setToast , startCountdown , t ] ) ;
118+
119+ setPendingVerificationRecord ( {
120+ recordId : result . verificationRecordId ,
121+ expiresAt : result . expiresAt ,
122+ } ) ;
123+ setCodeInput ( [ ] ) ;
124+ setErrorMessage ( undefined ) ;
125+ setHasSentCode ( true ) ;
126+ startCountdown ( ) ;
127+ } , [ getAccessToken , identifier , loading , sendCodeRequest , setToast , startCountdown , t ] ) ;
128128
129129 const handleVerify = useCallback (
130130 async ( code : string [ ] ) => {
131131 if (
132132 ! identifier ||
133133 ! pendingVerificationRecord ?. recordId ||
134134 ! pendingVerificationRecord . expiresAt ||
135- isVerifying ||
135+ loading ||
136136 ! isCodeReady ( code )
137137 ) {
138138 return ;
@@ -146,32 +146,29 @@ const CodeVerification = ({
146146 return ;
147147 }
148148
149- setIsVerifying ( true ) ;
150-
151- try {
152- await verifyCode ( accessToken , {
153- verificationRecordId : recordId ,
154- code : code . join ( '' ) ,
155- identifier,
156- } ) ;
149+ const [ error ] = await verifyCodeRequest ( accessToken , {
150+ verificationRecordId : recordId ,
151+ code : code . join ( '' ) ,
152+ identifier,
153+ } ) ;
157154
158- setVerificationId ( recordId , expiresAt ) ;
159- setPendingVerificationRecord ( undefined ) ;
160- } catch {
155+ if ( error ) {
161156 setCodeInput ( [ ] ) ;
162157 setErrorMessage ( t ( 'account_center.email_verification.error_verify_failed' ) ) ;
163- } finally {
164- setIsVerifying ( false ) ;
158+ return ;
165159 }
160+
161+ setVerificationId ( recordId , expiresAt ) ;
162+ setPendingVerificationRecord ( undefined ) ;
166163 } ,
167164 [
168165 getAccessToken ,
169166 identifier ,
170- isVerifying ,
167+ loading ,
171168 pendingVerificationRecord ,
172169 setVerificationId ,
173170 t ,
174- verifyCode ,
171+ verifyCodeRequest ,
175172 ]
176173 ) ;
177174
@@ -225,7 +222,7 @@ const CodeVerification = ({
225222 < button
226223 className = { styles . resendButton }
227224 type = "button"
228- disabled = { isSending }
225+ disabled = { loading }
229226 onClick = { ( ) => {
230227 void handleSendCode ( ) ;
231228 } }
@@ -238,7 +235,7 @@ const CodeVerification = ({
238235 title = "action.confirm"
239236 type = "primary"
240237 className = { styles . submit }
241- isLoading = { isVerifying }
238+ isLoading = { loading }
242239 onClick = { ( ) => {
243240 if ( ! isCodeReady ( codeInput ) ) {
244241 setErrorMessage ( t ( 'error.invalid_passcode' ) ) ;
@@ -263,7 +260,8 @@ const CodeVerification = ({
263260 title = "account_center.email_verification.send"
264261 type = "primary"
265262 className = { styles . prepareAction }
266- isLoading = { isSending }
263+ disabled = { loading }
264+ isLoading = { loading }
267265 onClick = { ( ) => {
268266 void handleSendCode ( ) ;
269267 } }
0 commit comments