From c05fee0c3f791d9a032f5a9576e620cb147622de Mon Sep 17 00:00:00 2001 From: Arpit Date: Fri, 13 Oct 2023 13:32:18 +0530 Subject: [PATCH] adds support of constants to current state of the ediotr (#7821) --- .../src/Editor/CodeBuilder/CodeHinter.jsx | 26 ++++++++++++------- frontend/src/Editor/EditorFunc.jsx | 17 ++++++++++++ .../Components/QueryManagerBody.jsx | 8 ------ 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/frontend/src/Editor/CodeBuilder/CodeHinter.jsx b/frontend/src/Editor/CodeBuilder/CodeHinter.jsx index 2685d3d3d4..e97e4e9024 100644 --- a/frontend/src/Editor/CodeBuilder/CodeHinter.jsx +++ b/frontend/src/Editor/CodeBuilder/CodeHinter.jsx @@ -93,7 +93,7 @@ export function CodeHinter({ }; const currentState = useCurrentState(); const [realState, setRealState] = useState(currentState); - const [currentValue, setCurrentValue] = useState(initialValue); + const [currentValue, setCurrentValue] = useState(''); const [prevCurrentValue, setPrevCurrentValue] = useState(null); const [resolvedValue, setResolvedValue] = useState(null); @@ -120,6 +120,17 @@ export function CodeHinter({ const { variablesExposedForPreview } = useContext(EditorContext); const prevCountRef = useRef(false); + useEffect(() => { + setCurrentValue(initialValue); + + return () => { + setPrevCurrentValue(null); + setResolvedValue(null); + setResolvingError(null); + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + useEffect(() => { if (_currentState) { setRealState(_currentState); @@ -127,7 +138,7 @@ export function CodeHinter({ setRealState(currentState); } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [currentState.components, _currentState]); + }, [JSON.stringify({ currentState, _currentState })]); useEffect(() => { const handleClickOutside = (event) => { @@ -149,7 +160,7 @@ export function CodeHinter({ }, [wrapperRef, isFocused, isPreviewFocused, currentValue, prevCountRef, isOpen]); useEffect(() => { - if (JSON.stringify(currentValue) !== JSON.stringify(prevCurrentValue)) { + if (enablePreview && isFocused && JSON.stringify(currentValue) !== JSON.stringify(prevCurrentValue)) { const customResolvables = getCustomResolvables(); const [preview, error] = resolveReferences(currentValue, realState, null, customResolvables, true, true); setPrevCurrentValue(currentValue); @@ -162,13 +173,8 @@ export function CodeHinter({ setResolvedValue(preview); } } - - return () => { - setPrevCurrentValue(null); - setResolvedValue(null); - setResolvingError(null); - }; - }, [JSON.stringify({ currentValue, realState })]); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [JSON.stringify({ currentValue, realState, isFocused })]); function valueChanged(editor, onChange, ignoreBraces) { if (editor.getValue()?.trim() !== currentValue) { diff --git a/frontend/src/Editor/EditorFunc.jsx b/frontend/src/Editor/EditorFunc.jsx index 4847301915..7144a3d34d 100644 --- a/frontend/src/Editor/EditorFunc.jsx +++ b/frontend/src/Editor/EditorFunc.jsx @@ -5,6 +5,7 @@ import { appVersionService, orgEnvironmentVariableService, appEnvironmentService, + orgEnvironmentConstantService, } from '@/_services'; import { DndProvider } from 'react-dnd'; import { HTML5Backend } from 'react-dnd-html5-backend'; @@ -298,6 +299,21 @@ const EditorComponent = (props) => { }); }; + const fetchOrgEnvironmentConstants = () => { + //! for @ee: get the constants from `getConstantsFromEnvironment ` -- '/organization-constants/:environmentId' + orgEnvironmentConstantService.getAll().then(({ constants }) => { + const orgConstants = {}; + constants.map((constant) => { + const constantValue = constant.values.find((value) => value.environmentName === 'production')['value']; + orgConstants[constant.name] = constantValue; + }); + + useCurrentStateStore.getState().actions.setCurrentState({ + constants: orgConstants, + }); + }); + }; + const initComponentVersioning = () => { updateEditorState({ canUndo: false, @@ -355,6 +371,7 @@ const EditorComponent = (props) => { await fetchApps(0); await fetchOrgEnvironmentVariables(); + await fetchOrgEnvironmentConstants(); initComponentVersioning(); initRealtimeSave(); initEventListeners(); diff --git a/frontend/src/Editor/QueryManager/Components/QueryManagerBody.jsx b/frontend/src/Editor/QueryManager/Components/QueryManagerBody.jsx index 7a81c173a3..0d3467d36a 100644 --- a/frontend/src/Editor/QueryManager/Components/QueryManagerBody.jsx +++ b/frontend/src/Editor/QueryManager/Components/QueryManagerBody.jsx @@ -97,14 +97,6 @@ export const QueryManagerBody = ({ validateNewOptions(newOptions); }; - const eventsChanged = (events) => { - optionchanged('events', events); - //added this here since the subscriber added in QueryManager component does not detect this change - useDataQueriesStore - .getState() - .actions.saveData({ ...selectedQuery, options: { ...selectedQuery.options, events: events } }); - }; - const toggleOption = (option) => { const currentValue = selectedQuery?.options?.[option] ?? false; optionchanged(option, !currentValue);