"use client" import classes from '../page.module.css'; import React, { useContext, useEffect, useState } from "react"; import { TextInput, Button, Container, Title, Image, SimpleGrid, LoadingOverlay, Avatar, Paper, Text, Group, Center } from "@mantine/core"; import image from '../helpdesk.png'; import { useForm, SubmitHandler } from "react-hook-form"; import axios, { AxiosError } from "axios"; import { notifications } from "@mantine/notifications"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useDisclosure } from "@mantine/hooks"; type OtpInput = { OTP: number } type Result = { ok?: boolean message?: string } async function handleResendOTP() { let otp; try { const response = await axios.get("/api/otp"); otp = response.data; console.log('OTP resent successfully:', response.data); // Object.assign(response.data); // alert(response.data.message); notifications.show({ color: 'green', // title: error.code, message: response.data.message }) } catch (error: AxiosError | any) { notifications.show({ color: 'red', title: error.code, message: error.message }) } return otp; } async function handleValidateOTP(OtpInput: OtpInput) { let Result: Result = { ok: false } try { const response = await axios.post("/api/otp", OtpInput); { window.location.href = '/home' } Object.assign(Result, response.data); } catch (error: AxiosError | any) { // alert(error.response.data.error); notifications.show({ withBorder: true, color: 'red', title: error.response.status, message: error.response.data.error, autoClose: 4000, }) } // For countdown reset every time after successful verification sessionStorage.removeItem('countdown'); sessionStorage.removeItem('timerStart'); return Result; } export default function CheckOTP() { const queryClient = useQueryClient(); const { register, handleSubmit, formState: { errors } } = useForm(); const [countdown, setCountdown] = useState(90); const [timerActive, setTimerActive] = useState(true); const validateOTPMutation = useMutation({ mutationKey: ['validateOtp'], mutationFn: handleValidateOTP, }) const resendOTPMutation = useQuery({ queryKey: ['resendOTP'], queryFn: handleResendOTP, enabled: false, }) //// useEffect(() => { if (typeof window !== 'undefined') { const savedCountdown = sessionStorage.getItem('countdown'); const savedStartTime = sessionStorage.getItem('timerStart'); if (savedCountdown && savedStartTime) { const elapsedTime = Math.floor((Date.now() - parseInt(savedStartTime, 10)) / 1000); const remainingTime = parseInt(savedCountdown, 10) - elapsedTime; if (remainingTime > 0) { setCountdown(remainingTime); setTimerActive(true); } else { setCountdown(0); setTimerActive(false); sessionStorage.removeItem('countdown'); sessionStorage.removeItem('timerStart'); } } else { sessionStorage.setItem('countdown', '90'); sessionStorage.setItem('timerStart', Date.now().toString()); setCountdown(90); setTimerActive(true); } } }, []); // Effect to manage the countdown timer useEffect(() => { if (!timerActive) return; const interval = setInterval(() => { setCountdown((prev) => { if (prev <= 1) { clearInterval(interval); setTimerActive(false); sessionStorage.removeItem('countdown'); sessionStorage.removeItem('timerStart'); return 0; } return prev - 1; }); }, 1000); return () => clearInterval(interval); // Cleanup interval on unmount }, [timerActive]); ///// const onSubmit: SubmitHandler = async (OtpInput) => { const Result = await validateOTPMutation.mutateAsync(OtpInput); if (Result.ok) await queryClient.refetchQueries({ queryKey: ['OTP'] }); }; const handleResend = async () => { resendOTPMutation.refetch(); setCountdown(90); setTimerActive(true); sessionStorage.setItem('countdown', '90'); sessionStorage.setItem('timerStart', Date.now().toString()); } const [opened, { open, close }] = useDisclosure(false); return (
Customer Request Management {errors.OTP && *Required}
{timerActive ? `Resend OTP in ${countdown}s` : 'Resend OTP Available'}
{validateOTPMutation.data?.message && {validateOTPMutation.data.message}} {/* {resendOTPMutation?.data?.message && {resendOTPMutation.data.message}} */}
); }