mirror of
https://github.com/portainer/portainer.git
synced 2025-08-02 12:25:22 +02:00
fix(edge/stacks): load template [EE-7109] (#11848)
Some checks are pending
ci / build_images (map[arch:amd64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
ci / build_images (map[arch:arm platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:arm64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:ppc64le platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:s390x platform:linux version:]) (push) Waiting to run
ci / build_manifests (push) Blocked by required conditions
/ triage (push) Waiting to run
Lint / Run linters (push) Waiting to run
Test / test-client (push) Waiting to run
Test / test-server (map[arch:amd64 platform:linux]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
Test / test-server (map[arch:arm64 platform:linux]) (push) Waiting to run
Some checks are pending
ci / build_images (map[arch:amd64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
ci / build_images (map[arch:arm platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:arm64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:ppc64le platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:s390x platform:linux version:]) (push) Waiting to run
ci / build_manifests (push) Blocked by required conditions
/ triage (push) Waiting to run
Lint / Run linters (push) Waiting to run
Test / test-client (push) Waiting to run
Test / test-server (map[arch:amd64 platform:linux]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
Test / test-server (map[arch:arm64 platform:linux]) (push) Waiting to run
This commit is contained in:
parent
a28bd349ae
commit
0f5988af49
8 changed files with 126 additions and 60 deletions
|
@ -24,7 +24,7 @@ export function TemplateSelector({
|
|||
) => void;
|
||||
error?: string;
|
||||
}) {
|
||||
const { options, getTemplate } = useOptions();
|
||||
const { options, getTemplate, selectedValue } = useOptions(value);
|
||||
|
||||
return (
|
||||
<FormControl label="Template" inputId="template_selector" errors={error}>
|
||||
|
@ -32,10 +32,8 @@ export function TemplateSelector({
|
|||
inputId="template_selector"
|
||||
formatGroupLabel={GroupLabel}
|
||||
placeholder="Select an Edge stack template"
|
||||
value={{
|
||||
templateId: value.templateId,
|
||||
type: value.type,
|
||||
}}
|
||||
options={options}
|
||||
value={selectedValue}
|
||||
onChange={(value) => {
|
||||
if (!value) {
|
||||
onChange(undefined, undefined);
|
||||
|
@ -48,14 +46,19 @@ export function TemplateSelector({
|
|||
}
|
||||
onChange(getTemplate({ type, id: templateId }), type);
|
||||
}}
|
||||
options={options}
|
||||
data-cy="edge-stacks-create-template-selector"
|
||||
/>
|
||||
</FormControl>
|
||||
);
|
||||
}
|
||||
|
||||
function useOptions() {
|
||||
interface Option {
|
||||
label: string;
|
||||
templateId?: number;
|
||||
type: 'app' | 'custom';
|
||||
}
|
||||
|
||||
function useOptions(value: SelectedTemplateValue) {
|
||||
const customTemplatesQuery = useCustomTemplates({
|
||||
params: {
|
||||
edge: true,
|
||||
|
@ -71,43 +74,75 @@ function useOptions() {
|
|||
),
|
||||
});
|
||||
|
||||
const appTemplateOptions: Array<Option> = useMemo(
|
||||
() =>
|
||||
appTemplatesQuery.data?.map(
|
||||
(template) =>
|
||||
({
|
||||
label: `${template.Title} - ${template.Description}`,
|
||||
|
||||
templateId: template.Id,
|
||||
type: 'app',
|
||||
}) satisfies Option
|
||||
) || [],
|
||||
[appTemplatesQuery.data]
|
||||
);
|
||||
|
||||
const customTemplateOptions: Array<Option> = useMemo(
|
||||
() =>
|
||||
customTemplatesQuery.data && customTemplatesQuery.data.length > 0
|
||||
? customTemplatesQuery.data.map(
|
||||
(template) =>
|
||||
({
|
||||
label: `${template.Title} - ${template.Description}`,
|
||||
|
||||
templateId: template.Id,
|
||||
type: 'custom' as 'app' | 'custom',
|
||||
}) satisfies Option
|
||||
)
|
||||
: [
|
||||
{
|
||||
label: 'No edge custom templates available',
|
||||
|
||||
templateId: undefined,
|
||||
type: 'custom' as 'app' | 'custom',
|
||||
} satisfies Option,
|
||||
],
|
||||
[customTemplatesQuery.data]
|
||||
);
|
||||
|
||||
const options = useMemo(
|
||||
() =>
|
||||
[
|
||||
{
|
||||
label: 'Edge App Templates',
|
||||
options:
|
||||
appTemplatesQuery.data?.map((template) => ({
|
||||
label: `${template.Title} - ${template.Description}`,
|
||||
|
||||
templateId: template.Id,
|
||||
type: 'app' as 'app' | 'custom',
|
||||
})) || [],
|
||||
options: appTemplateOptions,
|
||||
},
|
||||
{
|
||||
label: 'Edge Custom Templates',
|
||||
options:
|
||||
customTemplatesQuery.data && customTemplatesQuery.data.length > 0
|
||||
? customTemplatesQuery.data.map((template) => ({
|
||||
label: `${template.Title} - ${template.Description}`,
|
||||
|
||||
templateId: template.Id,
|
||||
type: 'custom' as 'app' | 'custom',
|
||||
}))
|
||||
: [
|
||||
{
|
||||
label: 'No edge custom templates available',
|
||||
|
||||
templateId: undefined,
|
||||
type: undefined,
|
||||
},
|
||||
],
|
||||
options: customTemplateOptions,
|
||||
},
|
||||
] as const,
|
||||
[appTemplatesQuery.data, customTemplatesQuery.data]
|
||||
[appTemplateOptions, customTemplateOptions]
|
||||
);
|
||||
|
||||
return { options, getTemplate };
|
||||
const selectedValue: Option | undefined = useMemo(() => {
|
||||
if (!value.templateId) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (value.type === 'app') {
|
||||
return appTemplateOptions.find(
|
||||
(template) => template.templateId === value.templateId
|
||||
);
|
||||
}
|
||||
|
||||
return customTemplateOptions.find(
|
||||
(template) => template.templateId === value.templateId
|
||||
);
|
||||
}, [value.templateId, value.type, customTemplateOptions, appTemplateOptions]);
|
||||
|
||||
return { options, getTemplate, selectedValue };
|
||||
|
||||
function getTemplate({ type, id }: { type: 'app' | 'custom'; id: number }) {
|
||||
if (type === 'app') {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue