22
33import * as Yup from 'yup'
44
5- import { Alert , AlertDescription } from '@/components/ui/alert'
65import { Formik , Form as FormikForm } from 'formik'
76import React , { useState } from 'react'
87import { apiStatusCodes , emailRegex } from '@/config/CommonConstant'
@@ -12,6 +11,7 @@ import {
1211 sendVerificationMail ,
1312} from '@/app/api/Auth'
1413
14+ import { AlertComponent } from '@/components/AlertComponent'
1515import { AxiosResponse } from 'axios'
1616import { Button } from '@/components/ui/button'
1717import { Input } from '@/components/ui/input'
@@ -30,12 +30,8 @@ export default function EmailVerificationForm({
3030} : StepEmailProps ) : React . ReactElement {
3131 const [ loading , setLoading ] = useState ( false )
3232 const [ verifyLoader , setVerifyLoader ] = useState ( false )
33-
34- const [ showEmailVerification , setShowEmailVerification ] = useState < {
35- message : string
36- isError : boolean
37- type : string
38- } > ( { message : '' , isError : false , type : '' } )
33+ const [ emailSuccess , setEmailSuccess ] = useState < string | null > ( null )
34+ const [ addFailure , setAddFailure ] = useState < string | null > ( null )
3935
4036 const validationSchema = Yup . object ( ) . shape ( {
4137 email : Yup . string ( )
@@ -58,34 +54,26 @@ export default function EmailVerificationForm({
5854 const { data } = userRsp as AxiosResponse
5955
6056 if ( data ?. statusCode === apiStatusCodes . API_STATUS_CREATED ) {
61- setShowEmailVerification ( {
62- message : data ?. message ,
63- isError : false ,
64- type : 'success' ,
65- } )
57+ setEmailSuccess ( data ?. message )
58+ setAddFailure ( null )
6659 } else {
67- setShowEmailVerification ( {
68- message : data ?. message ,
69- isError : true ,
70- type : 'danger' ,
71- } )
60+ setAddFailure ( userRsp as string )
61+ setEmailSuccess ( null )
7262 }
7363 } catch ( err ) {
7464 // eslint-disable-next-line no-console
7565 console . error ( 'Error during sending verification email:' , err )
76- setShowEmailVerification ( {
77- message : 'An error occurred while sending verification email.' ,
78- isError : true ,
79- type : 'danger' ,
80- } )
66+ setAddFailure ( 'An error occurred while sending verification email.' )
67+ setEmailSuccess ( null )
8168 } finally {
8269 setVerifyLoader ( false )
8370 }
8471 }
8572
8673 const handleVerifyEmail = async ( emailValue : string ) : Promise < void > => {
8774 setLoading ( true )
88- setShowEmailVerification ( { message : '' , isError : false , type : '' } )
75+ setEmailSuccess ( null )
76+ setAddFailure ( null )
8977
9078 try {
9179 const userRsp = await checkUserExist ( emailValue )
@@ -95,11 +83,7 @@ export default function EmailVerificationForm({
9583 if ( data ?. statusCode === apiStatusCodes . API_STATUS_SUCCESS ) {
9684 if ( isEmailVerified ) {
9785 if ( isRegistrationCompleted ) {
98- setShowEmailVerification ( {
99- message : 'Email is already registered.' ,
100- isError : true ,
101- type : 'danger' ,
102- } )
86+ setAddFailure ( data ?. data ?. message )
10387 } else {
10488 setEmail ( emailValue )
10589 goToNext ( )
@@ -108,20 +92,12 @@ export default function EmailVerificationForm({
10892 await handleSendVerificationEmail ( emailValue )
10993 }
11094 } else {
111- setShowEmailVerification ( {
112- message : data ?. data ?. message ?? 'Something went wrong.' ,
113- isError : true ,
114- type : 'danger' ,
115- } )
95+ setAddFailure ( data ?. data ?. message ?? 'Something went wrong.' )
11696 }
11797 } catch ( err ) {
11898 // eslint-disable-next-line no-console
11999 console . error ( 'Unexpected error during email verification:' , err )
120- setShowEmailVerification ( {
121- message : 'An unexpected error occurred. Please try again.' ,
122- isError : true ,
123- type : 'danger' ,
124- } )
100+ setAddFailure ( 'An unexpected error occurred. Please try again.' )
125101 } finally {
126102 setLoading ( false )
127103 }
@@ -147,29 +123,31 @@ export default function EmailVerificationForm({
147123
148124 return (
149125 < FormikForm className = "space-y-4" >
150- { showEmailVerification . message && (
151- < Alert
152- variant = {
153- showEmailVerification . type === 'success'
154- ? 'default'
155- : 'destructive'
156- }
157- className = { `mb-4 ${
158- showEmailVerification . type === 'success'
159- ? 'bg-success'
160- : 'bg-error'
161- } `}
162- >
163- < AlertDescription
164- className = {
165- showEmailVerification . type === 'success'
166- ? 'text-success'
167- : 'text-error'
168- }
169- >
170- { showEmailVerification . message }
171- </ AlertDescription >
172- </ Alert >
126+ { emailSuccess && (
127+ < div className = "w-full" role = "alert" >
128+ < AlertComponent
129+ message = { emailSuccess }
130+ type = { 'success' }
131+ onAlertClose = { ( ) => {
132+ if ( emailSuccess ) {
133+ setEmailSuccess ( null )
134+ }
135+ } }
136+ />
137+ </ div >
138+ ) }
139+ { addFailure && (
140+ < div className = "w-full" role = "alert" >
141+ < AlertComponent
142+ message = { addFailure }
143+ type = { 'failure' }
144+ onAlertClose = { ( ) => {
145+ if ( addFailure ) {
146+ setAddFailure ( null )
147+ }
148+ } }
149+ />
150+ </ div >
173151 ) }
174152
175153 < div className = "h-12" >
0 commit comments