Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions site/src/pages/TemplateBuilder/TemplateBuilderPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const TemplateBuilderPage: FC = () => {
onCreateTemplate={handleCreate}
createError={createMutation.error}
isCreating={createMutation.isPending}
onClearCreateError={() => createMutation.reset()}
/>
</>
);
Expand Down
26 changes: 24 additions & 2 deletions site/src/pages/TemplateBuilder/TemplateBuilderPageView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { type FC, type ReactNode, useReducer, useState } from "react";
import {
type FC,
type ReactNode,
useCallback,
useReducer,
useState,
} from "react";

import { useQuery } from "react-query";
import { templateBuilderModules } from "#/api/queries/templateBuilder";
Expand Down Expand Up @@ -51,6 +57,7 @@ interface TemplateBuilderPageViewProps {
onCreateTemplate: (state: TemplateBuilderWizardState) => void;
createError: Error | null;
isCreating: boolean;
onClearCreateError?: () => void;
}

export const TemplateBuilderPageView: FC<TemplateBuilderPageViewProps> = ({
Expand All @@ -59,6 +66,7 @@ export const TemplateBuilderPageView: FC<TemplateBuilderPageViewProps> = ({
onCreateTemplate,
createError,
isCreating,
onClearCreateError,
}) => {
const [state, dispatch] = useReducer(wizardReducer, initialWizardState);
const [stepIndex, setStepIndex] = useState(0);
Expand All @@ -85,6 +93,10 @@ export const TemplateBuilderPageView: FC<TemplateBuilderPageViewProps> = ({
);

const handleBack = () => {
if (currentStep.id === "customizations") {
dispatch({ type: "RESET_CUSTOMIZATIONS" });
onClearCreateError?.();
}
window.scrollTo(0, 0);
setStepIndex(prevIndex);
};
Expand All @@ -98,6 +110,13 @@ export const TemplateBuilderPageView: FC<TemplateBuilderPageViewProps> = ({
setStepIndex(nextIndex);
};

const handleProvisionerStatusChange = useCallback(
(value: boolean | undefined) => {
dispatch({ type: "SET_HAS_PROVISIONERS", value });
},
[],
);

const handleDeselectModule = (moduleId: string) => {
// If the only module gets deselected, go back to module selection
if (state.modules.length === 1) {
Expand Down Expand Up @@ -142,6 +161,7 @@ export const TemplateBuilderPageView: FC<TemplateBuilderPageViewProps> = ({
dispatch,
moduleVarMap,
createError,
handleProvisionerStatusChange,
)}
</div>

Expand Down Expand Up @@ -193,6 +213,7 @@ function renderStepContent(
dispatch: (action: WizardAction) => void,
moduleVarMap: Record<string, Record<string, string>>,
createError: Error | null,
onProvisionerStatusChange: (value: boolean | undefined) => void,
): ReactNode {
switch (stepId) {
case "base-infra":
Expand Down Expand Up @@ -253,6 +274,7 @@ function renderStepContent(
value,
})
}
onProvisionerStatusChange={onProvisionerStatusChange}
/>
</>
);
Expand Down Expand Up @@ -284,7 +306,7 @@ function computeCanContinue(
moduleVarMap,
);
case "customizations":
return state.name.trim() !== "";
return state.name.trim() !== "" && state.hasProvisioners !== false;
default:
return true;
}
Expand Down
12 changes: 10 additions & 2 deletions site/src/pages/TemplateBuilder/TemplateCustomizationsStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ interface TemplateCustomizationsStepProps {
field: "organizationId" | "name" | "displayName" | "description" | "icon",
value: string,
) => void;
onProvisionerStatusChange: (hasProvisioners: boolean | undefined) => void;
}

export const TemplateCustomizationsStep: FC<
TemplateCustomizationsStepProps
> = ({ state, onChangeField }) => {
> = ({ state, onChangeField, onProvisionerStatusChange }) => {
const permittedOrgsQuery = useQuery(
permittedOrganizations({
object: { resource_type: "template" },
Expand All @@ -47,7 +48,14 @@ export const TemplateCustomizationsStep: FC<
...provisionerDaemons(selectedOrg?.id ?? ""),
enabled: Boolean(selectedOrg),
});
const showProvisionerWarning = provisioners ? provisioners.length < 1 : false;
const hasProvisioners = provisioners ? provisioners.length > 0 : undefined;
const showProvisionerWarning = hasProvisioners === false;

// Notify parent when provisioner status changes so the wizard can
// disable the create button when no provisioners are available.
useEffect(() => {
onProvisionerStatusChange(hasProvisioners);
}, [hasProvisioners, onProvisionerStatusChange]);

// Auto-select when exactly one org is available.
useEffect(() => {
Expand Down
19 changes: 19 additions & 0 deletions site/src/pages/TemplateBuilder/wizardState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type TemplateBuilderWizardState = {
baseVariableValues: Record<string, string>;
modules: TemplateBuilderComposeModule[];
organizationId?: string;
hasProvisioners: boolean | undefined;
name: string;
displayName: string;
description: string;
Expand All @@ -46,6 +47,7 @@ export const initialWizardState: TemplateBuilderWizardState = {
baseTemplateId: null,
baseVariableValues: {},
modules: [],
hasProvisioners: undefined,
name: "",
displayName: "",
description: "",
Expand All @@ -72,6 +74,8 @@ export type WizardAction =
field: "organizationId" | "name" | "displayName" | "description" | "icon";
value: string;
}
| { type: "SET_HAS_PROVISIONERS"; value: boolean | undefined }
| { type: "RESET_CUSTOMIZATIONS" }
| { type: "RESET" };

export function wizardReducer(
Expand Down Expand Up @@ -123,6 +127,21 @@ export function wizardReducer(
...state,
[action.field]: action.value,
};
case "SET_HAS_PROVISIONERS":
return {
...state,
hasProvisioners: action.value,
};
case "RESET_CUSTOMIZATIONS":
return {
...state,
organizationId: undefined,
hasProvisioners: undefined,
name: "",
displayName: "",
description: "",
icon: "",
};
case "RESET":
return initialWizardState;
default:
Expand Down
Loading