1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 15:59:41 +02:00
portainer/app/react/portainer/templates/custom-templates/queries/useCustomTemplates.ts

46 lines
1.3 KiB
TypeScript

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';
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(), {
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');
}
}