diff --git a/src/components/Patient/PatientDetailsTab/Demography.tsx b/src/components/Patient/PatientDetailsTab/Demography.tsx index 95811be7361..a1f1e9833d9 100644 --- a/src/components/Patient/PatientDetailsTab/Demography.tsx +++ b/src/components/Patient/PatientDetailsTab/Demography.tsx @@ -31,34 +31,28 @@ export const Demography = (props: PatientProps) => { useState(); const [activeSection, setActiveSection] = useState(null); + const [clickedSection, setClickedSection] = useState(null); useEffect(() => { setAssignedVolunteerObject(patientData.assigned_to_object); - - const observedSections: Element[] = []; const sections = document.querySelectorAll("div[id]"); const observer = new IntersectionObserver( (entries) => { - entries.forEach((entry) => { - if (entry.isIntersecting) { - setActiveSection(entry.target.id); - } - }); - }, - { - threshold: 0.6, + if (clickedSection) return; + const visibleSection = entries.find((entry) => entry.isIntersecting); + if (visibleSection) { + setActiveSection(visibleSection.target.id); + } }, + { threshold: 0.6 }, ); - sections.forEach((section) => { - observer.observe(section); - observedSections.push(section); - }); + sections.forEach((section) => observer.observe(section)); return () => { - observedSections.forEach((section) => observer.unobserve(section)); + sections.forEach((section) => observer.unobserve(section)); }; - }, [patientData.assigned_to_object]); + }, [clickedSection]); const { data: insuranceDetials } = useTanStackQueryInstead( routes.hcx.policies.list, @@ -72,6 +66,8 @@ export const Demography = (props: PatientProps) => { )?.text; const scrollToSection = (sectionId: string) => { + setClickedSection(sectionId); + setActiveSection(sectionId); const section = document.getElementById(sectionId); if (section) { section.scrollIntoView({ behavior: "smooth" });