1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-04 13:25:26 +02:00

refactor(containers): migrate create view to react [EE-2307] (#9175)

This commit is contained in:
Chaim Lev-Ari 2023-10-19 13:45:50 +02:00 committed by GitHub
parent bc0050a7b4
commit d970f0e2bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
71 changed files with 2612 additions and 1399 deletions

View file

@ -0,0 +1,11 @@
import { Webhook } from './types';
export function buildUrl(id?: Webhook['Id']) {
const url = '/webhooks';
if (id) {
return `${url}/${id}`;
}
return url;
}

View file

@ -0,0 +1,23 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '../environments/types';
import { RegistryId } from '../registries/types/registry';
import { buildUrl } from './build-url';
import { Webhook, WebhookType } from './types';
interface CreateWebhookPayload {
resourceId: string;
environmentId: EnvironmentId;
registryId?: RegistryId;
webhookType: WebhookType;
}
export async function createWebhook(payload: CreateWebhookPayload) {
try {
const { data } = await axios.post<Webhook>(buildUrl(), payload);
return data;
} catch (error) {
throw parseAxiosError(error, 'Unable to create webhook');
}
}

View file

@ -0,0 +1,6 @@
import { Filters } from './types';
export const queryKeys = {
base: () => ['webhooks'] as const,
list: (filters: Filters) => [...queryKeys.base(), { filters }],
};

View file

@ -0,0 +1,21 @@
import { EnvironmentId } from '../environments/types';
import { RegistryId } from '../registries/types/registry';
export enum WebhookType {
DockerService = 1,
DockerContainer = 2,
}
export interface Webhook {
Id: number;
Token: string;
ResourceId: string;
EndpointId: EnvironmentId;
RegistryId: RegistryId;
Type: WebhookType;
}
export interface Filters {
endpointId: EnvironmentId;
resourceId?: string;
}

View file

@ -0,0 +1,27 @@
import { useQuery } from 'react-query';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { buildUrl } from './build-url';
import { queryKeys } from './query-keys';
import { Filters, Webhook } from './types';
export function useWebhooks(
filters: Filters,
{ enabled }: { enabled?: boolean } = {}
) {
return useQuery(queryKeys.list(filters), () => getWebhooks(filters), {
enabled,
});
}
async function getWebhooks(filters: Filters) {
try {
const { data } = await axios.get<Array<Webhook>>(buildUrl(), {
params: { filters },
});
return data;
} catch (err) {
throw parseAxiosError(err, 'failed fetching webhooks');
}
}