1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-08 23:35:31 +02:00

feat(custom-templates): filter templates by edge [EE-6565] (#10979)

This commit is contained in:
Chaim Lev-Ari 2024-01-28 15:54:34 +02:00 committed by GitHub
parent 441a8bbbbf
commit 2826a4ce39
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 229 additions and 21 deletions

View file

@ -27,7 +27,11 @@ export function useValidation({
}) {
const { user } = useCurrentUser();
const gitCredentialsQuery = useGitCredentials(user.Id);
const customTemplatesQuery = useCustomTemplates();
const customTemplatesQuery = useCustomTemplates({
params: {
edge: undefined,
},
});
return useMemo(
() =>

View file

@ -25,7 +25,11 @@ export function useValidation({
}) {
const { user } = useCurrentUser();
const gitCredentialsQuery = useGitCredentials(user.Id);
const customTemplatesQuery = useCustomTemplates();
const customTemplatesQuery = useCustomTemplates({
params: {
edge: undefined,
},
});
return useMemo(
() =>

View file

@ -2,6 +2,7 @@ import { CustomTemplate } from '../types';
export const queryKeys = {
base: () => ['custom-templates'] as const,
list: (params: unknown) => [...queryKeys.base(), { params }] as const,
item: (id: CustomTemplate['Id']) => [...queryKeys.base(), id] as const,
file: (id: CustomTemplate['Id'], options: { git: boolean }) =>
[...queryKeys.item(id), 'file', options] as const,

View file

@ -2,26 +2,45 @@ import { useQuery } from 'react-query';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { withGlobalError } from '@/react-tools/react-query';
import { StackType } from '@/react/common/stacks/types';
import { CustomTemplate } from '../types';
import { queryKeys } from './query-keys';
import { buildUrl } from './build-url';
export async function getCustomTemplates() {
type Params = {
type?: StackType[];
/**
* filter edge templates
* true if should show only edge templates
* false if should show only non-edge templates
* undefined if should show all templates
*/
edge?: boolean;
};
export function useCustomTemplates<T = Array<CustomTemplate>>({
select,
params,
}: { params?: Params; select?(templates: Array<CustomTemplate>): T } = {}) {
return useQuery(queryKeys.list(params), () => getCustomTemplates(params), {
select,
...withGlobalError('Unable to retrieve custom templates'),
});
}
async function getCustomTemplates({ type, edge = false }: Params = {}) {
try {
const { data } = await axios.get<CustomTemplate[]>(buildUrl());
const { data } = await axios.get<CustomTemplate[]>(buildUrl(), {
params: {
// deconstruct to make sure we don't pass other params
type,
edge,
},
});
return data;
} catch (e) {
throw parseAxiosError(e, 'Unable to get custom templates');
}
}
export function useCustomTemplates<T = Array<CustomTemplate>>({
select,
}: { select?(templates: Array<CustomTemplate>): T } = {}) {
return useQuery(queryKeys.base(), () => getCustomTemplates(), {
select,
...withGlobalError('Unable to retrieve custom templates'),
});
}