138 lines
3.2 KiB
TypeScript
138 lines
3.2 KiB
TypeScript
import React, { useEffect, useState } from "react";
|
|
import { Box, Button, Typography } from "@mui/material";
|
|
import AddEditRoleModal from "../../components/AddEditRoleModal";
|
|
import { useForm } from "react-hook-form";
|
|
import CustomTable, { Column } from "../../components/CustomTable";
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
import { createRole, roleList } from "../../redux/slices/roleSlice";
|
|
import { AppDispatch, RootState } from "../../redux/store/store";
|
|
|
|
export default function RoleList() {
|
|
const [modalOpen, setModalOpen] = useState(false);
|
|
const { reset } = useForm();
|
|
|
|
const [deleteModal, setDeleteModal] = React.useState<boolean>(false);
|
|
const [viewModal, setViewModal] = React.useState<boolean>(false);
|
|
const [rowData, setRowData] = React.useState<any | null>(null);
|
|
|
|
const dispatch = useDispatch<AppDispatch>();
|
|
|
|
const roles = useSelector((state: RootState) => state.roleReducer.roles);
|
|
|
|
useEffect(() => {
|
|
dispatch(roleList());
|
|
}, [dispatch]);
|
|
|
|
const handleClickOpen = () => {
|
|
setRowData(null); // Reset row data when opening for new role
|
|
setModalOpen(!modalOpen);
|
|
};
|
|
|
|
const handleCloseModal = () => {
|
|
setModalOpen(false);
|
|
setRowData(null);
|
|
reset();
|
|
};
|
|
|
|
const handleCreate = async (data: {
|
|
name: string;
|
|
resource: {
|
|
moduleName: string;
|
|
moduleId: string;
|
|
permissions: string[];
|
|
}[];
|
|
}) => {
|
|
try {
|
|
await dispatch(createRole(data));
|
|
await dispatch(roleList()); // Refresh the list after creation
|
|
handleCloseModal();
|
|
} catch (error) {
|
|
console.error("Creation failed", error);
|
|
}
|
|
};
|
|
|
|
const categoryColumns: Column[] = [
|
|
{ id: "srno", label: "Sr No" },
|
|
{ id: "name", label: "Name" },
|
|
{ id: "action", label: "Action", align: "center" },
|
|
];
|
|
|
|
const categoryRows = roles?.length
|
|
? roles?.map(function (
|
|
role: {
|
|
id: string;
|
|
name: string;
|
|
// email: string;
|
|
|
|
// phone: string;
|
|
// location?: string;
|
|
// managerAssigned?: string;
|
|
// vehicle?: string;
|
|
},
|
|
index: number
|
|
) {
|
|
return {
|
|
id: role?.id,
|
|
srno: index + 1,
|
|
name: role?.name,
|
|
// email: user?.email,
|
|
// phone: user?.phone,
|
|
// location: user?.location,
|
|
// managerAssigned: user?.managerAssigned,
|
|
// vehicle: user?.vehicle,
|
|
};
|
|
})
|
|
: [];
|
|
|
|
console.log("Category Rows:", categoryRows);
|
|
|
|
return (
|
|
<>
|
|
<Box
|
|
sx={{
|
|
width: "100%",
|
|
maxWidth: {
|
|
sm: "100%",
|
|
display: "flex",
|
|
justifyContent: "space-between",
|
|
alignItems: "center",
|
|
},
|
|
}}
|
|
>
|
|
<Typography
|
|
component="h2"
|
|
variant="h6"
|
|
sx={{ mt: 2, fontWeight: 600 }}
|
|
>
|
|
Roles
|
|
</Typography>
|
|
<Button
|
|
variant="contained"
|
|
size="medium"
|
|
sx={{ textAlign: "right" }}
|
|
onClick={handleClickOpen}
|
|
>
|
|
Add Role
|
|
</Button>
|
|
</Box>
|
|
|
|
<CustomTable
|
|
columns={categoryColumns}
|
|
rows={categoryRows}
|
|
setDeleteModal={setDeleteModal}
|
|
deleteModal={deleteModal}
|
|
setViewModal={setViewModal}
|
|
viewModal={viewModal}
|
|
setRowData={setRowData}
|
|
setModalOpen={setModalOpen}
|
|
/>
|
|
{/* <AddEditRoleModal
|
|
open={modalOpen}
|
|
handleClose={handleCloseModal}
|
|
handleCreate={handleCreate}
|
|
editRow={rowData}
|
|
/> */}
|
|
</>
|
|
);
|
|
}
|