import { Form, Formik, useFormikContext } from 'formik';
import { useRouter } from '@uirouter/react';
import { EdgeGroupsSelector } from '@/react/edge/edge-stacks/components/EdgeGroupsSelector';
import { AssociatedEdgeEnvironmentsSelector } from '@/react/edge/components/AssociatedEdgeEnvironmentsSelector';
import { notifySuccess } from '@/portainer/services/notifications';
import { FormActions } from '@@/form-components/FormActions';
import { FormSection } from '@@/form-components/FormSection';
import { BoxSelector } from '@@/BoxSelector';
import { editor, upload } from '@@/BoxSelector/common-options/build-methods';
import { WebEditorForm } from '@@/WebEditorForm';
import { FileUploadForm } from '@@/form-components/FileUpload';
import { NameField } from '../components/EdgeJobForm/NameField';
import { JobConfigurationFieldset } from '../components/EdgeJobForm/JobConfigurationFieldset';
import {
BasePayload,
CreateEdgeJobPayload,
useCreateEdgeJobMutation,
} from '../queries/useCreateEdgeJobMutation/useCreateEdgeJobMutation';
import {
toRecurringRequest,
toRecurringViewModel,
} from '../components/EdgeJobForm/parseRecurringValues';
import { FormValues } from './types';
import { useValidation } from './useValidation';
export function CreateEdgeJobForm() {
const mutation = useCreateEdgeJobMutation();
const validation = useValidation();
const router = useRouter();
return (
validationSchema={validation}
validateOnMount
initialValues={{
name: '',
method: 'editor',
edgeGroupIds: [],
environmentIds: [],
file: undefined,
fileContent: '',
...toRecurringViewModel(),
}}
onSubmit={(values) => {
mutation.mutate(getPayload(values.method, values), {
onSuccess: () => {
notifySuccess('Success', 'Edge job successfully created');
router.stateService.go('^');
},
});
}}
>
);
}
const buildMethods = [editor, upload];
function InnerForm({ isLoading }: { isLoading: boolean }) {
const { values, setFieldValue, isValid, errors } =
useFormikContext();
return (
);
}
function getPayload(
method: 'upload' | 'editor',
values: FormValues
): CreateEdgeJobPayload {
switch (method) {
case 'upload':
if (!values.file) {
throw new Error('File is required');
}
return {
method: 'file',
payload: {
...getBasePayload(values),
file: values.file,
},
};
case 'editor':
return {
method: 'string',
payload: {
...getBasePayload(values),
fileContent: values.fileContent,
},
};
default:
throw new Error(`Unknown method: ${method}`);
}
function getBasePayload(values: FormValues): BasePayload {
return {
name: values.name,
edgeGroups: values.edgeGroupIds,
endpoints: values.environmentIds,
...toRecurringRequest(values),
};
}
}