import React, { useEffect, useState } from 'react'
import { useQuery } from 'react-query'
import { getUserByAadhaar } from '../../../services/apiservice'
import Address from '../Address/Address'
import DocumentScanner from '../DocumentScanner/DocumentScanner'
import SubmitButton from '../../../components/SubmitButton/SubmitButton'
import FormOne from '../FormOne/FormOne'
import {
validEmail,
validMobileNumber,
validPincode,
validString
} from '../../../constants/RegEx'
import UpdateSelect from '../UpdateSelect/UpdateSelect'
import { useTranslation } from 'react-i18next'
import { userContext } from '../../../context/User'
import { ToastContainer, toast } from 'react-toastify'
import 'react-toastify/dist/ReactToastify.css'
const Demographic = () => {
const { t } = useTranslation()
const { aadhaarNumber, userData, setUserData } = userContext()
const [page, setPage] = useState(0)
const isLongEnough = aadhaarNumber?.toString().length > 11
const [formData, setFormData] = useState({
name: '',
gender: '',
dob: '',
mobile: '',
email: '',
country: '',
state: '',
district: '',
village: '',
houseNo: '',
street: '',
locality: '',
postOffice: '',
landmark: '',
pincode: ''
// address: userData?.address
})
useEffect(() => {
setFormData({
...formData,
...userData
})
}, [userData])
// Make api call using the provided aadhaar number and set the user data in the context if the api call is successful. Set form data to the user data if the api call is successful and prevent too many re-renders.
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) {
setUserData(data?.data)
}
const address = userData?.address
console.log(address)
console.log(
'Aadhaar: ',
aadhaarNumber,
'Islong: ',
isLongEnough,
'User: ',
userData
)
const handleSubmit = () => {
if (page === 0) {
if (formData.name === '' || formData.name.length < 1) {
toast.error(t('PLEASE_ENTER_YOUR_NAME'))
} else if (!validString.test(formData.name)) {
toast.error(t('PLEASE_ENTER_VALID_NAME'))
} else if (formData.gender === '') {
toast.error(t('PLEASE_SELECT_YOUR_GENDER'))
} else if (formData.mobile === '') {
toast.error(t('PLEASE_ENTER_YOUR_MOBILE_NUMBER'))
} else if (!validMobileNumber.test(formData.mobile)) {
toast.error(t('PLEASE_ENTER_VALID_MOBILE_NUMBER'))
} else if (formData.email === '') {
toast.error(t('PLEASE_ENTER_YOUR_EMAIL'))
} else if (!validEmail.test(formData.email)) {
toast.error(t('PLEASE_ENTER_VALID_EMAIL'))
} else {
setPage(page + 1)
}
} else if (page === 1) {
if (formData.country === '') {
toast.error(t('PLEASE_SELECT_YOUR_COUNTRY'))
} else if (formData.state === '') {
toast.error(t('PLEASE_SELECT_YOUR_STATE'))
} else if (formData.district === '') {
toast.error(t('PLEASE_SELECT_YOUR_DISTRICT'))
} else if (formData.village === '') {
toast.error(t('PLEASE_ENTER_YOUR_VILLAGE'))
} else if (formData.houseNo === '') {
toast.error(t('PLEASE_ENTER_YOUR_HOUSE_NUMBER'))
} else if (formData.street === '') {
toast.error(t('PLEASE_ENTER_YOUR_STREET'))
} else if (formData.locality === '') {
toast.error(t('PLEASE_ENTER_YOUR_LOCALITY'))
} else if (formData.postOffice === '') {
toast.error(t('PLEASE_ENTER_YOUR_AREA_POST_OFFICE'))
} else if (formData.landmark === '') {
toast.error(t('PLEASE_ENTER_NEAREST_LANDMARK'))
} else if (formData.pincode === '') {
toast.error(t('PLEASE_ENTER_YOUR_AREA_PINCODE'))
} else if (!validPincode.test(formData.pincode)) {
toast.error(t('PLEASE_ENTER_VALID_PINCODE'))
} else {
setFormData({
...formData,
address: `${formData.houseNo} ${formData.street}, ${formData.locality}, ${formData.landmark}, ${formData.village}, ${formData.district.label}, ${formData.country.label} ${formData.pincode}`
})
setPage(page + 1)
}
} else if (page === 3) {
setPage(page + 1)
} else setPage(page + 1)
}
const conditionalComponent = () => {
switch (page) {
case 0:
return
case 1:
return
case 2:
return
default:
return
}
}
const conditionalButton = () => {
switch (page) {
case 0:
return Next
case 1:
return Next
case 2:
return Next
default:
return Next
}
}
return (
<>
{conditionalComponent()}
{conditionalButton()}
>
)
}
export default Demographic