From 34a4bd93bd37b11042ebacd08ca1daa64c81763f Mon Sep 17 00:00:00 2001 From: Ari Date: Sun, 19 Jun 2022 01:04:02 -0400 Subject: [PATCH] better saving for yaml editor --- webapp/src/components/Inputs/InputYaml.tsx | 67 +++++++++++++------ .../ServiceEditor/ServiceEditorField.tsx | 6 +- 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/webapp/src/components/Inputs/InputYaml.tsx b/webapp/src/components/Inputs/InputYaml.tsx index 0c6fc3d..3fd4883 100644 --- a/webapp/src/components/Inputs/InputYaml.tsx +++ b/webapp/src/components/Inputs/InputYaml.tsx @@ -6,7 +6,8 @@ import { useDelayEffect } from "utils/useDelay"; import CodeMirror from "@uiw/react-codemirror"; import FormControl from "@mui/material/FormControl"; import InputLabel from "@mui/material/InputLabel"; -import { FormHelperText, InputAdornment } from "@mui/material"; +import { FormHelperText, IconButton, InputAdornment } from "@mui/material"; +import { Save } from "@mui/icons-material"; export interface Props { label: string; @@ -32,42 +33,57 @@ export default function InputYaml({ helperText, endAdornment, }: Props) { - const [{ text, error }, setState] = useState<{ + const [{ text, error, hasChanged }, setState] = useState<{ text: string; error: undefined | JSX.Element | string; + hasChanged: boolean; }>({ text: yaml.dump(value), error: incmError, + hasChanged: false, }); const setText = (t: string) => { try { yaml.load(t); - setState({ text: t, error: undefined }); + console.log("success!", yaml.load(t)); + setState({ text: t, error: undefined, hasChanged: true }); } catch (_) { - setState({ text: t, error: "! Invalid Yaml !" }); + setState({ text: t, error: "! Invalid Yaml !", hasChanged: true }); + } + }; + const onSave = () => { + if (!error && yaml.dump(value) !== text) { + if (text) { + onChange(yaml.load(text) as any); + } else if (Array.isArray(value)) { + onChange([] as any); + } else { + onChange({} as any); + } } }; useEffect(() => { setState({ text: yaml.dump(value), error: incmError, + hasChanged: false, }); }, [value, incmError]); - useDelayEffect( - () => { - if (!error && yaml.dump(value) !== text) { - if (text) { - onChange(yaml.load(text) as any); - } else if (Array.isArray(value)) { - onChange([] as any); - } else { - onChange({} as any); - } - } - }, - [text], - 1000 - ); + // useDelayEffect( + // () => { + // if (!error && yaml.dump(value) !== text) { + // if (text) { + // onChange(yaml.load(text) as any); + // } else if (Array.isArray(value)) { + // onChange([] as any); + // } else { + // onChange({} as any); + // } + // } + // }, + // [text], + // 1000 + // ); return ( ({ {!!text ? "" : placeholder} - {endAdornment} + + + + + {endAdornment} + {error ? error : helperText} diff --git a/webapp/src/components/ServiceEditor/ServiceEditorField.tsx b/webapp/src/components/ServiceEditor/ServiceEditorField.tsx index 198492c..59cfa80 100644 --- a/webapp/src/components/ServiceEditor/ServiceEditorField.tsx +++ b/webapp/src/components/ServiceEditor/ServiceEditorField.tsx @@ -30,7 +30,11 @@ export const ServiceEditorField: FC = ({ const removeIcon = !onRemove ? ( <> ) : ( - + );