@@ -4,6 +4,7 @@ import React, { useState } from "react"
44import { useSearchParams } from "next/navigation"
55import { SearchResponse , AgencyResponse } from "@/utils/api"
66import { useSearch } from "@/providers/SearchProvider"
7+ import { useEffect } from "react"
78
89type SearchResultsProps = {
910 total : number
@@ -21,31 +22,28 @@ const SearchResults = ({ total, results }: SearchResultsProps) => {
2122 const [ agencyLoading , setAgencyLoading ] = useState ( false )
2223 const [ agencyTotal , setAgencyTotal ] = useState ( 0 )
2324
24- const handleChange = ( event : React . SyntheticEvent , newValue : number ) => {
25- setTab ( newValue )
25+ useEffect ( ( ) => {
26+ const performAgencySearch = async ( ) => {
27+ if ( tab !== 3 || ! currentQuery ) return
2628
27- // When Agency tab is clicked
28- if ( newValue === 3 ) {
29- handleAgencySearch ( )
29+ setAgencyLoading ( true )
30+ try {
31+ const response = await searchAgencies ( { name : currentQuery } )
32+ setAgencyResults ( response . results || [ ] )
33+ setAgencyTotal ( response . total || 0 )
34+ } catch ( error ) {
35+ console . error ( 'Agency search failed:' , error )
36+ setAgencyResults ( [ ] )
37+ setAgencyTotal ( 0 )
38+ } finally {
39+ setAgencyLoading ( false )
40+ }
3041 }
31- }
32-
33- const handleAgencySearch = async ( ) => {
34- if ( ! currentQuery ) return
42+ performAgencySearch ( )
43+ } , [ currentQuery , tab , searchAgencies ] )
3544
36- setAgencyLoading ( true )
37- try {
38- // search by name for now
39- const response = await searchAgencies ( { name : currentQuery } )
40- setAgencyResults ( response . results || [ ] )
41- setAgencyTotal ( response . total || 0 )
42- } catch ( error ) {
43- console . error ( 'Agency search failed:' , error )
44- setAgencyResults ( [ ] )
45- setAgencyTotal ( 0 )
46- } finally {
47- setAgencyLoading ( false )
48- }
45+ const handleChange = ( event : React . SyntheticEvent , newValue : number ) => {
46+ setTab ( newValue )
4947 }
5048
5149 return (
@@ -160,7 +158,6 @@ const SearchResults = ({ total, results }: SearchResultsProps) => {
160158 ) ) }
161159 </ >
162160 ) }
163- < p > Agency tab - { results . length } total results</ p >
164161 </ CustomTabPanel >
165162 </ Box >
166163 ) }
0 commit comments