11"use client"
22import { Tab , Tabs , Box , CardHeader , Typography } from "@mui/material"
33import React , { useState } from "react"
4- import { SearchResponse } from "@/utils/api"
4+ import { useSearchParams } from "next/navigation"
5+ import { SearchResponse , AgencyResponse } from "@/utils/api"
56import { useSearch } from "@/providers/SearchProvider"
67
78type SearchResultsProps = {
@@ -11,10 +12,40 @@ type SearchResultsProps = {
1112
1213const 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 >
0 commit comments