Skip to content

Commit

Permalink
feat(platform):Create ui link for resend otp
Browse files Browse the repository at this point in the history
  • Loading branch information
Prakhargarg-2010196 committed Oct 14, 2024
1 parent f4af874 commit a1cca73
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion apps/platform/src/app/auth/otp/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default function AuthOTPPage(): React.JSX.Element {
const [otp, setOtp] = useState<string>('')
const [isLoading, setIsLoading] = useState<boolean>(false)
const [isInvalidOtp, setIsInvalidOtp] = useState<boolean>(false)
const [isLoadingRefresh, setIsLoadingRefresh] = useState<boolean>(false)

const router = useRouter()

Expand Down Expand Up @@ -88,7 +89,28 @@ export default function AuthOTPPage(): React.JSX.Element {
}
}
}

const handleResendOtp = async (userEmail: string): Promise<void> => {
try {
setIsLoadingRefresh(true)
const response = await fetch(
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/auth/resend-otp/${encodeURIComponent(userEmail)}`,
{
method: 'POST'
}
)
if (response.status === 429) {
toast.error("Couldn't send OTP, too many requests")
setIsLoadingRefresh(false)
} else if (response.ok)
toast.success('OTP successfully sent to your email')
setIsLoadingRefresh(false)
} catch (error) {
// eslint-disable-next-line no-console -- we need to log the error
console.error(`Failed to send OTP: ${error}`)
toast.error("Couldn't send OTP .")
setIsLoadingRefresh(false)
}
}
return (
<main className="flex h-dvh items-center justify-center justify-items-center px-4">
<div className="flex flex-col gap-6">
Expand Down Expand Up @@ -144,6 +166,28 @@ export default function AuthOTPPage(): React.JSX.Element {
>
{isLoading ? <LoadingSVG className="w-10" /> : 'Verify'}
</Button>
<div className="space-x-reverse-2 flex items-center justify-center text-[#71717A]">
<span>Didn’t receive OTP?</span>
<span>
{isLoadingRefresh ? (
<span>
<LoadingSVG className="w-10 h-10" />
</span>
) : (
<Button
className="text-[#71717A]"
disabled={isLoading}
onClick={(e) => {
e.preventDefault()
void handleResendOtp(email)
}}
variant="link"
>
Resend OTP
</Button>
)}
</span>
</div>
</form>
</div>
</div>
Expand Down

0 comments on commit a1cca73

Please sign in to comment.