From 7d546f6a8d9e22a21e2e9d59c12f698c23b62358 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Mon, 22 Jul 2024 22:07:30 +0530 Subject: [PATCH] Added validation to prevent making group inactive if the group has active roles (#9697) * Added validation to prevent making group inactive if the group has active roles * Review chnages --- app/models/user_group.rb | 2 ++ .../Panel/pages/RegionManager/index.jsx | 22 +++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/models/user_group.rb b/app/models/user_group.rb index e8804b592c..e3f1599cb5 100644 --- a/app/models/user_group.rb +++ b/app/models/user_group.rb @@ -35,6 +35,8 @@ class UserGroup < ApplicationRecord scope :root_groups, -> { where(parent_group: nil) } scope :active_groups, -> { where(is_active: true) } + validates :active_roles, absence: true, unless: :is_active? + def all_child_groups [direct_child_groups, direct_child_groups.map(&:all_child_groups)].flatten end diff --git a/app/webpacker/components/Panel/pages/RegionManager/index.jsx b/app/webpacker/components/Panel/pages/RegionManager/index.jsx index 169d234f5c..b8667ff40e 100644 --- a/app/webpacker/components/Panel/pages/RegionManager/index.jsx +++ b/app/webpacker/components/Panel/pages/RegionManager/index.jsx @@ -22,7 +22,9 @@ const defaultRegion = { friendlyId: '', }; -function UserGroupVisibility({ userGroup, save, sync }) { +function UserGroupVisibility({ + userGroup, save, sync, setSaveError, +}) { const [open, setOpen] = React.useState(false); const iconName = userGroup.is_active ? 'eye' : 'eye slash'; return ( @@ -42,6 +44,8 @@ function UserGroupVisibility({ userGroup, save, sync }) { userGroupsUpdateUrl(userGroup.id), { is_active: !userGroup.is_active, is_hidden: userGroup.is_hidden }, sync, + {}, + setSaveError, ); }} /> @@ -76,7 +80,7 @@ export default function RegionManager() { }, [data]); if (loading || fetchLoading || saving) return ; - if (error || saveError) return ; + if (error || saveError) return ; return ( <> @@ -123,7 +127,12 @@ export default function RegionManager() { - + {subRegions[region.id]?.map((subRegion) => ( @@ -153,7 +162,12 @@ export default function RegionManager() { )} - + ))}