Skip to content

Commit

Permalink
fix bug when changing template
Browse files Browse the repository at this point in the history
  • Loading branch information
baktun14 committed Oct 19, 2023
1 parent 2096287 commit 8fab063
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
10 changes: 6 additions & 4 deletions deploy-web/src/components/newDeploymentWizard/ManifestEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,12 @@ export const ManifestEdit: React.FunctionComponent<Props> = ({ editedManifest, s

{parsingError && <Alert severity="warning">{parsingError}</Alert>}

<ViewPanel stickToBottom style={{ overflow: "hidden", margin: smallScreen ? "0 -1rem" : 0 }}>
{selectedSdlEditMode === "yaml" && <DynamicMonacoEditor value={editedManifest} onChange={handleTextChange} />}
{selectedSdlEditMode === "builder" && <SdlBuilder sdlString={editedManifest} ref={sdlBuilderRef} setEditedManifest={setEditedManifest} />}
</ViewPanel>
{selectedSdlEditMode === "yaml" && (
<ViewPanel stickToBottom style={{ overflow: "hidden", margin: smallScreen ? "0 -1rem" : 0 }}>
<DynamicMonacoEditor value={editedManifest} onChange={handleTextChange} />
</ViewPanel>
)}
{selectedSdlEditMode === "builder" && <SdlBuilder sdlString={editedManifest} ref={sdlBuilderRef} setEditedManifest={setEditedManifest} />}

{isDepositingDeployment && (
<DeploymentDepositModal
Expand Down
25 changes: 12 additions & 13 deletions deploy-web/src/components/newDeploymentWizard/SdlBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Alert, Box, Button, CircularProgress } from "@mui/material";
import { SimpleServiceFormControl } from "../sdl/SimpleServiceFormControl";
import { useProviderAttributesSchema } from "@src/queries/useProvidersQuery";
import { importSimpleSdl } from "@src/utils/sdl/sdlImport";
import { Subscription } from "react-hook-form/dist/utils/createSubject";

interface Props {
sdlString: string;
Expand All @@ -21,7 +22,7 @@ export type SdlBuilderRefType = {
export const SdlBuilder = React.forwardRef<SdlBuilderRefType, Props>(({ sdlString, setEditedManifest }, ref) => {
const [error, setError] = useState(null);
const formRef = useRef<HTMLFormElement>();
const [isLoading, setIsLoading] = useState(true);
const [isInit, setIsInit] = useState(false);
const { control, trigger, watch, setValue } = useForm<SdlBuilderFormValues>({
defaultValues: {
services: [{ ...defaultService }]
Expand All @@ -45,24 +46,22 @@ export const SdlBuilder = React.forwardRef<SdlBuilderRefType, Props>(({ sdlStrin
}));

useEffect(() => {
const { unsubscribe } = watch(data => {
const sdl = generateSdl({ services: data.services as Service[] });
setEditedManifest(sdl);
});

try {
const services = createAndValidateSdl(sdlString);
setValue("services", services as Service[]);
} catch (error) {
setError("Error importing SDL");
}

setIsLoading(false);
}, []);

useEffect(() => {
const subscription = watch(data => {
const sdl = generateSdl({ services: data.services as Service[] });
setEditedManifest(sdl);
});
setIsInit(true);

return () => {
subscription.unsubscribe();
unsubscribe();
};
}, [watch]);

Expand Down Expand Up @@ -101,8 +100,8 @@ export const SdlBuilder = React.forwardRef<SdlBuilderRefType, Props>(({ sdlStrin
};

return (
<>
{isLoading ? (
<Box sx={{ paddingBottom: "2rem" }}>
{!isInit ? (
<Box sx={{ display: "flex", alignItems: "center", justifyContent: "center", padding: "2rem" }}>
<CircularProgress size="3rem" color="secondary" />
</Box>
Expand Down Expand Up @@ -138,6 +137,6 @@ export const SdlBuilder = React.forwardRef<SdlBuilderRefType, Props>(({ sdlStrin
</Box>
</form>
)}
</>
</Box>
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ const previewTemplateIds = [

type Props = {
setSelectedTemplate: Dispatch<TemplateCreation>;
setEditedManifest: Dispatch<string>;
};

export const TemplateList: React.FunctionComponent<Props> = ({ setSelectedTemplate }) => {
export const TemplateList: React.FunctionComponent<Props> = ({ setSelectedTemplate, setEditedManifest }) => {
const { templates } = useTemplates();
const { classes } = useStyles();
const router = useRouter();
Expand All @@ -52,6 +53,7 @@ export const TemplateList: React.FunctionComponent<Props> = ({ setSelectedTempla

function selectTemplate(template) {
setSelectedTemplate(template);
setEditedManifest(template.content);
router.push(UrlService.newDeployment({ step: RouteStepKeys.editDeployment }));
}

Expand All @@ -71,6 +73,7 @@ export const TemplateList: React.FunctionComponent<Props> = ({ setSelectedTempla
description: "Custom uploaded file",
content: event.target.result as string
});
setEditedManifest(event.target.result as string);
router.push(UrlService.newDeployment({ step: RouteStepKeys.editDeployment }));
};

Expand Down
8 changes: 3 additions & 5 deletions deploy-web/src/pages/new-deployment/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ const NewDeploymentPage: React.FunctionComponent<Props> = ({}) => {
if (redeployTemplate) {
// If it's a redeploy, set the template from local storage
setSelectedTemplate(redeployTemplate);
setEditedManifest(redeployTemplate.content);
} else if (galleryTemplate) {
// If it's a deploy from the template gallery, load from template data
setSelectedTemplate(galleryTemplate as TemplateCreation);
setEditedManifest(galleryTemplate.content);
}

const _activeStep = getStepIndexByParam(router.query.step);
Expand All @@ -68,10 +70,6 @@ const NewDeploymentPage: React.FunctionComponent<Props> = ({}) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [router.query, templates]);

useEffect(() => {
setEditedManifest(selectedTemplate?.content || "");
}, [selectedTemplate]);

const getRedeployTemplate = () => {
let template = null;
if (router.query.redeploy) {
Expand Down Expand Up @@ -148,7 +146,7 @@ const NewDeploymentPage: React.FunctionComponent<Props> = ({}) => {
<CustomizedSteppers steps={steps} activeStep={activeStep} />
</div>

{activeStep === 0 && <TemplateList setSelectedTemplate={c => setSelectedTemplate(c)} />}
{activeStep === 0 && <TemplateList setSelectedTemplate={setSelectedTemplate} setEditedManifest={setEditedManifest} />}
{activeStep === 1 && <ManifestEdit selectedTemplate={selectedTemplate} editedManifest={editedManifest} setEditedManifest={setEditedManifest} />}
{activeStep === 2 && <CreateLease dseq={router.query.dseq as string} />}
</Container>
Expand Down

0 comments on commit 8fab063

Please sign in to comment.