From 0ffc57cd21e89d16e401da6afa143e3c7b3c4419 Mon Sep 17 00:00:00 2001 From: idil-bit Date: Fri, 20 Dec 2024 13:25:35 -0500 Subject: [PATCH] phone number check --- .../[[...customer-questions]]/page.tsx | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/app/customer-questions/[[...customer-questions]]/page.tsx b/app/customer-questions/[[...customer-questions]]/page.tsx index e192714..42be94b 100644 --- a/app/customer-questions/[[...customer-questions]]/page.tsx +++ b/app/customer-questions/[[...customer-questions]]/page.tsx @@ -88,7 +88,7 @@ const PhoneNumber: React.FC<{ value: string, onChange: (value: string) => void, }; // Information Changed Module -const Changes: React.FC<{ value: string, onChange: (newValue: string) => void, setNextDisabled: (disabled: boolean) => void }> = ({ value, onChange, setNextDisabled }) => { +const Changes: React.FC<{ value: string, onChange: (newValue: string) => void, setNextDisabled: (disabled: boolean) => void, details: any }> = ({ value, onChange, setNextDisabled, details }) => { const [selectedValue, setSelectedValue] = useState(value); const handleYesNoChange = (newValue: string) => { @@ -104,7 +104,7 @@ const Changes: React.FC<{ value: string, onChange: (newValue: string) => void, s

Has your information changed? *

-

(Name, Address, Household size)

+

(Name: {details.name}, Address: {details.address}, Household size: {details.householdSize})

@@ -363,7 +363,32 @@ const DemographicsSurvey: React.FC = () => { setResponses((prev) => ({ ...prev, householdSize: value ?? 0 })); }; - const handleNextClick = () => { + const [prevRecord, setPrevRecord] = useState(null); + const fetchPrevRecord = async () => { + try { + const response = await fetch("../api/demographics", { method: "GET" }); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const surveyResponses = await response.json(); + console.log("Fetched responses:", surveyResponses); + + const filteredRecord = surveyResponses.find( + (response: any) => response.phoneNumber === responses.phoneNumber + ); + + console.log("Filtered Record:", filteredRecord); + setPrevRecord(filteredRecord || null); + return filteredRecord || null; + } catch (error) { + console.error("Error fetching responses:", error); + setPrevRecord(null); + return null; + } + }; + + + const handleNextClick = async () => { if (nextDisabled) return; switch (currentStep) { @@ -375,7 +400,12 @@ const DemographicsSurvey: React.FC = () => { } break; case 'phoneNum': - setCurrentStep('changes'); + const record = await fetchPrevRecord(); + if (record !== null) { + setCurrentStep('changes'); + } else { + setCurrentStep('name'); + } break; case 'changes': if (responses.changes == "yes") { @@ -426,7 +456,11 @@ const DemographicsSurvey: React.FC = () => { setCurrentStep('name'); break; case 'name': - setCurrentStep('changes'); + if (prevRecord !== null) { + setCurrentStep('changes'); + } else { + setCurrentStep('phoneNum'); + } break; case 'changes': setCurrentStep('phoneNum'); @@ -489,7 +523,7 @@ const DemographicsSurvey: React.FC = () => { {currentStep === 'action' && } {currentStep === 'donor' && } {currentStep === 'phoneNum' && } - {currentStep === 'changes' && } + {currentStep === 'changes' && } {currentStep === 'name' && updateName('firstName', value)} onLastNameChange={(value) => updateName('lastName', value)} setNextDisabled={setNextDisabled} />}