adds support of constants to current state of the ediotr (#7821)

This commit is contained in:
Arpit 2023-10-13 13:32:18 +05:30 committed by GitHub
parent f33893cb60
commit c05fee0c3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 18 deletions

View file

@ -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) {

View file

@ -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();

View file

@ -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);