1
0

Refactor HistoryCharts and PollutionMap components for improved date handling; update SummaryCards data mapping and Card component prop types.

This commit is contained in:
Jose
2025-05-18 19:58:58 +02:00
parent cdff306ca1
commit 934ac521a3
5 changed files with 58 additions and 58 deletions

View File

@@ -23,7 +23,7 @@ const HistoryCharts = ({ groupId, deviceId }) => {
const ENDPOINT = config.appConfig.endpoints.GET_DEVICE_HISTORY;
const endp = ENDPOINT
.replace(':groupId', groupId)
.replace(':deviceId', deviceId); // si tu endpoint lo necesita
.replace(':deviceId', deviceId);
const reqConfig = {
baseUrl: `${BASE}${endp}`,
@@ -56,22 +56,14 @@ const HistoryChartsContent = () => {
carbonMonoxide: []
};
const threeDaysAgo = new Date();
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3);
const isToday = (timestamp) => {
const date = new Date(timestamp * 1000);
return (
date.getUTCFullYear() >= threeDaysAgo.getUTCFullYear() &&
date.getUTCMonth() >= threeDaysAgo.getUTCMonth() &&
date.getUTCDate() >= threeDaysAgo.getUTCDate()
);
};
const threeDaysAgo = Date.now() - (3 * 24 * 60 * 60 * 1000); // hace 3 días en ms
const isRecent = (timestamp) => (timestamp * 1000) >= threeDaysAgo;
data?.forEach(sensor => {
if (
sensor.value != null &&
grouped[sensor.valueType] &&
isToday(sensor.timestamp)
isRecent(sensor.timestamp)
) {
grouped[sensor.valueType].push({
timestamp: sensor.timestamp * 1000,
@@ -81,6 +73,8 @@ const HistoryChartsContent = () => {
});
console.log("Grouped data:", grouped);
const sortAndExtract = (entries) => {
const sorted = entries.sort((a, b) => a.timestamp - b.timestamp);

View File

@@ -42,6 +42,18 @@ const PollutionMapContent = () => {
useEffect(() => {
if (!data || !config) return;
const isToday = (timestamp) => {
const today = new Date();
const date = new Date(timestamp * 1000); // si viene en segundos
return (
today.getFullYear() === date.getFullYear() &&
today.getMonth() === date.getMonth() &&
today.getDate() === date.getDate()
);
};
const mapContainer = document.getElementById("map");
if (!mapContainer) return;
@@ -54,14 +66,16 @@ const PollutionMapContent = () => {
'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
const points = data.map((p) => [p.lat, p.lon, p.carbonMonoxide]);
const points = data
.filter(p => isToday(p.timestamp))
.map(p => [p.lat, p.lon, p.carbonMonoxide]);
L.heatLayer(points, { radius: 25 }).addTo(map);
return () => {
map.remove();
};
}, [data, config]);
}, [data, config]);
if (configLoading) return <p>Cargando configuración...</p>;
if (configError) return <p>Error al cargar configuración: {configError}</p>;

View File

@@ -86,8 +86,8 @@ const SummaryCardsContent = () => {
if (data) {
let coData = data[2];
let tempData = data[1];
let coData = data[1];
let tempData = data[2];
CardsData[0].content = tempData.temperature + "°C";
CardsData[0].status = "Temperatura actual";

View File

@@ -82,15 +82,7 @@ Card.propTypes = {
link: PropTypes.bool,
to: PropTypes.string,
text: PropTypes.bool,
};
Card.defaultProps = {
styleMode: "",
className: "",
style: {},
link: false,
to: "",
text: false,
marquee: PropTypes.bool,
};
export default Card;

View File

@@ -110,7 +110,7 @@ const GroupViewContent = () => {
link: gpsSensor != undefined,
text: gpsSensor == undefined,
marquee: gpsSensor == undefined,
content: gpsSensor == undefined ? "SOLO VEHICULOS ELECTRICOS" : mapPreview,
content: gpsSensor == undefined ? "TODO TIPO DE VEHICULOS" : mapPreview,
to: `/groups/${groupId}/devices/${device.deviceId}`,
className: `col-12 col-md-6 col-lg-4 ${gpsSensor == undefined ? "led" : ""}`,
};