Skip to content

permissions in profile page #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 83 additions & 7 deletions client/app/profile/[userId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,35 @@ import { useRouter } from "next/navigation";
import { EditIcon, Factory, CircleUser } from "lucide-react";
import {useEffect, useState} from "react";
import { ProfileEditModal } from "@/components/modals/ProfileEditModal";
import useGetAllRole from "@/hooks/user_data/useGetAllRole";

type RolesWithPermisson = {
id: string;
name: string;
permissions: [{
name: string;
description: string;
}]
};


export default function ProfilePage() {

const router = useRouter();
const { user, stsDetails, landfillDetails, getUserDetails} = useGetUserProfile(); // Destructure user and getUserDetails
const [role, setRole] = useState<string>("Role Name");
// const [permissions, setPermissions] = useState<RolesWithPermisson[]>([]);
const RolePlace = 'Station';

const { fetchAllRoles, roles, rolesWithPermissions } = useGetAllRole();
useEffect(() => {
getUserDetails();
setRole(user.roleName);
const fetchData = async () => {
await getUserDetails();
await fetchAllRoles();

setRole(user.roleName);
};

fetchData();

}, []);

Expand Down Expand Up @@ -64,7 +80,7 @@ export default function ProfilePage() {

<Factory className="w-24 h-24" />

{user?.roleName === 'STS_MANAGER' && <div>
{user?.roleName === 'STS_MANAGER' && stsDetails?.stsId?.toString().length > 1 &&<div>
<div className="font-bold text-2xl mb-4">STS Details</div>
<p><span className="font-bold">Id: </span>{stsDetails.stsId}</p>
<p><span className="font-bold">STS Name: </span>{stsDetails.stsName}</p>
Expand All @@ -77,23 +93,83 @@ export default function ProfilePage() {
</div>

}
{user?.roleName === 'LAND_MANAGER' && <div>
<div className="font-bold text-2xl my-4">Landfill Details</div>

{user?.roleName === 'STS_MANAGER' && stsDetails?.stsId?.toString().length < 1 && <div>
<div className="font-bold text-2xl my-4">STS Not Assigned</div>
Call your admin to assign your STS.
</div>
}
{user?.roleName === 'STS_MANAGER' && rolesWithPermissions.some(role => role.name === 'STS_MANAGER') && (
<div>
<h1><b>STS Manager Permissions:</b></h1>
<ul>
{rolesWithPermissions
.find(role => role.name === 'STS_MANAGER')
?.permissions.map(permission => (
<li key={permission.name}>
<strong>{permission.name}:</strong> {permission.description}
</li>
))}
</ul>
</div>
)}


{user?.roleName === 'LAND_MANAGER' && landfillDetails?.landfillId?.toString().length > 1 &&<div>
<div className="font-bold text-2xl my-4">Landfill Details </div>
<p><span className="font-bold">ID: </span>{landfillDetails.landfillId}</p>
<p><span className="font-bold">Landfill Name: </span>{landfillDetails.landFillName}</p>
<p><span className="font-bold">Capacity: </span>{landfillDetails.landFillCapacity}</p>
<p><span className="font-bold">Current Total Waste: </span>{landfillDetails.landFillCurrentWaste}</p>
<p><span className="font-bold">Coordinate: </span>{landfillDetails.landfillLatitude}, {landfillDetails.landFillLongitude}</p>
</div>}

{user?.roleName === 'LAND_MANAGER' && landfillDetails?.landfillId?.toString().length < 1 && (
<div>
<div className="font-bold text-2xl my-4">LandFill Not Assigned</div>
Call your admin to assign your Landfill.
</div>
)}

{user?.roleName === 'LAND_MANAGER' && rolesWithPermissions.some(role => role.name === 'LAND_MANAGER') && (
<div>
<h1><b>Land Manager Permissions:</b></h1>
<ul>
{rolesWithPermissions
.find(role => role.name === 'LAND_MANAGER')
?.permissions.map(permission => (
<li key={permission.name}>
<strong>{permission.name}:</strong> {permission.description}
</li>
))}
</ul>
</div>
)}

</div>}
{user?.roleName === 'SYSTEM_ADMIN' && <div>
<div className="font-bold text-2xl my-4">Admin</div>
<div>You are admin</div>
</div>}
{user?.roleName !== 'STS_MANAGER' && user?.roleName !== 'LAND_MANAGER' && user?.roleName !== 'SYSTEM_ADMIN' && (
<div>Oops! Your role has not assigned yet</div>
)}

{user?.roleName === 'SYSTEM_ADMIN' && rolesWithPermissions.some(role => role.name === 'SYSTEM_ADMIN') && (
<div>
<h1><b>System Admin Permissions:</b></h1>
<ul>
{rolesWithPermissions
.find(role => role.name === 'SYSTEM_ADMIN')
?.permissions.map(permission => (
<li key={permission.name}>
<strong>{permission.name}:</strong> {permission.description}
</li>
))}
</ul>
</div>
)}


</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion client/hooks/user_data/useGetAllRole.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ export default function useGetAllRole() {
)};
}));
await setRoles([unassigned, admin, landfillManager, stsManager]);
console.log(roles);
// console.log(roles);
} catch (error: any) {
alert("Error fetching roles... Are you authorized?");
console.log(error.message);
}
}

useEffect(() => {

fetchAllRoles();
}, []);

Expand Down
37 changes: 33 additions & 4 deletions client/hooks/user_data/useGetUserProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type User = {
profileName: string;
roleName: string;
roleDescription: string;

userCreated: string;
userUpdated: string;


};

Expand All @@ -25,6 +29,9 @@ type STSType = {
stsCurrentTotalWaste: string;
stsLatitude: string;
stsLongitude: string;

roleCreated: string;
roleUpdated: string;
}

type LandfillType = {
Expand All @@ -34,6 +41,9 @@ type LandfillType = {
landFillCurrentWaste: string;
landfillLatitude: string;
landFillLongitude: string;

roleCreated: string;
roleUpdated: string;
}

export default function useGetUserProfile() {
Expand All @@ -44,6 +54,8 @@ export default function useGetUserProfile() {
profileName: '',
roleName: '',
roleDescription: '',
userCreated: '',
userUpdated: ''
}); // Initialize with undefined
const [stsDetails, setStsDetails] = useState<STSType >({
stsId: '',
Expand All @@ -53,6 +65,8 @@ export default function useGetUserProfile() {
stsCurrentTotalWaste: '',
stsLatitude: '',
stsLongitude: '',
roleCreated: '',
roleUpdated: ''
});

const [landfillDetails, setLandfillDetails] = useState<LandfillType>({
Expand All @@ -62,6 +76,8 @@ export default function useGetUserProfile() {
landFillCurrentWaste: '',
landfillLatitude: '',
landFillLongitude: '',
roleCreated: '',
roleUpdated: ''
});

async function getUserDetails() {
Expand All @@ -70,7 +86,7 @@ export default function useGetUserProfile() {
headers: { Authorization: `Bearer ${getCookie(jwtToken)}` },

});
console.log(res.data);
// console.log(res.data);
if (res.data.roleName === "STS_MANAGER" ) {
const userDetails: User = {
id: res.data.id,
Expand All @@ -80,6 +96,10 @@ export default function useGetUserProfile() {
roleName: res.data.roleName,
roleDescription: res.data.role.description,

userCreated: res.data.createdAt,
userUpdated: res.data.updatedAt



};

Expand All @@ -92,6 +112,9 @@ export default function useGetUserProfile() {
stsCurrentTotalWaste: res.data.sts.currentTotalWaste,
stsLatitude: res.data.sts.latitude,
stsLongitude: res.data.sts.longitude,

roleCreated: res.data.role.createdAt,
roleUpdated: res.data.role.updatedAt
}
setStsDetails(ResStsDetails);
}
Expand All @@ -110,7 +133,8 @@ export default function useGetUserProfile() {
roleName: res.data.roleName,
roleDescription: res.data.role.description,


userCreated: res.data.createdAt,
userUpdated: res.data.updatedAt
};

if(res.data.landfillId){
Expand All @@ -122,6 +146,9 @@ export default function useGetUserProfile() {
landFillCurrentWaste: res.data.landfill.currentTotalWaste,
landfillLatitude: res.data.landfill.latitude,
landFillLongitude: res.data.landfill.longitude,

roleCreated: res.data.role.createdAt,
roleUpdated: res.data.role.updatedAt
};
setLandfillDetails(ResLandDetails);

Expand All @@ -138,7 +165,8 @@ export default function useGetUserProfile() {
roleName: res.data.roleName,
roleDescription: res.data.role.description,


userCreated: res.data.createdAt,
userUpdated: res.data.updatedAt
};


Expand All @@ -153,7 +181,8 @@ export default function useGetUserProfile() {
roleName: res.data.roleName,
roleDescription: res.data.role.description,


userCreated: res.data.createdAt,
userUpdated: res.data.updatedAt
};
setUser(userDetails);

Expand Down