import React, { useEffect, useState } from "react"; import { useSelector } from "react-redux"; import { useHasUserPermission } from "../../hooks/useHasUserPermission"; import { ITEMS_PER_PAGE, MANAGE_MASTER } from "../../utils/constants"; import showToast from "../../services/toastService"; const MasterTable = ({ data, columns, loading, handleModalData }) => { const hasMasterPermission = useHasUserPermission(MANAGE_MASTER); const selectedMaster = useSelector( (store) => store.localVariables.selectedMaster ); const hiddenColumns = [ "id", "featurePermission", "tenant", "tenantId", "checkLists", "isSystem", "isActive", "noOfPersonsRequired", "color", "displayName", "permissionIds", "entityTypeId", "regexExpression", "isMandatory", "maxFilesAllowed", "maxSizeAllowedInMB", "isValidationRequired", "documentCategory", ]; const safeData = Array.isArray(data) ? data : []; const [currentPage, setCurrentPage] = useState(1); const [itemsPerPage] = useState(ITEMS_PER_PAGE); const sortKeys = { "Application Role": "role", Activity: "activityName", "Job Role": "name", }; const sortKey = sortKeys[selectedMaster]; const sortedData = [...safeData].sort((a, b) => { if (!sortKey) return 0; const aValue = a[sortKey] || ""; const bValue = b[sortKey] || ""; return aValue?.localeCompare(bValue); }); // Pagination logic const indexOfLastItem = currentPage * itemsPerPage; const indexOfFirstItem = indexOfLastItem - itemsPerPage; const currentItems = sortedData.slice(indexOfFirstItem, indexOfLastItem); useEffect(() => { setCurrentPage(1); }, [safeData]); const paginate = (pageNumber) => setCurrentPage(pageNumber); const totalPages = Math.ceil(safeData.length / itemsPerPage); const updatedColumns = columns .filter((col) => !hiddenColumns.includes(col.key)) .map((col) => ({ ...col, label: col.key === "role" || col.key === "activityName" || col.key === "name" ? "Name" : col.label, })); const handleSystemDefined = (message) => { if (message) { showToast( `The system-defined item ${selectedMaster} cannot be ${message}.` ); } }; return (
{loading ? (

Loading...

) : ( {currentItems.length > 0 ? ( currentItems.map((item, index) => ( {updatedColumns.map((col) => ( ))} )) ) : ( )}
{" "} {selectedMaster === "Activity" ? "Activity" : "Name"} {" "} {selectedMaster === "Activity" ? "Unit" : selectedMaster === "Document Type" ? "Content Type" : "Description"} Actions
{col.key === "description" ? ( item[col.key] !== undefined && item[col.key] !== null ? ( item[col.key].length > 80 ? ( <>{item[col.key].slice(0, 80)}... ) : ( item[col.key] ) ) : ( " --- " ) ) : item[col.key] !== undefined && item[col.key] !== null ? ( item[col.key] ) : ( " --- " )} {(selectedMaster === "Application Role" || selectedMaster === "Work Category") && item?.isSystem ? ( <> ) : ( <> {selectedMaster === "Services" && ( )} )}
No results found.
)} {/* Pagination */} {!loading && safeData.length > 20 && ( )}
); }; export default MasterTable;