From 3c987c61ddc258f991e2bf38c1f16a0c4248d6ae Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Tue, 26 Jul 2022 23:15:42 +0500 Subject: [PATCH] fix(web): saveAs functionality fixed in studio page --- web/src/containers/Studio/editor.tsx | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/web/src/containers/Studio/editor.tsx b/web/src/containers/Studio/editor.tsx index 5742ad2..1a9a677 100644 --- a/web/src/containers/Studio/editor.tsx +++ b/web/src/containers/Studio/editor.tsx @@ -39,7 +39,7 @@ import FilePathInputModal from '../../components/filePathInputModal' import BootstrapSnackbar, { AlertSeverityType } from '../../components/snackbar' import Modal from '../../components/modal' -import usePrompt from '../../utils/usePrompt' +import { usePrompt, useStateWithCallback } from '../../utils/hooks' const StyledTabPanel = styled(TabPanel)(() => ({ padding: '10px' @@ -74,7 +74,7 @@ const SASjsEditor = ({ const [snackbarSeverity, setSnackbarSeverity] = useState( AlertSeverityType.Success ) - const [prevFileContent, setPrevFileContent] = useState('') + const [prevFileContent, setPrevFileContent] = useStateWithCallback('') const [fileContent, setFileContent] = useState('') const [log, setLog] = useState('') const [ctrlPressed, setCtrlPressed] = useState(false) @@ -136,6 +136,7 @@ const SASjsEditor = ({ } else { setFileContent('') } + // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedFilePath]) useEffect(() => { @@ -223,10 +224,22 @@ const SASjsEditor = ({ axiosPromise .then(() => { - if (filePath) { + if (filePath && fileContent === prevFileContent) { + // when fileContent and prevFileContent is same, + // callback function in setPrevFileContent method is not called + // because behind the scene useEffect hook is being used + // for calling callback function, and it's only fired when the + // new value is not equal to old value. + // So, we'll have to explicitly update the selected file path + setSelectedFilePath(filePath, true) + } else { + setPrevFileContent(fileContent, () => { + if (filePath) { + setSelectedFilePath(filePath, true) + } + }) } - setPrevFileContent(fileContent) setSnackbarMessage('File saved!') setSnackbarSeverity(AlertSeverityType.Success) setOpenSnackbar(true)