1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 07:49:41 +02:00

fix(edge-stacks): various custom template issues [BE-11414] (#189)

This commit is contained in:
Ali 2024-12-09 17:48:34 +13:00 committed by GitHub
parent 16a1825990
commit 97e7a3c5e2
24 changed files with 749 additions and 374 deletions

View file

@ -1,43 +1,29 @@
import { useRouter } from '@uirouter/react';
import { useParamState } from '@/react/hooks/useParamState';
import { useParamsState } from '@/react/hooks/useParamState';
export function useTemplateParams() {
const router = useRouter();
const [id] = useParamState('templateId', (param) => {
if (!param) {
return undefined;
}
const [{ id, type }, setTemplateParams] = useParamsState(
['templateId', 'templateType'],
(params) => ({
id: parseTemplateId(params.templateId),
type: parseTemplateType(params.templateType),
})
);
const templateId = parseInt(param, 10);
if (Number.isNaN(templateId)) {
return undefined;
}
return templateId;
});
const [type] = useParamState('templateType', (param) => {
if (param === 'app' || param === 'custom') {
return param;
}
return undefined;
});
return [{ id, type }, handleChange] as const;
function handleChange({
id,
type,
}: {
id: number | undefined;
type: 'app' | 'custom' | undefined;
}) {
router.stateService.go(
'.',
{ templateId: id, templateType: type },
{ reload: false }
);
}
return [{ id, type }, setTemplateParams] as const;
}
function parseTemplateId(param?: string) {
if (!param) {
return undefined;
}
return parseInt(param, 10);
}
function parseTemplateType(param?: string): 'app' | 'custom' | undefined {
if (param === 'app' || param === 'custom') {
return param;
}
return undefined;
}