Skip to content

Commit 14d219e

Browse files
committed
Implement Agency tab with a dedicated agencies API endpoint
- Add /agencies endpoint to apiRoutes - Add searchAgencies function to SearchProvider - Update Agency tab to call agencies API when selected - Encountered CORS error
1 parent b9551b0 commit 14d219e

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

frontend/app/search/SearchResults.tsx

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use client"
22
import { Tab, Tabs, Box, CardHeader, Typography } from "@mui/material"
33
import React, { useState } from "react"
4-
import { SearchResponse } from "@/utils/api"
4+
import { useSearchParams } from "next/navigation"
5+
import { SearchResponse, AgencyResponse } from "@/utils/api"
56
import { useSearch } from "@/providers/SearchProvider"
67

78
type SearchResultsProps = {
@@ -11,10 +12,40 @@ type SearchResultsProps = {
1112

1213
const SearchResults = ({ total, results }: SearchResultsProps) => {
1314
const [tab, setTab] = useState(0)
14-
const { loading } = useSearch()
15+
const { loading, searchAgencies } = useSearch()
16+
17+
const searchParams = useSearchParams()
18+
const currentQuery = searchParams.get('query') || ''
19+
20+
const [agencyResults, setAgencyResults] = useState<AgencyResponse[]>([])
21+
const [agencyLoading, setAgencyLoading] = useState(false)
22+
const [agencyTotal, setAgencyTotal] = useState(0)
1523

1624
const handleChange = (event: React.SyntheticEvent, newValue: number) => {
1725
setTab(newValue)
26+
27+
// When Agency tab is clicked
28+
if (newValue === 3) {
29+
handleAgencySearch()
30+
}
31+
}
32+
33+
const handleAgencySearch = async () => {
34+
if (!currentQuery) return
35+
36+
setAgencyLoading(true)
37+
try {
38+
// search by name for now
39+
const reponse = 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+
}
1849
}
1950

2051
return (
@@ -83,9 +114,15 @@ const SearchResults = ({ total, results }: SearchResultsProps) => {
83114
))}
84115
</CustomTabPanel>
85116
<CustomTabPanel value={tab} index={3}>
86-
{results
87-
.filter((result) => result.content_type === "Agency")
88-
.map((result) => (
117+
{agencyLoading ? (
118+
<Typography>Searching agencies...</Typography>
119+
) : (
120+
<>
121+
<Typography sx={{ marginBottom: "1rem", fontWeight: "bold" }}>
122+
{agencyTotal} agency results
123+
</Typography>
124+
{agencyResults.map((result) => (
125+
89126
<CardHeader
90127
key={result.uid}
91128
title={result.title}
@@ -121,7 +158,8 @@ const SearchResults = ({ total, results }: SearchResultsProps) => {
121158
}}
122159
/>
123160
))}
124-
161+
</>
162+
)}
125163
<p>Agency tab - {results.length} total results</p>
126164
</CustomTabPanel>
127165
</Box>

frontend/providers/SearchProvider.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,15 @@ function useHook(): SearchContext {
112112

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

115-
const response = await apiFetch(apiBaseUrl, {
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+
123+
const response = await apiFetch(apiUrl, {
116124
method: "GET",
117125
headers: {
118126
"Content-Type": "application/json"
@@ -142,5 +150,5 @@ function useHook(): SearchContext {
142150
[accessToken]
143151
)
144152

145-
return useMemo(() => ({ searchAll, searchResults, loading }), [searchResults, searchAll, searchResults, loading])
153+
return useMemo(() => ({ searchAll, searchAgencies, searchResults, loading }), [searchResults, searchAll, searchAgencies, searchResults, loading])
146154
}

frontend/utils/apiRoutes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const API_ROUTES = {
99
all: "/search/",
1010
incidents: "/incidents/search"
1111
},
12-
agencies: "/api/v1/agencies"
12+
agencies: "/agencies"
1313
}
1414

1515
export const apiBaseUrl: string =

0 commit comments

Comments
 (0)