Con marca y dashboards
This commit is contained in:
50
lib/dashboard-utils.ts
Normal file
50
lib/dashboard-utils.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
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 HORAS_ORDER = ['Menos de 1h','Entre 1 y 3h','Entre 3 y 6h','Más de 6h','Varía mucho']
|
||||
export const AUSENCIAS_ORDER = ['Nunca','Pocas veces (1–3 veces al año)','Varias veces (4–10 veces al año)','Con frecuencia (más de 10 veces al año)']
|
||||
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']
|
||||
|
||||
export function sortByOrder<T extends Record<string, unknown>>(
|
||||
arr: T[],
|
||||
key: keyof T,
|
||||
order: string[]
|
||||
): T[] {
|
||||
return [...arr].sort((a, b) => {
|
||||
const ia = order.indexOf(a[key] as string)
|
||||
const ib = order.indexOf(b[key] as string)
|
||||
return (ia === -1 ? 999 : ia) - (ib === -1 ? 999 : ib)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user