From 85e55a4273623f43dc43a5f9beb777e1d2f5dff1 Mon Sep 17 00:00:00 2001
From: Ana Garcia <ana@eyeseetea.com>
Date: Thu, 24 Oct 2024 17:08:18 +0200
Subject: [PATCH] Unselected after delete an iteam and do not allow to delete a
 parent in the hierarchy

---
 .../useIMTeamBuilder.ts                             | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/webapp/pages/incident-management-team-builder/useIMTeamBuilder.ts b/src/webapp/pages/incident-management-team-builder/useIMTeamBuilder.ts
index 16b84d1f..6cb8f61f 100644
--- a/src/webapp/pages/incident-management-team-builder/useIMTeamBuilder.ts
+++ b/src/webapp/pages/incident-management-team-builder/useIMTeamBuilder.ts
@@ -107,9 +107,18 @@ export function useIMTeamBuilder(id: Id): State {
             );
 
             const isIncidentManagerRoleSelected = selectedRole?.roleId === INCIDENT_MANAGER_ROLE;
+            const isParentReporting = incidentManagementTeamItemSelected
+                ? !!incidentManagementTeam?.teamHierarchy.find(teamMember =>
+                      teamMember.teamRoles?.some(
+                          teamRole =>
+                              teamRole.reportsToUsername ===
+                              incidentManagementTeamItemSelected?.username
+                      )
+                  )
+                : false;
 
             setSelectedHierarchyItemId(selection);
-            setDisableDeletion(isIncidentManagerRoleSelected);
+            setDisableDeletion(isIncidentManagerRoleSelected || isParentReporting);
         },
         [incidentManagementTeam?.teamHierarchy]
     );
@@ -161,6 +170,7 @@ export function useIMTeamBuilder(id: Id): State {
                         });
                         getIncidentManagementTeam();
                         onOpenDeleteModalData(undefined);
+                        setSelectedHierarchyItemId("");
                     },
                     err => {
                         console.debug(err);
@@ -169,6 +179,7 @@ export function useIMTeamBuilder(id: Id): State {
                             type: "error",
                         });
                         onOpenDeleteModalData(undefined);
+                        setSelectedHierarchyItemId("");
                     }
                 );
         } else {