Skip to content

Commit

Permalink
feat/clinet: render error msg when email already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
apsinghdev committed Dec 17, 2024
1 parent 29f676f commit 492125b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
19 changes: 15 additions & 4 deletions clients/apps/web/src/components/Form/EmailUpdateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ import { useState } from 'react'
import { SubmitHandler, useForm } from 'react-hook-form'

interface EmailUpdateformProps {
returnTo?: string
returnTo?: string
onEmailUpdateRequest?: () => void
onEmailUpdateExists?: () => void
onEmailUpdateForm?: () => void
setErr?: (value: string | null) => void
}

const EmailUpdateForm: React.FC<EmailUpdateformProps> = ({
returnTo,
onEmailUpdateRequest
onEmailUpdateRequest,
onEmailUpdateExists,
onEmailUpdateForm,
setErr,
}) => {
const form = useForm<{ email: string }>()
const { control, handleSubmit, setError } = form
Expand All @@ -28,13 +34,18 @@ const EmailUpdateForm: React.FC<EmailUpdateformProps> = ({
const onSubmit: SubmitHandler<{ email: string }> = async ({ email }) => {
setLoading(true)
try {
sendEmailUpdate(email, returnTo)
await sendEmailUpdate(email, returnTo)
onEmailUpdateRequest?.()
} catch (e) {
if (e instanceof ResponseError) {
const body = await e.response.json()
if (e.response.status === 422) {
const validationErrors = body['detail'] as ValidationError[]
if (setErr) setErr(body['detail'][0].msg)
onEmailUpdateExists?.()
setTimeout(() => {
onEmailUpdateForm?.()
}, 6000)
setValidationErrors(validationErrors, setError)
} else if (body['detail']) {
setError('email', { message: body['detail'] })
Expand Down Expand Up @@ -84,4 +95,4 @@ const EmailUpdateForm: React.FC<EmailUpdateformProps> = ({
)
}

export default EmailUpdateForm
export default EmailUpdateForm
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ const AuthenticationSettings = () => {

const searchParams = useSearchParams()
const [updateEmailStage, setUpdateEmailStage] = useState<
'off' | 'form' | 'request' | 'verified'
'off' | 'form' | 'request' | 'verified' | 'exists'
>((searchParams.get('update_email') as 'verified' | null) || 'off')
const [userReloaded, setUserReloaded] = useState(false)
const [errMsg, setErrMsg] = useState<string | null>(null)

useEffect(() => {
if (!userReloaded && updateEmailStage === 'verified') {
Expand All @@ -156,7 +157,7 @@ const AuthenticationSettings = () => {
}, [updateEmailStage, reloadUser, userReloaded])

const updateEmailContent: Record<
'off' | 'form' | 'request' | 'verified',
'off' | 'form' | 'request' | 'verified' | 'exists',
React.ReactNode
> = {
off: (
Expand All @@ -180,6 +181,9 @@ const AuthenticationSettings = () => {
form: (
<EmailUpdateForm
onEmailUpdateRequest={() => setUpdateEmailStage('request')}
onEmailUpdateExists={() => setUpdateEmailStage('exists')}
onEmailUpdateForm={() => setUpdateEmailStage('form')}
setErr={setErrMsg}
/>
),
request: (
Expand All @@ -192,6 +196,11 @@ const AuthenticationSettings = () => {
Your email has been updated!
</div>
),
exists: (
<div className="text-center text-sm text-red-700 dark:text-red-500">
{errMsg}
</div>
),
}

return (
Expand Down
4 changes: 2 additions & 2 deletions clients/apps/web/src/hooks/emailUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export const useSendEmailUpdate = () => {
async (email: string, return_to?: string) => {
const body: EmailUpdateRequest = {
email,
return_to
return_to,
}
await api.emailUpdate.requestEmailUpdate({ body })
},
[router],
)
return func
}
}

0 comments on commit 492125b

Please sign in to comment.