fixed permission add and remove updation
This commit is contained in:
parent
2ef56e7f83
commit
0e3a634205
@ -43,67 +43,62 @@ 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((prev) => ({
|
||||||
|
...prev,
|
||||||
|
selectedPermissions: enabledPerms,
|
||||||
|
}));
|
||||||
|
}, [selectedEmpPermissions, reset, selectedEmployee]);
|
||||||
|
|
||||||
reset({
|
|
||||||
employeeId: selectedEmployee || employees[0]?.id || "",
|
|
||||||
selectedPermissions: enabledPerms,
|
|
||||||
});
|
|
||||||
}, [selectedEmpPermissions, reset, selectedEmployee, employees]);
|
|
||||||
|
|
||||||
const { mutate: updatePermission, isPending } =
|
const { mutate: updatePermission, isPending } =
|
||||||
useUpdateProjectLevelEmployeePermission();
|
useUpdateProjectLevelEmployeePermission();
|
||||||
|
|
||||||
const onSubmit = (formData) => {
|
const onSubmit = (formData) => {
|
||||||
if (!formData.employeeId) {
|
if (!formData.employeeId) {
|
||||||
showToast("Please select an employee", "warn");
|
showToast("Please select an employee", "warn");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) => ({
|
|
||||||
id: perm.id,
|
|
||||||
isEnabled: formData.selectedPermissions?.includes(perm.id) || false,
|
|
||||||
}))
|
|
||||||
: (formData.selectedPermissions || []).map((id) => ({
|
|
||||||
id,
|
|
||||||
isEnabled: true,
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (payloadPermissions.length === 0) {
|
const removed = existingEnabledIds
|
||||||
showToast("No permissions selected", "warn");
|
.filter((id) => !newSelectedIds.includes(id))
|
||||||
return;
|
.map((id) => ({ id, isEnabled: false }));
|
||||||
}
|
|
||||||
|
|
||||||
const hasChanges = existingPermissions.some(
|
const added = newSelectedIds
|
||||||
(perm) =>
|
.filter((id) => !existingEnabledIds.includes(id))
|
||||||
perm.isEnabled !==
|
.map((id) => ({ id, isEnabled: true }));
|
||||||
(formData.selectedPermissions?.includes(perm.id) || false)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!hasChanges && existingPermissions.length > 0) {
|
const payloadPermissions = [...removed, ...added];
|
||||||
showToast("No changes detected", "info");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const payload = {
|
if (payloadPermissions.length === 0) {
|
||||||
employeeId: formData.employeeId,
|
showToast("No changes detected", "info");
|
||||||
projectId: selectedProject,
|
return;
|
||||||
permission: payloadPermissions,
|
}
|
||||||
};
|
|
||||||
|
|
||||||
updatePermission(payload);
|
const payload = {
|
||||||
|
employeeId: formData.employeeId,
|
||||||
|
projectId: selectedProject,
|
||||||
|
permission: payloadPermissions,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
updatePermission(payload);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="row px-2 py-1">
|
<div className="row px-2 py-1">
|
||||||
<form className="row" onSubmit={handleSubmit(onSubmit)}>
|
<form className="row" onSubmit={handleSubmit(onSubmit)}>
|
||||||
@ -120,22 +115,31 @@ 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) =>
|
||||||
<option key={emp.id} value={emp.id}>
|
`${a.firstName} ${a.firstName}`?.localeCompare(
|
||||||
{emp.firstName} {emp.lastName}
|
`${b.firstName} ${b.lastName}`
|
||||||
</option>
|
)
|
||||||
))}
|
)
|
||||||
|
?.map((emp) => (
|
||||||
|
<option key={emp.id} value={emp.id}>
|
||||||
|
{emp.firstName} {emp.lastName}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</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}
|
||||||
|
@ -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"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user