fixed permission add and remove updation

This commit is contained in:
pramod mahajan 2025-09-13 18:16:56 +05:30
parent 2ef56e7f83
commit 0e3a634205
2 changed files with 60 additions and 59 deletions

View File

@ -43,19 +43,20 @@ const ProjectPermission = () => {
selectedProject selectedProject
); );
useEffect(() => { useEffect(() => {
if (!employees.length) return; if (!selectedEmployee) return;
const enabledPerms = const enabledPerms =
selectedEmpPermissions?.permissions selectedEmpPermissions?.permissions
?.filter((perm) => perm.isEnabled) ?.filter((perm) => perm.isEnabled)
?.map((perm) => perm.id) || []; ?.map((perm) => perm.id) || [];
reset({ reset((prev) => ({
employeeId: selectedEmployee || employees[0]?.id || "", ...prev,
selectedPermissions: enabledPerms, selectedPermissions: enabledPerms,
}); }));
}, [selectedEmpPermissions, reset, selectedEmployee, employees]); }, [selectedEmpPermissions, reset, selectedEmployee]);
const { mutate: updatePermission, isPending } = const { mutate: updatePermission, isPending } =
useUpdateProjectLevelEmployeePermission(); useUpdateProjectLevelEmployeePermission();
@ -67,30 +68,23 @@ const ProjectPermission = () => {
} }
const existingPermissions = selectedEmpPermissions?.permissions || []; const existingPermissions = selectedEmpPermissions?.permissions || [];
const existingEnabledIds = existingPermissions
.filter((p) => p.isEnabled)
.map((p) => p.id);
const payloadPermissions = const newSelectedIds = formData.selectedPermissions || [];
existingPermissions.length > 0
? existingPermissions.map((perm) => ({ const removed = existingEnabledIds
id: perm.id, .filter((id) => !newSelectedIds.includes(id))
isEnabled: formData.selectedPermissions?.includes(perm.id) || false, .map((id) => ({ id, isEnabled: false }));
}))
: (formData.selectedPermissions || []).map((id) => ({ const added = newSelectedIds
id, .filter((id) => !existingEnabledIds.includes(id))
isEnabled: true, .map((id) => ({ id, isEnabled: true }));
}));
const payloadPermissions = [...removed, ...added];
if (payloadPermissions.length === 0) { if (payloadPermissions.length === 0) {
showToast("No permissions selected", "warn");
return;
}
const hasChanges = existingPermissions.some(
(perm) =>
perm.isEnabled !==
(formData.selectedPermissions?.includes(perm.id) || false)
);
if (!hasChanges && existingPermissions.length > 0) {
showToast("No changes detected", "info"); showToast("No changes detected", "info");
return; return;
} }
@ -102,7 +96,8 @@ const ProjectPermission = () => {
}; };
updatePermission(payload); updatePermission(payload);
}; };
return ( return (
<div className="row px-2 py-1"> <div className="row px-2 py-1">
@ -120,8 +115,13 @@ const ProjectPermission = () => {
<option value="">Loading...</option> <option value="">Loading...</option>
) : ( ) : (
<> <>
<option value="">-- Select --</option> <option value="">-- Select Employee --</option>
{employees.map((emp) => ( {[...employees]?.sort((a, b) =>
`${a.firstName} ${a.firstName}`?.localeCompare(
`${b.firstName} ${b.lastName}`
)
)
?.map((emp) => (
<option key={emp.id} value={emp.id}> <option key={emp.id} value={emp.id}>
{emp.firstName} {emp.lastName} {emp.firstName} {emp.lastName}
</option> </option>
@ -129,13 +129,17 @@ const ProjectPermission = () => {
</> </>
)} )}
</select> </select>
{errors.employeeId && ( {errors.employeeId && (
<div className="text-danger small"> <div className="text-danger small">
{errors.employeeId.message} {errors.employeeId.message}
</div> </div>
)} )}
</div> </div>
<button className="btn btn-sm btn-primary" disabled={isPending || loading}> <button
className="btn btn-sm btn-primary"
disabled={isPending || loading}
>
{isPending ? "Please Wait..." : "Update Permission"} {isPending ? "Please Wait..." : "Update Permission"}
</button> </button>
</div> </div>
@ -149,10 +153,7 @@ const ProjectPermission = () => {
<div className="col-12"> <div className="col-12">
<div className="row"> <div className="row">
{feature.featurePermissions?.map((perm) => ( {feature.featurePermissions?.map((perm) => (
<div <div className="col-12 col-sm-6 col-md-4 mb-2" key={perm.id}>
className="col-12 col-sm-6 col-md-4 mb-2"
key={perm.id}
>
<label <label
className="form-check-label d-flex align-items-center" className="form-check-label d-flex align-items-center"
htmlFor={perm.id} htmlFor={perm.id}

View File

@ -32,8 +32,8 @@ const ProjectSetting = () => {
return ( return (
<div className="w-100"> <div className="w-100">
<div className="card py-2 px-5"> <div className="card py-2 px-5">
<div className="col-4"> <div className="col-12">
<div className="dropdown text-start"> <div className="dropdown text-end">
<button <button
className="btn btn-sm btn-outline-primary dropdown-toggle" className="btn btn-sm btn-outline-primary dropdown-toggle"
type="button" type="button"