Skip to content

Commit

Permalink
Merge pull request #41 from chaitanyakole/role_based_permissions
Browse files Browse the repository at this point in the history
Task #234974 Implement Role-Based Action Permissions for Add, Edit, and Delete
  • Loading branch information
gouravmore authored Feb 11, 2025
2 parents 6b6c9ff + 76bc536 commit 28c15a2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 61 deletions.
54 changes: 21 additions & 33 deletions src/components/ActionIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import cohortIcon from "../../public/images/apartment.svg";
import addIcon from "../../public/images/addIcon.svg";

import Image from "next/image";
import { useRouter } from "next/router";

interface ActionCellProps {
onEdit: (rowData: any) => void;
Expand Down Expand Up @@ -39,37 +40,38 @@ const ActionIcon: React.FC<ActionCellProps> = ({
}) => {
const { t } = useTranslation();
const theme = useTheme<any>();
const router = useRouter();

const isCohortAdmin = rowData?.userRoleTenantMapping?.code === "cohort_admin";
const isLearnersPage = router.pathname === "/learners";
const isActionAllowed = isCohortAdmin && !isLearnersPage;
return (
<Box
sx={{
display: "flex",
flexDirection: "row",
gap: "20px",
alignItems: "center",
pointerEvents: disable ? "none" : "auto",
pointerEvents: isActionAllowed ? "none" : "auto",
opacity: isActionAllowed ? 0.5 : 1,
}}
>
{roleButton && (
<Tooltip title={t("COMMON.ADD")}>
<Button
onClick={() => {
onAdd(rowData);
if (!isActionAllowed) onAdd(rowData);
}}
disabled={isActionAllowed}
sx={{
display: "flex",
flexDirection: "column",
alignItems: "center",
cursor: "pointer",
color: disable ? theme?.palette?.secondary.contrastText : "",
backgroundColor: "#EAF2FF",
cursor: isActionAllowed ? "not-allowed" : "pointer",
backgroundColor: isActionAllowed ? "#d3d3d3" : "#EAF2FF",
p: "10px",
"&:hover": {
backgroundColor: "#d0e5ff", // Optional: adjust hover color
},
}}
>
{/* Optional typography or icon within the button */}
<Typography variant="body2" fontFamily={"Poppins"}>
{t("COMMON.ADD")}
</Typography>
Expand All @@ -81,7 +83,7 @@ const ActionIcon: React.FC<ActionCellProps> = ({
<Tooltip title={t("COMMON.ADD")}>
<Box
onClick={() => {
onAdd(rowData);
if (!isActionAllowed) onAdd(rowData);
}}
sx={{
display: "flex",
Expand All @@ -106,45 +108,36 @@ const ActionIcon: React.FC<ActionCellProps> = ({
<Tooltip title={t("COMMON.EDIT")}>
<Box
onClick={() => {
onEdit(rowData);
if (!isActionAllowed) onEdit(rowData);
}}
sx={{
display: "flex",
flexDirection: "column",
alignItems: "center",
cursor: "pointer",
color: disable ? theme?.palette?.secondary.contrastText : "",
backgroundColor: "#E3EAF0",
cursor: isActionAllowed ? "not-allowed" : "pointer",
backgroundColor: isActionAllowed ? "#d3d3d3" : "#E3EAF0",
p: "10px",
}}
>
<Image src={editIcon} alt="" />
{/* <Typography variant="body2" fontFamily={"Poppins"}>
{t("COMMON.EDIT")}
</Typography> */}
</Box>
</Tooltip>

<Tooltip title={t("COMMON.DELETE")}>
<Box
onClick={() => {
onDelete(rowData);
if (!isActionAllowed) onDelete(rowData);
}}
sx={{
display: "flex",
flexDirection: "column",
alignItems: "center",
cursor: "pointer",
color: disable ? theme?.palette?.secondary.contrastText : "",
backgroundColor: "#EAF2FF",
cursor: isActionAllowed ? "not-allowed" : "pointer",
backgroundColor: isActionAllowed ? "#d3d3d3" : "#EAF2FF",
p: "10px",
}}
>
<Image src={deleteIcon} alt="" />
{/*
<Typography variant="body2" fontFamily={"Poppins"}>
{t("COMMON.DELETE")}
</Typography> */}
</Box>
</Tooltip>
</>
Expand All @@ -154,23 +147,18 @@ const ActionIcon: React.FC<ActionCellProps> = ({
<Tooltip title={reassignType}>
<Box
onClick={() => {
if (reassignCohort) reassignCohort(rowData);
if (!isActionAllowed && reassignCohort) reassignCohort(rowData);
}}
sx={{
display: "flex",
flexDirection: "column",
alignItems: "center",
cursor: "pointer",
color: disable ? theme?.palette?.secondary.contrastText : "",
backgroundColor: "#E5E5E5",
cursor: isActionAllowed ? "not-allowed" : "pointer",
backgroundColor: isActionAllowed ? "#d3d3d3" : "#E5E5E5",
p: "10px",
}}
>
<Image src={cohortIcon} alt="" />
{/*
<Typography variant="body2" fontFamily={"Poppins"}>
{t("COMMON.DELETE")}
</Typography> */}
</Box>
</Tooltip>
)}
Expand Down
76 changes: 48 additions & 28 deletions src/data/tableColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const getUserTableColumns = (t: any, isMobile: boolean) => {
return generateColumns(t, configs, isMobile);
};

export const getTLTableColumns = (t: any, isMobile: boolean, filter:any) => {
export const getTLTableColumns = (t: any, isMobile: boolean, filter: any) => {
const configs: ColumnConfig[] = [
{ key: "name", titleKey: "TABLE_TITLE.NAME", width: 130 },
{ key: "mobile", titleKey: "TABLE_TITLE.MOBILE", width: 130 },
Expand All @@ -60,30 +60,34 @@ export const getTLTableColumns = (t: any, isMobile: boolean, filter:any) => {
{ key: "createdAt", titleKey: "TABLE_TITLE.CREATED_DATE", width: 130 },
{ key: "updatedAt", titleKey: "TABLE_TITLE.UPDATED_DATE", width: 130 },
// { key: "roleDefine", titleKey: "TABLE_TITLE.ROLE", width: 130 },
...(filter?.status?.[0]!=="archived"?[{
key: "actions",
titleKey: "TABLE_TITLE.ACTIONS",
width: 170,
isSortable: false,
}]:[]),
...(filter?.status?.[0] !== "archived"
? [
{
key: "actions",
titleKey: "TABLE_TITLE.ACTIONS",
width: 170,
isSortable: false,
},
]
: []),
];

return generateColumns(t, configs, isMobile);
};

export const getTenantTableData = (t: any, isMobile: boolean,role:any) => {
console.log({role});
const adminInfo = JSON.parse(localStorage.getItem("adminInfo") ?? '{}');
export const getTenantTableData = (t: any, isMobile: boolean, role: any) => {
console.log({ role });
const adminInfo = JSON.parse(localStorage.getItem("adminInfo") ?? "{}");
// const localRoleCheck = adminInfo?.tenantData?.[0]?.roleName === "cohort admin";
const isCohortAdmin = adminInfo?.tenantData?.[0]?.roleName === "cohort admin" ? true : false;
// const isCohortAdmin = adminInfo?.tenantData?.[0]?.roleName === "cohort admin" ? true : false;

const configs: ColumnConfig[] = [
{ key: "name", titleKey: "TABLE_TITLE.NAME", width: 130 },
// { key: "updatedBy", titleKey: "TABLE_TITLE.UPDATED_BY", width: 130 },
// { key: "createdBy", titleKey: "TABLE_TITLE.CREATED_BY", width: 130 },
{ key: "createdAt", titleKey: "TABLE_TITLE.CREATED_DATE", width: 130 },
// { key: "updatedAt", titleKey: "TABLE_TITLE.UPDATED_DATE", width: 130 },

// {
// key: "totalActiveMembers",
// titleKey: "TABLE_TITLE.ACTIVE_LEARNERS",
Expand All @@ -95,16 +99,28 @@ export const getTenantTableData = (t: any, isMobile: boolean,role:any) => {
// width: 130,
// },
{ key: "status", titleKey: "TABLE_TITLE.STATUS", width: 90 },

...(role == true ? [{ key: "roleDefine", titleKey: "TABLE_TITLE.CREATE_TENANT_ADMIN", width: 130 }] : []),
...(isCohortAdmin ==false?[{ key: "actions", titleKey: "TABLE_TITLE.ACTIONS", width: 125 }]:[]),

...(role == true
? [
{
key: "roleDefine",
titleKey: "TABLE_TITLE.CREATE_TENANT_ADMIN",
width: 130,
},
]
: []),
{ key: "actions", titleKey: "TABLE_TITLE.ACTIONS", width: 125 },
// ...(role== true ?[{ key: "actions", titleKey: "TABLE_TITLE.ACTIONS", width: 125 }]:[]),
];


return generateColumns(t, configs, isMobile);
};
export const getCohortTableData = (t: any, isMobile: boolean,role:any,filter:any) => {
export const getCohortTableData = (
t: any,
isMobile: boolean,
role: any,
filter: any
) => {
const configs: ColumnConfig[] = [
{ key: "name", titleKey: "TABLE_TITLE.NAME", width: 130 },
{ key: "type", titleKey: "TABLE_TITLE.TYPE", width: 90 },
Expand All @@ -124,16 +140,20 @@ export const getCohortTableData = (t: any, isMobile: boolean,role:any,filter:any
// width: 130,
// },
// { key: "roleDefine", titleKey: "TABLE_TITLE.ROLE", width: 130 },


...(
role == true && !(filter?.status?.[0] === "inactive" || filter?.status?.[0] === "archived")
? [{ key: "cohortAdmin", titleKey: "TABLE_TITLE.CREATE_COHORT_ADMIN", width: 130 }]
: []
)

,
...(filter?.status?.[0]!="archived"?[{ key: "actions", titleKey: "TABLE_TITLE.ACTIONS", width: 125 }]:[]),

...(role == true &&
!(filter?.status?.[0] === "inactive" || filter?.status?.[0] === "archived")
? [
{
key: "cohortAdmin",
titleKey: "TABLE_TITLE.CREATE_COHORT_ADMIN",
width: 130,
},
]
: []),
...(filter?.status?.[0] != "archived"
? [{ key: "actions", titleKey: "TABLE_TITLE.ACTIONS", width: 125 }]
: []),
];

return generateColumns(t, configs, isMobile);
Expand Down
1 change: 1 addition & 0 deletions src/pages/cohorts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ const Center: React.FC = () => {
createdAt: item?.createdAt,
updatedAt: item?.updatedAt,
cohortId: item?.cohortId,
userRoleTenantMapping: { code: item?.role },
}));

setCohortData(resultData || []);
Expand Down
2 changes: 2 additions & 0 deletions src/pages/tenant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ interface TenantData {
domain: string;
tenantId: string;
type: string;
userRoleTenantMapping: string;
}
interface RowData {
cohortId: string;
Expand Down Expand Up @@ -302,6 +303,7 @@ const Tenant: React.FC = () => {
createdAt: item.createdAt || "-",
domain: item.domain || "-",
tenantId: item.tenantId || "-",
userRoleTenantMapping: item.userRoleTenantMapping || "-",
};

resultData.push(requiredData);
Expand Down

0 comments on commit 28c15a2

Please sign in to comment.