F #8208: fix form groups (#4643)

(cherry picked from commit d53a967a019e4e93ed23b11d414f35ae4f437407)
This commit is contained in:
Jorge Miguel Lobo Escalona authored and Ruben S. Montero committed 2026-11-24 14:23:11 +01:00
1 parent d05ea2a2b9
commit 60554b118b
2 files changed
+92 -76

No files matched your search

@@ -22,6 +22,7 @@ import {
AclAPI,
SystemAPI,
GroupAPI,
useAuth,
useGeneralApi,
} from '@FeaturesModule'
@@ -51,6 +52,7 @@ export function CreateGroup() {
const history = useHistory()
const { state: { ID: groupId } = {} } = useLocation()
const { isOneAdmin } = useAuth()
const { enqueueSuccess, enqueueError } = useGeneralApi()
const [createGroup] = GroupAPI.useAllocateGroupMutation()
const [updateGroup] = GroupAPI.useUpdateGroupMutation()
@@ -69,7 +71,7 @@ export function CreateGroup() {
data: acls,
isError: isAclsError,
error: aclsError,
} = AclAPI.useGetAclsQuery(undefined, { skip: !groupId })
} = AclAPI.useGetAclsQuery(undefined, { skip: !groupId || !isOneAdmin })
const updateTemplate = async (props, id) => {
// Create group template with advanced options
@@ -171,11 +173,13 @@ export function CreateGroup() {
enqueueSuccess(T.SuccessGroupCreated, newGroupId)
} else {
// Update case. Only update template
const updates = [updateTemplate(props, groupId)]
await Promise.all([
updateTemplate(props, groupId),
updatePermissions(props, groupId),
])
if (isOneAdmin) {
updates.push(updatePermissions(props, groupId))
}
await Promise.all(updates)
// Only show group message
enqueueSuccess(T.SuccessGroupUpdated, groupId)
@@ -188,6 +192,9 @@ export function CreateGroup() {
}
}
const isGroupUpdateReady = group && (!isOneAdmin || (acls && !isAclsError))
const hasAclError = isOneAdmin && isAclsError
return (
<>
{views && version ? (
@@ -202,19 +209,20 @@ export function CreateGroup() {
>
{(config) => <DefaultFormStepper {...config} />}
</Group.Forms.CreateForm>
) : group && acls && !isAclsError ? (
) : isGroupUpdateReady ? (
<Group.Forms.UpdateForm
initialValues={{ group, acls }}
onSubmit={onSubmit}
stepProps={{
views,
version,
isOneAdmin,
}}
fallback={<SkeletonStepsForm />}
>
{(config) => <DefaultFormStepper {...config} update />}
</Group.Forms.UpdateForm>
) : isAclsError ? (
) : hasAclError ? (
<QueryState error={aclsError?.data} />
) : (
<SkeletonStepsForm />
@@ -32,87 +32,95 @@ import { ACL_RESOURCES } from '@ConstantsModule'
/**
* Create steps for Groups Update Form:
* 1. Permissions: Set permissions about some resources for the group
* 1. Permissions: Set permissions about some resources for the group (oneadmin only)
* 2. Advanced options: Options that will be set on group template
* 2.1. Views: Views of the group
* 2.2. Default options
*/
const Steps = createSteps([Permissions, Views, System], {
transformInitialValue: (initialValues, schema) => {
const group = initialValues?.group ?? initialValues
const acls = initialValues?.acls ?? []
const objectSchema = {
[PERMISSIONS_ID]: getGroupPermissionFormValues(acls, group?.ID),
[VIEWS_ID]: {
VIEWS: group?.TEMPLATE?.FIREEDGE?.VIEWS?.split(',').reduce(
(acc, view) => ({ ...acc, [view]: true }),
{}
),
DEFAULT_VIEW: group?.TEMPLATE?.FIREEDGE?.DEFAULT_VIEW,
GROUP_ADMIN_VIEWS: group?.TEMPLATE?.FIREEDGE?.GROUP_ADMIN_VIEWS?.split(
','
).reduce((acc, view) => ({ ...acc, [view]: true }), {}),
GROUP_ADMIN_DEFAULT_VIEW:
group?.TEMPLATE?.FIREEDGE?.GROUP_ADMIN_DEFAULT_VIEW,
},
[SYSTEM_ID]: {
OPENNEBULA: group?.TEMPLATE?.OPENNEBULA,
},
}
const Steps = createSteps(
({ isOneAdmin }) =>
[isOneAdmin && Permissions, Views, System].filter(Boolean),
{
transformInitialValue: (initialValues, schema) => {
const group = initialValues?.group ?? initialValues
const acls = initialValues?.acls ?? []
const objectSchema = {
[PERMISSIONS_ID]: getGroupPermissionFormValues(acls, group?.ID),
[VIEWS_ID]: {
VIEWS: group?.TEMPLATE?.FIREEDGE?.VIEWS?.split(',').reduce(
(acc, view) => ({ ...acc, [view]: true }),
{}
),
DEFAULT_VIEW: group?.TEMPLATE?.FIREEDGE?.DEFAULT_VIEW,
GROUP_ADMIN_VIEWS:
group?.TEMPLATE?.FIREEDGE?.GROUP_ADMIN_VIEWS?.split(',').reduce(
(acc, view) => ({ ...acc, [view]: true }),
{}
),
GROUP_ADMIN_DEFAULT_VIEW:
group?.TEMPLATE?.FIREEDGE?.GROUP_ADMIN_DEFAULT_VIEW,
},
[SYSTEM_ID]: {
OPENNEBULA: group?.TEMPLATE?.OPENNEBULA,
},
}
const knownGroup = schema.cast(objectSchema, {
stripUnknown: true,
})
const knownGroup = schema.cast(objectSchema, {
stripUnknown: true,
})
return knownGroup
},
return knownGroup
},
transformBeforeSubmit: (formData) => {
// Get data from steps
const { [PERMISSIONS_ID]: permissionsData } = formData
const { [VIEWS_ID]: views } = formData
const { [SYSTEM_ID]: system } = formData
transformBeforeSubmit: (formData) => {
// Get data from steps
const { [PERMISSIONS_ID]: permissionsData } = formData
const { [VIEWS_ID]: views } = formData
const { [SYSTEM_ID]: system } = formData
const response = {}
const response = {}
// Permissions
const createResources = Object.entries(permissionsData?.create ?? {})
.filter((resource) => resource[1])
.map((resource) => ACL_RESOURCES[resource[0]])
.filter(Boolean)
const viewResources = Object.entries(permissionsData?.view ?? {})
.filter((resource) => resource[1])
.map((resource) => ACL_RESOURCES[resource[0]])
.filter(Boolean)
response.permissions = {
view: viewResources,
create: createResources,
}
// Views
response.views = {
FIREEDGE: {
VIEWS: Object.entries(views?.VIEWS)
if (permissionsData) {
// Permissions
const createResources = Object.entries(permissionsData.create ?? {})
.filter((resource) => resource[1])
.map((resource) => resource[0])
.join(','),
DEFAULT_VIEW: views?.DEFAULT_VIEW,
GROUP_ADMIN_VIEWS: Object.entries(views?.GROUP_ADMIN_VIEWS)
.map((resource) => ACL_RESOURCES[resource[0]])
.filter(Boolean)
const viewResources = Object.entries(permissionsData.view ?? {})
.filter((resource) => resource[1])
.map((resource) => resource[0])
.join(','),
GROUP_ADMIN_DEFAULT_VIEW: views?.GROUP_ADMIN_DEFAULT_VIEW,
},
}
.map((resource) => ACL_RESOURCES[resource[0]])
.filter(Boolean)
// System
response.system = {
OPENNEBULA: system?.OPENNEBULA,
}
response.permissions = {
view: viewResources,
create: createResources,
}
}
return response
},
})
// Views
response.views = {
FIREEDGE: {
VIEWS: Object.entries(views?.VIEWS)
.filter((resource) => resource[1])
.map((resource) => resource[0])
.join(','),
DEFAULT_VIEW: views?.DEFAULT_VIEW,
GROUP_ADMIN_VIEWS: Object.entries(views?.GROUP_ADMIN_VIEWS)
.filter((resource) => resource[1])
.map((resource) => resource[0])
.join(','),
GROUP_ADMIN_DEFAULT_VIEW: views?.GROUP_ADMIN_DEFAULT_VIEW,
},
}
// System
response.system = {
OPENNEBULA: system?.OPENNEBULA,
}
return response
},
}
)
export default Steps