11import React from "react" ;
2- import type { GetAllOpts } from "../Models/Commons" ;
32import type { InternetTiendasService } from "../Services/InternetTiendas.service" ;
43import type { InfoInternetTienda , InternetTiendas } from "../Models/Internet" ;
54import type { SociedadesService } from "../Services/Sociedades.service" ;
65
76
8- const escOData = ( s : string ) => `'${ String ( s ) . replace ( / ' / g, "''" ) } '` ;
97
108/** Carga un diccionario nombreEmpresa -> NIT, consultando por lotes */
119 async function getCompaniesMapByIds ( CompaniesSvc : SociedadesService , ids : Array < string | number > , concurrency = 8 ) : Promise < Record < string , string > > {
@@ -94,43 +92,45 @@ const escOData = (s: string) => `'${String(s).replace(/'/g, "''")}'`;
9492 return map ;
9593 }
9694
97-
9895export function useInfoInternetTiendas ( InfoInternetSvc : InternetTiendasService , CompaniesSvc : SociedadesService ) {
96+ const [ allRows , setAllRows ] = React . useState < InfoInternetTienda [ ] > ( [ ] ) ;
9997 const [ rows , setRows ] = React . useState < InfoInternetTienda [ ] > ( [ ] ) ;
10098 const [ loading , setLoading ] = React . useState ( false ) ;
10199 const [ error , setError ] = React . useState < string | null > ( null ) ;
102100 const [ query , setQuery ] = React . useState ( "" ) ;
103101
104- const buildFilter = React . useCallback ( ( ) : GetAllOpts => {
105- const q = query . trim ( ) . toLowerCase ( ) ;
106- if ( q . length < 2 ) return { top : 0 } ;
102+ const norm = React . useCallback ( ( s : unknown ) => { return String ( s ?? "" ) . trim ( ) . toLowerCase ( ) . normalize ( "NFD" ) . replace ( / [ \u0300 - \u036f ] / g, "" ) ; } , [ ] ) ;
103+
104+ const applyClientFilter = React . useCallback ( ( qRaw : string , base : InfoInternetTienda [ ] ) => {
105+ const q = norm ( qRaw ) ;
107106
108- const qEsc = escOData ( q ) ;
109- const filters = [
110- `startswith(fields/Tienda, ${ qEsc } )` ,
111- `startswith(fields/CORREO, ${ qEsc } )` ,
112- `startswith(fields/IDENTIFICADOR, ${ qEsc } )` ,
113- ] ;
107+ if ( q . length < 2 ) return base ;
114108
115- return {
116- filter : filters . join ( " or " ) ,
117- top : 150 ,
118- } ;
119- } , [ query ] ) ;
109+ return base . filter ( ( r ) => {
110+ return (
111+ norm ( r . Tienda ) . includes ( q ) ||
112+ norm ( r . Correo ) . includes ( q ) ||
113+ norm ( r . Identificador ) . includes ( q )
114+ ) ;
115+ } ) ;
116+ } ,
117+ [ norm ]
118+ ) ;
119+
120+ const buildFilter = React . useCallback ( ( ) => {
121+ return { top : 5000 } ;
122+ } , [ ] ) ;
120123
121124 const loadQuery = React . useCallback ( async ( ) => {
122125 setLoading ( true ) ;
123126 setError ( null ) ;
124127 try {
125-
126- const items : InternetTiendas [ ] = await InfoInternetSvc . getAll ( buildFilter ( ) ) ;
127-
128- const companyNames = items . map ( r => r . Compa_x00f1__x00ed_a ?? "" ) ;
128+ const items : InternetTiendas [ ] = await InfoInternetSvc . getAll ( buildFilter ( ) ) ;
129+ const companyNames = items . map ( ( r ) => r . Compa_x00f1__x00ed_a ?? "" ) ;
129130 const companiesMap = await getCompaniesMapByIds ( CompaniesSvc , companyNames ) ;
130- const companiesName = await getNamesCompaniesMapByIds ( CompaniesSvc , companyNames )
131+ const companiesName = await getNamesCompaniesMapByIds ( CompaniesSvc , companyNames ) ;
131132
132- // 3) Mapear a tu modelo normalizado
133- const view : InfoInternetTienda [ ] = items . map ( r => ( {
133+ const view : InfoInternetTienda [ ] = items . map ( ( r ) => ( {
134134 ID : r . ID ,
135135 Ciudad : r . Title ?? "N/A" ,
136136 CentroComercial : r . Centro_x0020_Comercial ?? "N/A" ,
@@ -142,22 +142,29 @@ export function useInfoInternetTiendas(InfoInternetSvc: InternetTiendasService,
142142 Direccion : r . DIRECCI_x00d3_N ?? "N/A" ,
143143 Local : r . Local ?? "N/A" ,
144144 Nota : r . Nota ?? "N/A" ,
145- ComparteCon : r . Nota ?? "N/A" , // si es otra columna, cámbiala aquí
145+ ComparteCon : r . Nota ?? "N/A" ,
146146 Nit : companiesMap [ ( r . Compa_x00f1__x00ed_a ?? "" ) . trim ( ) ] ?? "N/A" ,
147- Sociedad : companiesName [ ( r . Compa_x00f1__x00ed_a ?? "" ) . trim ( ) ] ?? "N/A" ,
147+ Sociedad : companiesName [ ( r . Compa_x00f1__x00ed_a ?? "" ) . trim ( ) ] ?? "N/A" ,
148148 } ) ) ;
149149
150- setRows ( view ) ;
150+ setAllRows ( view ) ;
151+ setRows ( applyClientFilter ( query , view ) ) ;
151152 } catch ( e : any ) {
152153 setError ( e ?. message ?? "Error cargando tiendas" ) ;
154+ setAllRows ( [ ] ) ;
153155 setRows ( [ ] ) ;
154156 } finally {
155157 setLoading ( false ) ;
156158 }
157- } , [ InfoInternetSvc , CompaniesSvc , buildFilter ] ) ;
159+ } , [ InfoInternetSvc , CompaniesSvc , buildFilter , applyClientFilter , query ] ) ;
160+
161+ // Re-filtra cuando cambie el query (sin llamar al servidor)
162+ React . useEffect ( ( ) => {
163+ setRows ( applyClientFilter ( query , allRows ) ) ;
164+ } , [ query , allRows , applyClientFilter ] ) ;
158165
159166 return {
160- // datos visibles (solo la página actual)
167+ // datos visibles
161168 rows,
162169 loading,
163170 error,
@@ -166,5 +173,8 @@ export function useInfoInternetTiendas(InfoInternetSvc: InternetTiendasService,
166173 // acciones
167174 setQuery,
168175 loadQuery,
176+
177+ // opcional: por si quieres acceder al dataset completo
178+ allRows,
169179 } ;
170180}
0 commit comments