import React, { useState, useEffect } from "react"; import { MaterialReactTable } from "material-react-table"; import { useDispatch, useSelector } from "react-redux"; import { useTranslation } from "react-i18next"; import { Card, CardHeader, CardTitle, Alert } from "reactstrap"; import { getVMEmissionSummary } from "../../redux/actions/mainDataTables/index"; import { editNumbers } from "../../components/edit-numbers"; function MainDataTables() { const { t } = useTranslation(); const dispatch = useDispatch(); const mainDataTablesStore = useSelector((state) => state.mainDataTables); const [error, setError] = useState(null); useEffect(() => { try { // Fetch VM emission data dispatch(getVMEmissionSummary()); } catch (err) { console.error('Error in MainDataTables:', err); setError(err.message); } }, [dispatch]); // Debug log for store data useEffect(() => { console.log('Current store data:', mainDataTablesStore); }, [mainDataTablesStore]); const columns = [ { header: "VM ID", accessorKey: "vmId", Cell: ({ cell }) => {cell.getValue() || "-"}, }, { header: "Data Center", accessorKey: "dataCenter", Cell: ({ cell }) => {cell.getValue() || "-"}, }, { header: "Project", accessorKey: "project", Cell: ({ cell }) => {cell.getValue() || "-"}, }, { header: "Physical Machine", accessorKey: "physicalMachine", Cell: ({ cell }) => {cell.getValue() || "-"}, }, { header: "Virtual Machine", accessorKey: "vmName", Cell: ({ cell }) => {cell.getValue() || "-"}, }, { header: "VM Power (W)", accessorKey: "vmPower", Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"}, }, { header: "VM Status", accessorKey: "vmStatus", Cell: ({ cell }) => {cell.getValue() || "-"}, }, { header: "Total Emissions", accessorKey: "totalEmission", Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"}, }, { header: "CO2", accessorKey: "co2", Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"}, }, { header: "CH4", accessorKey: "ch4", Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"}, }, { header: "N2O", accessorKey: "n2o", Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"}, }, { header: "Created Date", accessorKey: "createdDate", Cell: ({ cell }) => ( {cell.getValue() ? new Date(cell.getValue()).toLocaleString() : "-"} ), }, ]; const tableData = mainDataTablesStore?.vmEmissionSummary || []; console.log('VM Emission data:', tableData); if (error) { return ( Error loading data: {error} ); } return (
{t("Carbon Emission Data")}
); } export { MainDataTables }; export default MainDataTables;