/* eslint-disable multiline-ternary */ import React, { useState } from 'react' import Webcam from 'react-webcam' import Header from '../../../components/Header/Header' import styles from './DocumentScanner.module.css' import { Button, Typography, StepLabel, Step, Stepper } from '@mui/material' import PopUpModal from '../../../components/Modal/Modal' import { useTranslation } from 'react-i18next' import { userContext } from '../../../context/User' import { toast } from 'react-toastify' import { useStyles } from './styles' import AudioAutoplay from '../../../components/AudioAutoplay/AudioAutoplay' const DocumentScanner = () => { const { userData, oriUserData, setUserData } = userContext() const { t } = useTranslation() let steps if ( (userData.address !== oriUserData.address && userData.dob !== oriUserData.dob && userData.name !== oriUserData.name) || userData.gender !== oriUserData.gender ) { steps = ['POA', 'POI', 'DOB'] } else { if (userData.address !== oriUserData.address) { if ( userData.name !== oriUserData.name || userData.gender !== oriUserData.gender ) { steps = ['POA', 'POI'] } else if (userData.dob !== oriUserData.dob) { steps = ['POA', 'DOB'] } else { steps = ['POA'] } } else if ( userData.name !== oriUserData.name || userData.gender !== oriUserData.gender ) { if (userData.dob !== oriUserData.dob) { steps = ['POI', 'DOB'] } else { steps = ['POI'] } } else if (userData.dob !== oriUserData.dob) { steps = ['DOB'] } else { steps = [] } } const [documents, setDocuments] = useState({ POI: '', POA: '', DOB: '' }) const [activeStep, setActiveStep] = React.useState(0) const [doccu] = useState({ POI: '', POA: '', DOB: '' }) const classes = useStyles() const webcamRef = React.useRef(null) const capture = React.useCallback((doc) => { const imageSrc = webcamRef.current.getScreenshot() doccu[doc] = imageSrc setDocuments({ ...documents, POI: doccu.POI, POA: doccu.POA, DOB: doccu.DOB }) }) const handleNext = () => { if ( activeStep === steps.length - 1 && documents.POI !== '' && documents.POA !== '' && documents.DOB !== '' ) { setUserData({ ...userData, documents: documents }) } else if (documents.POI !== '' && documents.POA !== '') { toast.error(t('SCAN_YOUR_DOCUMENT')) } else if (documents.POI !== '' && documents.DOB !== '') { toast.error(t('SCAN_YOUR_DOCUMENT')) } else if (documents.POA !== '' && documents.DOB !== '') { toast.error(t('SCAN_YOUR_DOCUMENT')) } else if (documents.POI !== '') { toast.error(t('SCAN_YOUR_DOCUMENT')) } else if (documents.POA !== '') { toast.error(t('SCAN_YOUR_DOCUMENT')) } else if (documents.DOB !== '') { toast.error(t('SCAN_YOUR_DOCUMENT')) } else { toast.error(t('SCAN_YOUR_DOCUMENT')) } if ( (!documents.POI && activeStep === 0) || (!documents.POA && activeStep === 1) || (!documents.DOB && activeStep === 2) ) { toast.error(t('SCAN_YOUR_DOCUMENT')) } else { setActiveStep((prevActiveStep) => prevActiveStep + 1) } } const handleBack = () => { setActiveStep((prevActiveStep) => prevActiveStep - 1) } const WebcamComponent = ({ doc }) => { return ( <>