From ad2d2dfbf4679c4f8f11cbed0868f3e5533c663b Mon Sep 17 00:00:00 2001 From: pradeepkumar Date: Sun, 1 Feb 2026 21:58:57 +0530 Subject: [PATCH] refactor: Simplify URL parameter handling by removing the `isInitialized` flag and directly using derived URL parameters for state and data fetching. --- src/app/(user)/user/profiles/page.tsx | 43 +++++++++++---------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/src/app/(user)/user/profiles/page.tsx b/src/app/(user)/user/profiles/page.tsx index dcf01a2..d223ec7 100644 --- a/src/app/(user)/user/profiles/page.tsx +++ b/src/app/(user)/user/profiles/page.tsx @@ -237,6 +237,11 @@ function ProfileCard({ profile }: ProfileCardProps) { function ProfilesPageContent() { const searchParams = useSearchParams(); + // Get URL params directly for use in effects + const typeFromUrl = searchParams.get('type'); + const nameFromUrl = searchParams.get('name') || ''; + const locationFromUrl = searchParams.get('location') || ''; + // State for API data const [agents, setAgents] = useState([]); const [agentTypes, setAgentTypes] = useState([]); @@ -248,28 +253,17 @@ function ProfilesPageContent() { const [totalResults, setTotalResults] = useState(0); // State for search and filters - initialize from URL params - const [searchQuery, setSearchQuery] = useState(() => searchParams.get('name') || ''); - const [activeTypeId, setActiveTypeId] = useState(() => searchParams.get('type')); - const [isInitialized, setIsInitialized] = useState(false); + const [searchQuery, setSearchQuery] = useState(nameFromUrl); + const [activeTypeId, setActiveTypeId] = useState(typeFromUrl); const [listingType, setListingType] = useState<'agents' | 'lenders'>('agents'); const [isFilterModalOpen, setIsFilterModalOpen] = useState(false); const [filters, setFilters] = useState({}); - // Mark as initialized after first render and sync with URL param changes + // Sync state when URL params change (e.g., browser back/forward, client-side navigation) useEffect(() => { - setIsInitialized(true); - }, []); - - // Sync state when URL params change (e.g., browser back/forward) - useEffect(() => { - if (isInitialized) { - const type = searchParams.get('type'); - const name = searchParams.get('name'); - - setActiveTypeId(type); - setSearchQuery(name || ''); - } - }, [searchParams, isInitialized]); + setActiveTypeId(typeFromUrl); + setSearchQuery(nameFromUrl); + }, [typeFromUrl, nameFromUrl]); // Fetch agent types and filter fields on mount useEffect(() => { @@ -320,10 +314,9 @@ function ProfilesPageContent() { } // Add location from URL params - const location = searchParams.get('location'); - if (location) { + if (locationFromUrl) { // Try to set as city first - params.city = location; + params.city = locationFromUrl; } // Add dynamic profile field filters @@ -348,14 +341,12 @@ function ProfilesPageContent() { } finally { setLoading(false); } - }, [currentPage, searchQuery, activeTypeId, searchParams, filters]); + }, [currentPage, searchQuery, activeTypeId, locationFromUrl, filters]); - // Fetch agents when filters change (only after initialization) + // Fetch agents when filters change useEffect(() => { - if (isInitialized) { - fetchAgents(); - } - }, [fetchAgents, isInitialized]); + fetchAgents(); + }, [fetchAgents]); const toggleFilter = (fieldSlug: string, optionValue: string) => { setFilters(prev => {