diff options
author | Blaster4385 <venkateshchaturvedi12@gmail.com> | 2022-08-17 17:03:22 +0530 |
---|---|---|
committer | Blaster4385 <venkateshchaturvedi12@gmail.com> | 2022-08-17 17:03:22 +0530 |
commit | 1af97096f8b7c71967279694c3f5271061d633ad (patch) | |
tree | abeaf9a2bc1afcfd4e71f85b7b316da8d7fa2b44 | |
parent | e563ee2f67f5289c0ca50da6f55e874ae8a0136f (diff) |
Fix this shit
-rw-r--r-- | client/src/pages/Update/Demographic/Demographic.jsx | 55 |
1 files changed, 31 insertions, 24 deletions
diff --git a/client/src/pages/Update/Demographic/Demographic.jsx b/client/src/pages/Update/Demographic/Demographic.jsx index 8853678..6ce06a1 100644 --- a/client/src/pages/Update/Demographic/Demographic.jsx +++ b/client/src/pages/Update/Demographic/Demographic.jsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react' +import React, { useEffect, useState } from 'react' import { useQuery } from 'react-query' import { getUserByAadhaar } from '../../../services/apiservice' import Address from '../Address/Address' @@ -25,29 +25,6 @@ const Demographic = () => { const isLongEnough = aadhaarNumber?.toString().length > 11 - const { data } = useQuery( - ['user', aadhaarNumber], - () => getUserByAadhaar(aadhaarNumber), - { - enabled: isLongEnough, - retry: 1, - onSuccess: () => { - while (!data?.data?.name) { - console.log(formData.name) - console.log('success') - setUserData(data?.data) - console.log(data?.data) - console.log(userData?.name) - console.log(data?.data?.name) - setFormData({ - ...formData, - name: userData?.name - }) - } - } - } - ) - const [formData, setFormData] = useState({ name: '', gender: '', @@ -67,6 +44,36 @@ const Demographic = () => { // 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 <div>{t('loading')}</div> + } + + if (isError) { + return <div>{t('error')}</div> + } + + if (data) { + setUserData(data?.data) + } + const address = userData?.address console.log(address) |