11import { useState , useEffect , useCallback } from 'react' ;
2- import { companiesApi , Company , CompanyListItem , CompanySearchRequest , Filing , PaginatedResponse } from '../api' ;
2+ import { companiesApi , Company , CompanyListItem , CompanySearchRequest , Filing } from '../api' ;
33
44export function useCompanies ( initialParams : CompanySearchRequest = { } ) {
5- const [ companies , setCompanies ] = useState < PaginatedResponse < CompanyListItem > | null > ( null ) ;
5+ const [ companies , setCompanies ] = useState < CompanyListItem [ ] > ( [ ] ) ;
66 const [ loading , setLoading ] = useState ( true ) ;
77 const [ error , setError ] = useState < string | null > ( null ) ;
8- const [ params , setParams ] = useState < CompanySearchRequest > ( initialParams ) ;
8+ const [ totalElements , setTotalElements ] = useState ( 0 ) ;
9+ const [ totalPages , setTotalPages ] = useState ( 0 ) ;
10+ const [ params , setParams ] = useState < CompanySearchRequest > ( {
11+ page : 0 ,
12+ size : 20 ,
13+ sortBy : 'name' ,
14+ sortDir : 'asc' ,
15+ ...initialParams ,
16+ } ) ;
917
1018 const fetchCompanies = useCallback ( async ( searchParams : CompanySearchRequest ) => {
1119 setLoading ( true ) ;
1220 setError ( null ) ;
1321 try {
1422 const data = await companiesApi . getCompanies ( searchParams ) ;
15- setCompanies ( data ) ;
23+ setCompanies ( data ?. content ?? [ ] ) ;
24+ setTotalElements ( data ?. totalElements ?? 0 ) ;
25+ setTotalPages ( data ?. totalPages ?? 0 ) ;
1626 } catch ( err ) {
1727 setError ( err instanceof Error ? err . message : 'Failed to load companies' ) ;
28+ setCompanies ( [ ] ) ;
29+ setTotalElements ( 0 ) ;
30+ setTotalPages ( 0 ) ;
1831 } finally {
1932 setLoading ( false ) ;
2033 }
@@ -24,8 +37,13 @@ export function useCompanies(initialParams: CompanySearchRequest = {}) {
2437 fetchCompanies ( params ) ;
2538 } , [ params , fetchCompanies ] ) ;
2639
27- const search = useCallback ( ( searchTerm : string ) => {
28- setParams ( prev => ( { ...prev , searchTerm, page : 0 } ) ) ;
40+ const search = useCallback ( ( searchInput : CompanySearchRequest & { name ?: string } ) => {
41+ setParams ( prev => ( {
42+ ...prev ,
43+ ...searchInput ,
44+ searchTerm : searchInput . searchTerm ?? searchInput . name ?? prev . searchTerm ,
45+ page : searchInput . page ?? 0 ,
46+ } ) ) ;
2947 } , [ ] ) ;
3048
3149 const setPage = useCallback ( ( page : number ) => {
@@ -44,6 +62,8 @@ export function useCompanies(initialParams: CompanySearchRequest = {}) {
4462 companies,
4563 loading,
4664 error,
65+ totalElements,
66+ totalPages,
4767 params,
4868 search,
4969 setPage,
@@ -95,23 +115,29 @@ export function useCompanyByCik(cik: string | undefined) {
95115 return { company, loading, error } ;
96116}
97117
98- export function useCompanyFilings ( companyId : string | undefined , page : number = 0 , size : number = 10 ) {
99- const [ filings , setFilings ] = useState < PaginatedResponse < Filing > | null > ( null ) ;
100- const [ loading , setLoading ] = useState ( true ) ;
118+ export function useCompanyFilings ( ) {
119+ const [ filings , setFilings ] = useState < Filing [ ] > ( [ ] ) ;
120+ const [ loading , setLoading ] = useState ( false ) ;
101121 const [ error , setError ] = useState < string | null > ( null ) ;
102122
103- useEffect ( ( ) => {
104- if ( ! companyId ) {
105- setLoading ( false ) ;
123+ const fetchByCompany = useCallback ( async ( cik : string , page : number = 0 , size : number = 10 ) => {
124+ if ( ! cik ) {
125+ setFilings ( [ ] ) ;
106126 return ;
107127 }
108128
109129 setLoading ( true ) ;
110- companiesApi . getCompanyFilings ( companyId , page , size )
111- . then ( setFilings )
112- . catch ( err => setError ( err . message ) )
113- . finally ( ( ) => setLoading ( false ) ) ;
114- } , [ companyId , page , size ] ) ;
130+ setError ( null ) ;
131+ try {
132+ const data = await companiesApi . getCompanyFilingsByCik ( cik , page , size ) ;
133+ setFilings ( data ?. content ?? [ ] ) ;
134+ } catch ( err ) {
135+ setError ( err instanceof Error ? err . message : 'Failed to load company filings' ) ;
136+ setFilings ( [ ] ) ;
137+ } finally {
138+ setLoading ( false ) ;
139+ }
140+ } , [ ] ) ;
115141
116- return { filings, loading, error } ;
142+ return { filings, loading, error, fetchByCompany } ;
117143}
0 commit comments