mirror of
https://github.com/portainer/portainer.git
synced 2025-08-06 22:35:23 +02:00
feat(edge/jobs): migrate create view to react [EE-2221] (#11867)
This commit is contained in:
parent
94c91035a7
commit
02fbdfec36
27 changed files with 777 additions and 163 deletions
|
@ -0,0 +1,39 @@
|
|||
import axios, {
|
||||
json2formData,
|
||||
parseAxiosError,
|
||||
} from '@/portainer/services/axios';
|
||||
import { EdgeGroup } from '@/react/edge/edge-groups/types';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
|
||||
import { EdgeJob } from '../../types';
|
||||
import { buildUrl } from '../build-url';
|
||||
|
||||
/**
|
||||
* Payload to create an EdgeJob from a file
|
||||
*/
|
||||
export type FileUploadPayload = {
|
||||
Name: string;
|
||||
CronExpression: string;
|
||||
Recurring: boolean;
|
||||
|
||||
EdgeGroups: Array<EdgeGroup['Id']>;
|
||||
Endpoints: Array<EnvironmentId>;
|
||||
File: File;
|
||||
};
|
||||
|
||||
export async function createJobFromFile(payload: FileUploadPayload) {
|
||||
try {
|
||||
const { data } = await axios.post<EdgeJob>(
|
||||
buildUrl({ action: 'create/file' }),
|
||||
json2formData(payload),
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
}
|
||||
);
|
||||
return data;
|
||||
} catch (e) {
|
||||
throw parseAxiosError(e as Error);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
import { EdgeGroup } from '@/react/edge/edge-groups/types';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
|
||||
import { EdgeJob } from '../../types';
|
||||
import { buildUrl } from '../build-url';
|
||||
|
||||
/**
|
||||
* Payload for creating an EdgeJob from a string
|
||||
*/
|
||||
export interface FileContentPayload {
|
||||
name: string;
|
||||
cronExpression: string;
|
||||
recurring: boolean;
|
||||
|
||||
edgeGroups: Array<EdgeGroup['Id']>;
|
||||
endpoints: Array<EnvironmentId>;
|
||||
fileContent: string;
|
||||
}
|
||||
|
||||
export async function createJobFromFileContent(payload: FileContentPayload) {
|
||||
try {
|
||||
const { data } = await axios.post<EdgeJob>(
|
||||
buildUrl({ action: 'create/string' }),
|
||||
payload
|
||||
);
|
||||
return data;
|
||||
} catch (e) {
|
||||
throw parseAxiosError(e as Error);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
import { useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { EdgeGroup } from '@/react/edge/edge-groups/types';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
|
||||
import { createJobFromFile } from './createJobFromFile';
|
||||
import { createJobFromFileContent } from './createJobFromFileContent';
|
||||
|
||||
export function useCreateEdgeJobMutation() {
|
||||
return useMutation(createEdgeJob);
|
||||
}
|
||||
|
||||
export type BasePayload = {
|
||||
name: string;
|
||||
cronExpression: string;
|
||||
recurring: boolean;
|
||||
|
||||
edgeGroups: Array<EdgeGroup['Id']>;
|
||||
endpoints: Array<EnvironmentId>;
|
||||
};
|
||||
|
||||
export type CreateEdgeJobPayload =
|
||||
| {
|
||||
method: 'file';
|
||||
payload: BasePayload & {
|
||||
/** File to upload */
|
||||
file: File;
|
||||
};
|
||||
}
|
||||
| {
|
||||
method: 'string';
|
||||
payload: BasePayload & {
|
||||
/** Content of the Job file */
|
||||
fileContent: string;
|
||||
};
|
||||
};
|
||||
|
||||
function createEdgeJob({ method, payload }: CreateEdgeJobPayload) {
|
||||
switch (method) {
|
||||
case 'file':
|
||||
return createJobFromFile({
|
||||
CronExpression: payload.cronExpression,
|
||||
Recurring: payload.recurring,
|
||||
Name: payload.name,
|
||||
EdgeGroups: payload.edgeGroups,
|
||||
Endpoints: payload.endpoints,
|
||||
File: payload.file,
|
||||
});
|
||||
|
||||
case 'string':
|
||||
return createJobFromFileContent({
|
||||
cronExpression: payload.cronExpression,
|
||||
recurring: payload.recurring,
|
||||
name: payload.name,
|
||||
edgeGroups: payload.edgeGroups,
|
||||
endpoints: payload.endpoints,
|
||||
fileContent: payload.fileContent,
|
||||
});
|
||||
default:
|
||||
throw new Error('Invalid method');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue