summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorBlaster4385 <venkateshchaturvedi12@gmail.com>2022-08-17 17:03:22 +0530
committerBlaster4385 <venkateshchaturvedi12@gmail.com>2022-08-17 17:03:22 +0530
commit1af97096f8b7c71967279694c3f5271061d633ad (patch)
treeabeaf9a2bc1afcfd4e71f85b7b316da8d7fa2b44 /client
parente563ee2f67f5289c0ca50da6f55e874ae8a0136f (diff)
Fix this shit
Diffstat (limited to 'client')
-rw-r--r--client/src/pages/Update/Demographic/Demographic.jsx55
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)