From ffd2c78b86111a5d1f7914e7dab4ffc946cb5287 Mon Sep 17 00:00:00 2001 From: rohan09-raj Date: Tue, 23 Aug 2022 14:39:22 +0530 Subject: check status feature --- client/src/pages/CheckStatus/StatusOtp.jsx | 192 +++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 client/src/pages/CheckStatus/StatusOtp.jsx (limited to 'client/src/pages/CheckStatus/StatusOtp.jsx') diff --git a/client/src/pages/CheckStatus/StatusOtp.jsx b/client/src/pages/CheckStatus/StatusOtp.jsx new file mode 100644 index 0000000..d7d62d1 --- /dev/null +++ b/client/src/pages/CheckStatus/StatusOtp.jsx @@ -0,0 +1,192 @@ +import React, { useState, useEffect } from 'react' +import Header from '../../components/Header/Header' +import Input from '../../components/Input/Input' +import { Button } from '@mui/material' +import { useNavigate } from 'react-router-dom' +import { useTranslation } from 'react-i18next' +import { userContext } from '../../context/User' +import { useQuery, useMutation } from 'react-query' +import { getUserByAadhaar, sendOTP, getUser } from '../../services/apiservice' +import SubmitButton from '../../components/SubmitButton/SubmitButton' +import { toast, ToastContainer } from 'react-toastify' +import PopUpModal from '../../components/Modal/Modal' + +import styles from './StatusOtp.module.css' + +const StatusOtp = () => { + const navigate = useNavigate() + const { t } = useTranslation() + const [otp, setOtp] = useState() + const [disabled, setDisabled] = useState(false) + const [finalDisable, setFinalDisable] = useState(false) + const [unverified, setUnverified] = useState(true) + const [show, setShow] = useState(false) + const { + aadhaarNumber, + eidNumber, + userData, + setUserData, + oriUserData, + setOriUserData + } = userContext() + + const mutateOTP = useMutation(() => + sendOTP({ mobile: `+91${userData?.mobile}` }) + ) + + const verifyOTP = () => { + if (mutateOTP?.data?.data?.otpCode === Number(otp)) { + setFinalDisable(true) + setDisabled(true) + setShow(false) + setUnverified(false) + toast.success(t('OTP_VERIFIED!')) + } else { + toast.error(t('INCORRECT_OTP')) + } + } + + const sendResendOTP = () => { + setTimeout(() => { + if (finalDisable === false) { + console.log('Disabled: ', disabled, 'Final Disable: ', finalDisable) + setDisabled(false) + } + }, 30000) + } + + useEffect(() => { + setUserData(oriUserData) + }, [oriUserData]) + + if (aadhaarNumber) { + const isLongEnough = aadhaarNumber?.toString().length > 11 + const { isLoading, isError, data } = useQuery('user', async () => { + if (isLongEnough) { + const response = await getUserByAadhaar(aadhaarNumber) + return response + } + }) + if (isLoading) { + return
{t('loading')}
+ } + + if (isError) { + return
{t('error')}
+ } + + if (data) { + setOriUserData(data?.data) + } + } else if (eidNumber) { + const { isLoading, isError, data } = useQuery('user', async () => { + const response = await getUser(eidNumber) + return response + }) + + if (isLoading) { + return
{t('loading')}
+ } + + if (isError) { + return
{t('error')}
+ } + + if (data) { + setOriUserData(data?.data) + } + } + + return ( + <> + +
+ +
    +
  • Click on "SEND OTP"
  • +
  • + You will recieve an OTP on your entered mobile number +
  • +
  • + You can "RESEND" the OTP after 30 seconds, if you + haven't received it yet. +
  • +
  • + Click on "VERIFY OTP" to verify your mobile number +
  • +
+ + } + /> +
+

{t('ENTER_OTP')}

+

+ {t('SENT_TO_YOUR_REGISTERED_MOBILE_NUMBER')} +

+ + {show && ( + <> + setOtp(e.target.value)} + maxLength="6" + placeholder={t('ENTER_OTP')} + /> + + + )} +
+ { + if (unverified) { + toast.error(t('PLEASE_VERIFY_OTP')) + } else { + if (aadhaarNumber) { + navigate('/status/update') + } else if (eidNumber) { + navigate('/status/enrollment') + } + } + }} + /> + + ) +} + +export default StatusOtp -- cgit