Skip to content

Commit

Permalink
💥 Buscar elementos por ID y no por índice #34
Browse files Browse the repository at this point in the history
  • Loading branch information
1cgonza committed Nov 3, 2024
1 parent c83cdde commit 6db492b
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 87 deletions.
19 changes: 11 additions & 8 deletions aplicaciones/procesador/fuente/procesadorColectivos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function procesarLista(llaveLista: LlavesColectivos, valor: string) {
const existe = listas[llaveLista].find((obj) => obj.slug === slug);
if (!existe) {
listas[llaveLista].push({
id: `${listas[llaveLista].length}`,
nombre,
slug,
conteo: 1,
Expand Down Expand Up @@ -68,7 +69,7 @@ export default async (
const nombre = limpiarTextoSimple(fila[0]);

if (nombre) {
const colectivo: Colectivo = { id: numeroFila - 1, titulo: { nombre, slug: slugificar(nombre) } };
const colectivo: Colectivo = { id: `${numeroFila - 1}`, titulo: { nombre, slug: slugificar(nombre) } };

/** Tipos de Ámbito */
if (fila[1]) {
Expand Down Expand Up @@ -194,6 +195,8 @@ export default async (
}
colectivo.dependencias.push({ nombre, slug });
});
} else {
errata.push({ fila: numeroFila, error: 'No tiene DEPENDENCIA' });
}

/** Modalidad */
Expand Down Expand Up @@ -285,9 +288,9 @@ export default async (
const slugsCampoProyecto = aplanarDefinicionesASlugs(datosRelacion);

slugsCampoProyecto.forEach((slug) => {
const indice = listas[campoRelacion].findIndex((obj) => obj.slug === slug);
const existe = listas[campoRelacion].find((obj) => obj.slug === slug);

if (indice >= 0) {
if (existe) {
if (!colectivo[campo]) return;

let datos = colectivo[campo];
Expand All @@ -311,7 +314,7 @@ export default async (
// if (campoRelacion === 'indicadores') {
// console.log(elementosDondeConectar, listas[campoRelacion], indice, campoRelacion, id);
// }
llenarRelacion(elementosDondeConectar, listas[campo], indice, campoRelacion, id);
llenarRelacion(elementosDondeConectar, listas[campo], existe.id, campoRelacion, id);
} else {
console.log('Esto no puede pasar');
}
Expand All @@ -333,18 +336,18 @@ export default async (
function llenarRelacion(
elementosDondeConectar: string[],
elementoLista: ElementoLista[],
indice: number,
indice: string,
campoRelacion: LlavesColectivos,
id: number
id: string
) {
elementosDondeConectar.forEach((elementoConector) => {
const elementoALlenar = elementoLista.find((obj) => obj.slug === elementoConector);

if (elementoALlenar) {
const existe = elementoALlenar.relaciones.find((obj) => obj.indice === indice);
const existe = elementoALlenar.relaciones.find((obj) => obj.id === indice);

if (!existe) {
elementoALlenar.relaciones.push({ conteo: 1, indice, tipo: campoRelacion });
elementoALlenar.relaciones.push({ conteo: 1, id: indice, tipo: campoRelacion });
} else {
existe.conteo++;
}
Expand Down
19 changes: 11 additions & 8 deletions aplicaciones/procesador/fuente/procesadorPublicaciones.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function procesarLista(llaveLista: LlavesPublicaciones, valor: string) {
const existe = listas[llaveLista].find((obj) => obj.slug === slug);
if (!existe) {
listas[llaveLista].push({
id: `${listas[llaveLista].length}`,
nombre,
slug,
conteo: 1,
Expand Down Expand Up @@ -66,7 +67,7 @@ export default async (

if (titulo) {
const slug = slugificar(titulo);
const publicacion: Publicacion = { id: numeroFila - 2, titulo: { nombre: titulo, slug } };
const publicacion: Publicacion = { id: `${numeroFila - 2}`, titulo: { nombre: titulo, slug } };

/** Autores */
if (fila[0]) {
Expand Down Expand Up @@ -148,6 +149,8 @@ export default async (
}
publicacion.dependencias.push({ nombre, slug });
});
} else {
errata.push({ fila: numeroFila, error: 'No tiene DEPENDENCIA' });
}

/** Indicadores */
Expand Down Expand Up @@ -229,9 +232,9 @@ export default async (
const slugsCampoProyecto = aplanarDefinicionesASlugs(datosRelacion);

slugsCampoProyecto.forEach((slug) => {
const indice = listas[campoRelacion].findIndex((obj) => obj.slug === slug);
const existe = listas[campoRelacion].find((obj) => obj.slug === slug);

if (indice >= 0) {
if (existe) {
if (!publicacion[campo]) return;

let datos = publicacion[campo];
Expand All @@ -252,7 +255,7 @@ export default async (
// }

const elementosDondeConectar = aplanarDefinicionesASlugs(datos);
llenarRelacion(elementosDondeConectar, listas[campo], indice, campoRelacion, id);
llenarRelacion(elementosDondeConectar, listas[campo], existe.id, campoRelacion, id);
} else {
console.log('Esto no puede pasar');
}
Expand All @@ -274,17 +277,17 @@ export default async (
function llenarRelacion(
elementosDondeConectar: string[],
elementoLista: ElementoLista[],
indice: number,
indice: string,
campoRelacion: LlavesPublicaciones,
id: number
id: string
) {
elementosDondeConectar.forEach((elementoConector) => {
const elementoALlenar = elementoLista.find((obj) => obj.slug === elementoConector);
if (elementoALlenar) {
const existe = elementoALlenar.relaciones.find((obj) => obj.indice === indice);
const existe = elementoALlenar.relaciones.find((obj) => obj.id === indice);

if (!existe) {
elementoALlenar.relaciones.push({ conteo: 1, indice, tipo: campoRelacion });
elementoALlenar.relaciones.push({ conteo: 1, id: indice, tipo: campoRelacion });
} else {
existe.conteo++;
}
Expand Down
2 changes: 1 addition & 1 deletion aplicaciones/www/estaticos/datos/colectivos.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion aplicaciones/www/estaticos/datos/errataColectivos.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"fila":115,"error":"No tiene DEPENDENCIA."},{"fila":414,"error":"No tiene DEPENDENCIA."}]
[{"fila":115,"error":"No tiene DEPENDENCIA"},{"fila":414,"error":"No tiene DEPENDENCIA"}]
2 changes: 1 addition & 1 deletion aplicaciones/www/estaticos/datos/listasColectivos.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion aplicaciones/www/estaticos/datos/listasPublicaciones.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion aplicaciones/www/estaticos/datos/publicaciones.json

Large diffs are not rendered by default.

105 changes: 70 additions & 35 deletions aplicaciones/www/src/cerebros/ficha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import type {
LlavesPublicaciones,
Publicacion,
} from '@/tipos/compartidos';
import { nombresListas } from '@/utilidades/constantes';
import { crearUrlsEnTexto } from '@/utilidades/ayudas';
import { nombresListas } from '@/utilidades/constantes';

export const usarCerebroFicha = defineStore('cerebroFichas', {
state: (): CerebroFicha => {
return {
fichaVisible: false,
datosFicha: null,
indiceActual: -1,
idActual: '',
totalNodos: 0,
llaveLista: 'autores',
};
Expand All @@ -27,26 +27,53 @@ export const usarCerebroFicha = defineStore('cerebroFichas', {
actions: {
cerrarFicha() {
this.fichaVisible = false;
this.indiceActual = -1;
this.idActual = '';
},

fichaAnterior() {
if (this.indiceActual === 0) {
this.seleccionarNodo(this.totalNodos - 1, this.llaveLista);
} else {
this.seleccionarNodo(this.indiceActual - 1, this.llaveLista);
cambiarFicha(id: string, tipo: TiposNodo, direccion: 'adelante' | 'atras' = 'adelante') {
const { colectivos, listasColectivos, publicaciones, listasPublicaciones } = usarCerebroDatos();
const { paginaActual } = usarCerebroGeneral();
let lista: ElementoLista[] | Colectivo[] | Publicacion[] | null = null;

if (paginaActual === 'colectivos') {
if (tipo === 'colectivos') {
lista = colectivos;
} else {
lista = listasColectivos ? listasColectivos[tipo as LlavesColectivos] : null;
}
} else if (paginaActual === 'publicaciones') {
if (tipo === 'publicaciones') {
lista = publicaciones;
} else {
lista = listasPublicaciones ? listasPublicaciones[tipo as LlavesPublicaciones] : null;
}
}
},

fichaSiguiente() {
if (this.indiceActual >= this.totalNodos - 1) {
this.seleccionarNodo(0, this.llaveLista);
} else {
this.seleccionarNodo(this.indiceActual + 1, this.llaveLista);
if (lista) {
const indice = lista.findIndex((obj) => obj.id === id);
let nuevoId = '';

if (direccion === 'atras') {
if (indice >= 0) {
if (indice === 0) {
nuevoId = lista[this.totalNodos - 1].id;
} else {
nuevoId = lista[indice - 1].id;
}
}
} else if (direccion === 'adelante') {
if (indice >= this.totalNodos - 1) {
nuevoId = lista[0].id;
} else {
nuevoId = lista[indice + 1].id;
}
}

this.seleccionarNodo(nuevoId, this.llaveLista);
}
},

seleccionarNodo(i: number, tipo: TiposNodo) {
seleccionarNodo(id: string, tipo: TiposNodo) {
const {
listasColectivos,
listasPublicaciones,
Expand All @@ -58,30 +85,36 @@ export const usarCerebroFicha = defineStore('cerebroFichas', {
const { paginaActual } = usarCerebroGeneral();

const datosFicha: DatosFicha = {
tipo: nombresListas[tipo],
id,
tipo,
nombreTipo: nombresListas[tipo],
titulo: '',
resumen: '',
};

if (paginaActual === 'colectivos') {
if (tipo === 'colectivos' && colectivos) {
llenarDatosFichaColectivo(colectivos[i]);
const colectivo = colectivos.find((obj) => obj.id === id);
if (colectivo) llenarDatosFichaColectivo(colectivo);
this.totalNodos = colectivos.length;
} else if (listasColectivos) {
llenarDatosFicha(listasColectivos[tipo as LlavesColectivos][i]);
const datos = listasColectivos[tipo as LlavesColectivos].find((obj) => obj.id === id);
if (datos) llenarDatosFicha(datos);
this.totalNodos = listasColectivos[tipo as LlavesColectivos].length;
}
} else if (paginaActual === 'publicaciones') {
if (tipo === 'publicaciones' && publicaciones) {
llenarDatosFichaPublicacion(publicaciones[i]);
const publicacion = publicaciones.find((obj) => obj.id === id);
if (publicacion) llenarDatosFichaPublicacion(publicacion);
this.totalNodos = publicaciones.length;
} else if (listasPublicaciones) {
llenarDatosFicha(listasPublicaciones[tipo as LlavesPublicaciones][i]);
const datos = listasPublicaciones[tipo as LlavesPublicaciones].find((obj) => obj.id === id);
if (datos) llenarDatosFicha(datos);
this.totalNodos = listasPublicaciones[tipo as LlavesPublicaciones].length;
}
}

this.indiceActual = i;
this.idActual = id;
this.fichaVisible = true;
this.llaveLista = tipo;
this.datosFicha = datosFicha;
Expand All @@ -99,9 +132,9 @@ export const usarCerebroFicha = defineStore('cerebroFichas', {
if (datos.dependencias) {
datosFicha.dependencias = [];
datos.dependencias.forEach((obj) => {
const indice = listasColectivos?.dependencias.findIndex((dep) => dep.slug === obj.slug);
if (indice && indice >= 0) {
datosFicha.dependencias?.push({ nombre: obj.nombre, conteo: 1, indice });
const existe = listasColectivos?.dependencias.find((dep) => dep.slug === obj.slug);
if (existe) {
datosFicha.dependencias?.push({ nombre: obj.nombre, conteo: 1, id: existe.id });
}
});
}
Expand All @@ -115,9 +148,9 @@ export const usarCerebroFicha = defineStore('cerebroFichas', {

if (lista) {
datosFicha.indicadores = [];
const indice = lista.indicadores.findIndex((obj) => obj.slug === datos.indicadores?.slug);
if (indice && indice >= 0) {
datosFicha.indicadores.push({ nombre: datos.indicadores.nombre, conteo: 1, indice });
const existe = lista.indicadores.find((obj) => obj.slug === datos.indicadores?.slug);
if (existe) {
datosFicha.indicadores.push({ nombre: datos.indicadores.nombre, conteo: 1, id: existe.id });
}
}
}
Expand All @@ -127,9 +160,9 @@ export const usarCerebroFicha = defineStore('cerebroFichas', {

if (lista) {
datosFicha.tipos = [];
const indice = lista.tipos.findIndex((obj) => obj.slug === datos.tipos?.slug);
if (indice && indice >= 0) {
datosFicha.tipos.push({ nombre: datos.tipos.nombre, conteo: 1, indice });
const existe = lista.tipos.find((obj) => obj.slug === datos.tipos?.slug);
if (existe) {
datosFicha.tipos.push({ nombre: datos.tipos.nombre, conteo: 1, id: existe.id });
}
}
}
Expand All @@ -153,7 +186,7 @@ export const usarCerebroFicha = defineStore('cerebroFichas', {
const colectivo = colectivos.find((obj) => obj.id === id);

if (colectivo && datosFicha.colectivos) {
datosFicha.colectivos.push({ nombre: colectivo.titulo.nombre, conteo: 1, indice: id });
datosFicha.colectivos.push({ nombre: colectivo.titulo.nombre, conteo: 1, id });
}
});
}
Expand All @@ -165,7 +198,7 @@ export const usarCerebroFicha = defineStore('cerebroFichas', {
const publicacion = publicaciones.find((obj) => obj.id === id);

if (publicacion && datosFicha.publicaciones) {
datosFicha.publicaciones.push({ nombre: publicacion.titulo.nombre, conteo: 1, indice: id });
datosFicha.publicaciones.push({ nombre: publicacion.titulo.nombre, conteo: 1, id });
}
});
}
Expand All @@ -190,16 +223,18 @@ export const usarCerebroFicha = defineStore('cerebroFichas', {
let nombre = '';

if (paginaActual === 'colectivos' && listasColectivos) {
nombre = listasColectivos[obj.tipo as LlavesColectivos][obj.indice].nombre;
const datos = listasColectivos[obj.tipo as LlavesColectivos].find((c) => c.id === obj.id);
if (datos) nombre = datos.nombre;
} else if (paginaActual === 'publicaciones' && listasPublicaciones) {
nombre = listasPublicaciones[obj.tipo as LlavesPublicaciones][obj.indice].nombre;
const datos = listasPublicaciones[obj.tipo as LlavesPublicaciones].find((p) => p.id === obj.id);
if (datos) nombre = datos.nombre;
}

if (!datosFicha[obj.tipo]) {
datosFicha[obj.tipo] = [];
}

datosFicha[obj.tipo]?.push({ nombre, conteo: obj.conteo, indice: obj.indice });
datosFicha[obj.tipo]?.push({ nombre, conteo: obj.conteo, id: obj.id });
});
}
},
Expand Down
Loading

0 comments on commit 6db492b

Please sign in to comment.