Changes
This commit is contained in:
@@ -2,6 +2,7 @@ import { useEffect, useMemo, useState } from "react";
|
||||
import {
|
||||
fetchConcentrators,
|
||||
type Concentrator,
|
||||
type ConcentratorType,
|
||||
} from "../../api/concentrators";
|
||||
import { fetchProjects, type Project } from "../../api/projects";
|
||||
import type { ProjectCard, SampleView } from "./ConcentratorsPage";
|
||||
@@ -78,8 +79,6 @@ export function useConcentrators(currentUser: User) {
|
||||
};
|
||||
|
||||
const loadConcentrators = async () => {
|
||||
if (!isGeneral) return;
|
||||
|
||||
setLoadingConcentrators(true);
|
||||
|
||||
try {
|
||||
@@ -102,14 +101,8 @@ export function useConcentrators(currentUser: User) {
|
||||
|
||||
// view changes
|
||||
useEffect(() => {
|
||||
if (isGeneral) {
|
||||
loadProjects();
|
||||
loadConcentrators();
|
||||
} else {
|
||||
setLoadingProjects(false);
|
||||
setLoadingConcentrators(false);
|
||||
setSelectedProject("");
|
||||
}
|
||||
loadProjects();
|
||||
loadConcentrators();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [sampleView]);
|
||||
|
||||
@@ -121,25 +114,41 @@ export function useConcentrators(currentUser: User) {
|
||||
}
|
||||
}, [visibleProjects, selectedProject, isGeneral]);
|
||||
|
||||
// filter by project
|
||||
useEffect(() => {
|
||||
if (!isGeneral) {
|
||||
setFilteredConcentrators([]);
|
||||
return;
|
||||
}
|
||||
let filtered = concentrators;
|
||||
|
||||
if (selectedProject) {
|
||||
setFilteredConcentrators(
|
||||
concentrators.filter((c) => c.projectId === selectedProject)
|
||||
);
|
||||
} else {
|
||||
setFilteredConcentrators(concentrators);
|
||||
filtered = filtered.filter((c) => c.projectId === selectedProject);
|
||||
}
|
||||
}, [selectedProject, concentrators, isGeneral]);
|
||||
|
||||
if (!isGeneral) {
|
||||
const typeMap: Record<Exclude<SampleView, "GENERAL">, ConcentratorType> = {
|
||||
LORA: "LORA",
|
||||
LORAWAN: "LORAWAN",
|
||||
GRANDES: "GRANDES",
|
||||
};
|
||||
const targetType = typeMap[sampleView as Exclude<SampleView, "GENERAL">];
|
||||
filtered = filtered.filter((c) => c.type === targetType);
|
||||
}
|
||||
|
||||
setFilteredConcentrators(filtered);
|
||||
}, [selectedProject, concentrators, isGeneral, sampleView]);
|
||||
|
||||
// sidebar cards (general)
|
||||
const projectsDataGeneral: ProjectCard[] = useMemo(() => {
|
||||
const counts = concentrators.reduce<Record<string, number>>((acc, c) => {
|
||||
let concentratorsToCount = concentrators;
|
||||
|
||||
if (!isGeneral) {
|
||||
const typeMap: Record<Exclude<SampleView, "GENERAL">, ConcentratorType> = {
|
||||
LORA: "LORA",
|
||||
LORAWAN: "LORAWAN",
|
||||
GRANDES: "GRANDES",
|
||||
};
|
||||
const targetType = typeMap[sampleView as Exclude<SampleView, "GENERAL">];
|
||||
concentratorsToCount = concentrators.filter((c) => c.type === targetType);
|
||||
}
|
||||
|
||||
const counts = concentratorsToCount.reduce<Record<string, number>>((acc, c) => {
|
||||
const project = c.projectId ?? "SIN PROYECTO";
|
||||
acc[project] = (acc[project] ?? 0) + 1;
|
||||
return acc;
|
||||
@@ -165,70 +174,11 @@ export function useConcentrators(currentUser: User) {
|
||||
contact: baseContact,
|
||||
status: "ACTIVO" as const,
|
||||
}));
|
||||
}, [concentrators, visibleProjects, projects]);
|
||||
|
||||
// sidebar cards (mock)
|
||||
const projectsDataMock: Record<Exclude<SampleView, "GENERAL">, ProjectCard[]> =
|
||||
useMemo(
|
||||
() => ({
|
||||
LORA: [
|
||||
{
|
||||
id: "mock-lora-centro",
|
||||
name: "LoRa - Zona Centro",
|
||||
region: "Baja California",
|
||||
projects: 1,
|
||||
concentrators: 12,
|
||||
activeAlerts: 1,
|
||||
lastSync: "Hace 15 min",
|
||||
contact: "Operaciones",
|
||||
status: "ACTIVO",
|
||||
},
|
||||
{
|
||||
id: "mock-lora-este",
|
||||
name: "LoRa - Zona Este",
|
||||
region: "Baja California",
|
||||
projects: 1,
|
||||
concentrators: 8,
|
||||
activeAlerts: 0,
|
||||
lastSync: "Hace 40 min",
|
||||
contact: "Operaciones",
|
||||
status: "ACTIVO",
|
||||
},
|
||||
],
|
||||
LORAWAN: [
|
||||
{
|
||||
id: "mock-lorawan-industrial",
|
||||
name: "LoRaWAN - Industrial",
|
||||
region: "Baja California",
|
||||
projects: 1,
|
||||
concentrators: 5,
|
||||
activeAlerts: 0,
|
||||
lastSync: "Hace 1 h",
|
||||
contact: "Operaciones",
|
||||
status: "ACTIVO",
|
||||
},
|
||||
],
|
||||
GRANDES: [
|
||||
{
|
||||
id: "mock-grandes-convenios",
|
||||
name: "Grandes - Convenios",
|
||||
region: "Baja California",
|
||||
projects: 1,
|
||||
concentrators: 3,
|
||||
activeAlerts: 0,
|
||||
lastSync: "Hace 2 h",
|
||||
contact: "Operaciones",
|
||||
status: "ACTIVO",
|
||||
},
|
||||
],
|
||||
}),
|
||||
[]
|
||||
);
|
||||
}, [concentrators, visibleProjects, projects, isGeneral, sampleView]);
|
||||
|
||||
const projectsData: ProjectCard[] = useMemo(() => {
|
||||
if (isGeneral) return projectsDataGeneral;
|
||||
return projectsDataMock[sampleView as Exclude<SampleView, "GENERAL">];
|
||||
}, [isGeneral, projectsDataGeneral, projectsDataMock, sampleView]);
|
||||
return projectsDataGeneral;
|
||||
}, [projectsDataGeneral]);
|
||||
|
||||
return {
|
||||
// view
|
||||
|
||||
Reference in New Issue
Block a user