37 lines
1.5 KiB
TypeScript
37 lines
1.5 KiB
TypeScript
import type { DashboardFilters } from './dashboard-types'
|
|
|
|
/** Convierte filtros a query string para las API routes */
|
|
export function filtersToQuery(surveyId: string, filters: DashboardFilters): string {
|
|
const p = new URLSearchParams({ survey_id: surveyId })
|
|
if (filters.depto) p.set('depto', filters.depto)
|
|
if (filters.genero?.length) p.set('genero', filters.genero.join(','))
|
|
if (filters.etario?.length) p.set('etario', filters.etario.join(','))
|
|
if (filters.modalidad?.length) p.set('modalidad', filters.modalidad.join(','))
|
|
return p.toString()
|
|
}
|
|
|
|
/** Parsea query string a objeto DashboardFilters */
|
|
export function queryToFilters(searchParams: URLSearchParams): DashboardFilters {
|
|
const f: DashboardFilters = {}
|
|
if (searchParams.has('depto')) f.depto = searchParams.get('depto')!
|
|
const g = searchParams.get('genero')
|
|
if (g) f.genero = g.split(',').filter(Boolean)
|
|
const e = searchParams.get('etario')
|
|
if (e) f.etario = e.split(',').filter(Boolean)
|
|
const m = searchParams.get('modalidad')
|
|
if (m) f.modalidad = m.split(',').filter(Boolean)
|
|
return f
|
|
}
|
|
|
|
/** Normaliza filtros a JSONB-compatible object para las RPC */
|
|
export function filtersToRpc(filters: DashboardFilters): Record<string, unknown> {
|
|
return {
|
|
depto: filters.depto ?? null,
|
|
genero: filters.genero ?? [],
|
|
etario: filters.etario ?? [],
|
|
modalidad: filters.modalidad ?? [],
|
|
}
|
|
}
|
|
|
|
export const ETARIO_ORDER = ['Menos de 25 años','25 a 34 años','35 a 44 años','45 a 54 años','55 años o más','Prefiero no decir']
|