Skip to content

Commit c7e4f5f

Browse files
committed
Add useEffect for reactive agency search state and remove debug logs
1 parent b282e37 commit c7e4f5f

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

frontend/app/search/SearchResults.tsx

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, { useState } from "react"
44
import { useSearchParams } from "next/navigation"
55
import { SearchResponse, AgencyResponse } from "@/utils/api"
66
import { useSearch } from "@/providers/SearchProvider"
7+
import { useEffect } from "react"
78

89
type 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
)}

frontend/providers/SearchProvider.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,6 @@ function useHook(): SearchContext {
112112

113113
const apiUrl = `${apiBaseUrl}${API_ROUTES.agencies}?${queryParams.toString()}`
114114

115-
console.log('====== AGENCY SEARCH DEBUG ======')
116-
console.log('apiBaseUrl:', apiBaseUrl)
117-
console.log('API_ROUTES.agencies:', API_ROUTES.agencies)
118-
console.log('queryParams:', queryParams.toString())
119-
console.log('Full apiUrl:', apiUrl)
120-
console.log('Has accessToken:', !!accessToken)
121-
console.log('================================')
122-
123115
const response = await apiFetch(apiUrl, {
124116
method: "GET",
125117
headers: {

0 commit comments

Comments
 (0)