mirror of https://github.com/portainer/portainer
fix(edge/updates): validate amount of environments [EE-5053] (#9014)
parent
4c8af378af
commit
ea2f752a4f
|
@ -5,6 +5,7 @@ import { useRouter } from '@uirouter/react';
|
||||||
|
|
||||||
import { notifySuccess } from '@/portainer/services/notifications';
|
import { notifySuccess } from '@/portainer/services/notifications';
|
||||||
import { withLimitToBE } from '@/react/hooks/useLimitToBE';
|
import { withLimitToBE } from '@/react/hooks/useLimitToBE';
|
||||||
|
import { useEdgeGroups } from '@/react/edge/edge-groups/queries/useEdgeGroups';
|
||||||
|
|
||||||
import { PageHeader } from '@@/PageHeader';
|
import { PageHeader } from '@@/PageHeader';
|
||||||
import { Widget } from '@@/Widget';
|
import { Widget } from '@@/Widget';
|
||||||
|
@ -36,7 +37,7 @@ function CreateView() {
|
||||||
}),
|
}),
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
const edgeGroupsQuery = useEdgeGroups();
|
||||||
const schedulesQuery = useList();
|
const schedulesQuery = useList();
|
||||||
|
|
||||||
const createMutation = useCreateMutation();
|
const createMutation = useCreateMutation();
|
||||||
|
@ -75,7 +76,9 @@ function CreateView() {
|
||||||
initialValues={initialValues}
|
initialValues={initialValues}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
validateOnMount
|
validateOnMount
|
||||||
validationSchema={() => validation(schedules)}
|
validationSchema={() =>
|
||||||
|
validation(schedules, edgeGroupsQuery.data)
|
||||||
|
}
|
||||||
>
|
>
|
||||||
{({ isValid, setFieldValue, values, handleBlur, errors }) => (
|
{({ isValid, setFieldValue, values, handleBlur, errors }) => (
|
||||||
<FormikForm className="form-horizontal">
|
<FormikForm className="form-horizontal">
|
||||||
|
|
|
@ -5,6 +5,8 @@ import { object, SchemaOf } from 'yup';
|
||||||
|
|
||||||
import { notifySuccess } from '@/portainer/services/notifications';
|
import { notifySuccess } from '@/portainer/services/notifications';
|
||||||
import { withLimitToBE } from '@/react/hooks/useLimitToBE';
|
import { withLimitToBE } from '@/react/hooks/useLimitToBE';
|
||||||
|
import { useEdgeGroups } from '@/react/edge/edge-groups/queries/useEdgeGroups';
|
||||||
|
import { EdgeGroup } from '@/react/edge/edge-groups/types';
|
||||||
|
|
||||||
import { PageHeader } from '@@/PageHeader';
|
import { PageHeader } from '@@/PageHeader';
|
||||||
import { Widget } from '@@/Widget';
|
import { Widget } from '@@/Widget';
|
||||||
|
@ -32,6 +34,7 @@ function ItemView() {
|
||||||
} = useCurrentStateAndParams();
|
} = useCurrentStateAndParams();
|
||||||
|
|
||||||
const id = parseInt(idParam, 10);
|
const id = parseInt(idParam, 10);
|
||||||
|
const edgeGroupsQuery = useEdgeGroups();
|
||||||
|
|
||||||
if (!idParam || Number.isNaN(id)) {
|
if (!idParam || Number.isNaN(id)) {
|
||||||
throw new Error('id is a required path param');
|
throw new Error('id is a required path param');
|
||||||
|
@ -113,7 +116,12 @@ function ItemView() {
|
||||||
}}
|
}}
|
||||||
validateOnMount
|
validateOnMount
|
||||||
validationSchema={() =>
|
validationSchema={() =>
|
||||||
updateValidation(item.id, schedules, isScheduleActive)
|
updateValidation(
|
||||||
|
item.id,
|
||||||
|
schedules,
|
||||||
|
edgeGroupsQuery.data,
|
||||||
|
isScheduleActive
|
||||||
|
)
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{({ isValid, setFieldValue, values, handleBlur, errors }) => (
|
{({ isValid, setFieldValue, values, handleBlur, errors }) => (
|
||||||
|
@ -171,9 +179,10 @@ function ItemView() {
|
||||||
function updateValidation(
|
function updateValidation(
|
||||||
itemId: EdgeUpdateSchedule['id'],
|
itemId: EdgeUpdateSchedule['id'],
|
||||||
schedules: EdgeUpdateSchedule[],
|
schedules: EdgeUpdateSchedule[],
|
||||||
|
edgeGroups: Array<EdgeGroup> | undefined,
|
||||||
isScheduleActive: boolean
|
isScheduleActive: boolean
|
||||||
): SchemaOf<{ name: string } | FormValues> {
|
): SchemaOf<{ name: string } | FormValues> {
|
||||||
return !isScheduleActive
|
return !isScheduleActive
|
||||||
? validation(schedules, itemId)
|
? validation(schedules, edgeGroups, itemId)
|
||||||
: object({ name: nameValidation(schedules, itemId) });
|
: object({ name: nameValidation(schedules, itemId) });
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { array, object, SchemaOf, string } from 'yup';
|
import { array, object, SchemaOf, string, number } from 'yup';
|
||||||
|
|
||||||
import { parseIsoDate } from '@/portainer/filters/filters';
|
import { parseIsoDate } from '@/portainer/filters/filters';
|
||||||
|
import { EdgeGroup } from '@/react/edge/edge-groups/types';
|
||||||
|
|
||||||
import { EdgeUpdateSchedule, ScheduleType } from '../types';
|
import { EdgeUpdateSchedule, ScheduleType } from '../types';
|
||||||
|
|
||||||
|
@ -10,10 +11,24 @@ import { FormValues } from './types';
|
||||||
|
|
||||||
export function validation(
|
export function validation(
|
||||||
schedules: EdgeUpdateSchedule[],
|
schedules: EdgeUpdateSchedule[],
|
||||||
|
edgeGroups: Array<EdgeGroup> | undefined,
|
||||||
currentId?: EdgeUpdateSchedule['id']
|
currentId?: EdgeUpdateSchedule['id']
|
||||||
): SchemaOf<FormValues> {
|
): SchemaOf<FormValues> {
|
||||||
return object({
|
return object({
|
||||||
groupIds: array().min(1, 'At least one group is required'),
|
groupIds: array()
|
||||||
|
.of(number().default(0))
|
||||||
|
.min(1, 'At least one group is required')
|
||||||
|
.test(
|
||||||
|
'At least one group must have endpoints',
|
||||||
|
(groupIds) =>
|
||||||
|
!!(
|
||||||
|
groupIds &&
|
||||||
|
edgeGroups &&
|
||||||
|
groupIds?.flatMap(
|
||||||
|
(id) => edgeGroups?.find((group) => group.Id === id)?.Endpoints
|
||||||
|
).length > 0
|
||||||
|
)
|
||||||
|
),
|
||||||
name: nameValidation(schedules, currentId),
|
name: nameValidation(schedules, currentId),
|
||||||
type: typeValidation(),
|
type: typeValidation(),
|
||||||
scheduledTime: string()
|
scheduledTime: string()
|
||||||
|
@ -28,5 +43,6 @@ export function validation(
|
||||||
// rollback
|
// rollback
|
||||||
otherwise: (schema) => schema.required('No rollback options available'),
|
otherwise: (schema) => schema.required('No rollback options available'),
|
||||||
}),
|
}),
|
||||||
|
registryId: number().default(0),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue