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 { getMainDataTablesWithPaginate } 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(() => {
const fetchData = async () => {
try {
setLoading(true);
await dispatch(getMainDataTablesWithPaginate());
} catch (err) {
console.error('Error in MainDataTables:', err);
setError(err.message);
} finally {
setLoading(false);
}
};
fetchData();
}, [dispatch]);
// Debug log for store data
useEffect(() => {
console.log('Current store data:', mainDataTablesStore);
}, [mainDataTablesStore]);
const [loading, setLoading] = useState(true);
const columns = [
{
header: t("ID"),
accessorKey: "id",
Cell: ({ cell }) => {cell.getValue() || "-"},
},
{
header: t("Year"),
accessorKey: "year",
Cell: ({ cell }) => {cell.getValue() || "-"},
},
{
header: t("Sector"),
accessorKey: "sector",
Cell: ({ cell, row }) => {row.original?.sector?.tag || "-"},
},
{
header: t("Sub Sector"),
accessorKey: "subSector",
Cell: ({ cell, row }) => {row.original?.subSector?.tag || "-"},
},
{
header: t("Activity Sub Unit"),
accessorKey: "activitySubUnit",
Cell: ({ cell, row }) => {row.original?.activitySubUnit?.tag || "-"},
},
{
header: t("Emission Source"),
accessorKey: "emissionSource",
Cell: ({ cell, row }) => {row.original?.emissionSource?.tag || "-"},
},
{
header: t("Emission Scope"),
accessorKey: "emissionScope",
Cell: ({ cell, row }) => {row.original?.emissionScope?.tag || "-"},
},
{
header: t("Total Emission"),
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: t("Created Date"),
accessorKey: "createdDate",
Cell: ({ cell }) => (
{cell.getValue() ? new Date(cell.getValue()).toLocaleString() : "-"}
),
sortable: true,
},
];
const tableData = mainDataTablesStore?.mainDataTablesWithPaginate?.content || [];
console.log('VM Emission data:', tableData);
if (error) {
return (