From 634c9892df640b79a5e72a5c46822aeea03bee3a Mon Sep 17 00:00:00 2001 From: "Keith J. Grant" Date: Thu, 11 Mar 2021 10:49:57 -0800 Subject: [PATCH] VariablesDetail: don't evaluate YAML expressions --- .../components/CodeEditor/VariablesDetail.jsx | 76 +++++++++---------- .../CodeEditor/VariablesDetail.test.jsx | 2 +- 2 files changed, 35 insertions(+), 43 deletions(-) diff --git a/awx/ui_next/src/components/CodeEditor/VariablesDetail.jsx b/awx/ui_next/src/components/CodeEditor/VariablesDetail.jsx index 797c10673d..5063ab033f 100644 --- a/awx/ui_next/src/components/CodeEditor/VariablesDetail.jsx +++ b/awx/ui_next/src/components/CodeEditor/VariablesDetail.jsx @@ -1,5 +1,5 @@ import 'styled-components/macro'; -import React, { useState, useEffect } from 'react'; +import React, { useState } from 'react'; import { node, number, oneOfType, shape, string, arrayOf } from 'prop-types'; import { withI18n } from '@lingui/react'; import { t } from '@lingui/macro'; @@ -23,39 +23,42 @@ import { import CodeEditor from './CodeEditor'; import { JSON_MODE, YAML_MODE } from './constants'; -function getValueAsMode(value, mode) { - if (!value) { - if (mode === JSON_MODE) { - return '{}'; - } - return '---'; - } - const modeMatches = isJsonString(value) === (mode === JSON_MODE); - if (modeMatches) { - return value; - } - return mode === YAML_MODE ? jsonToYaml(value) : yamlToJson(value); -} - -function VariablesDetail({ dataCy, helpText, value, label, rows, i18n }) { +function VariablesDetail({ + dataCy, + helpText, + value, + label, + rows, + fullHeight, + i18n, +}) { const [mode, setMode] = useState( isJsonObject(value) || isJsonString(value) ? JSON_MODE : YAML_MODE ); - const [currentValue, setCurrentValue] = useState( - isJsonObject(value) ? JSON.stringify(value, null, 2) : value || '---' - ); const [isExpanded, setIsExpanded] = useState(false); - const [error, setError] = useState(null); - useEffect(() => { - setCurrentValue( - getValueAsMode( - isJsonObject(value) ? JSON.stringify(value, null, 2) : value, - mode - ) - ); - /* eslint-disable-next-line react-hooks/exhaustive-deps */ - }, [value]); + let currentValue = value; + let error; + + const getValueInCurrentMode = () => { + if (!value) { + if (mode === JSON_MODE) { + return '{}'; + } + return '---'; + } + const modeMatches = isJsonString(value) === (mode === JSON_MODE); + if (modeMatches) { + return value; + } + return mode === YAML_MODE ? jsonToYaml(value) : yamlToJson(value); + }; + + try { + currentValue = getValueInCurrentMode(); + } catch (err) { + error = err; + } const labelCy = dataCy ? `${dataCy}-label` : null; const valueCy = dataCy ? `${dataCy}-value` : null; @@ -76,8 +79,6 @@ function VariablesDetail({ dataCy, helpText, value, label, rows, i18n }) { mode={mode} setMode={setMode} currentValue={currentValue} - setCurrentValue={setCurrentValue} - setError={setError} onExpand={() => setIsExpanded(true)} i18n={i18n} /> @@ -93,6 +94,7 @@ function VariablesDetail({ dataCy, helpText, value, label, rows, i18n }) { value={currentValue} readOnly rows={rows} + fullHeight={fullHeight} css="margin-top: 10px" /> {error && ( @@ -129,8 +131,6 @@ function VariablesDetail({ dataCy, helpText, value, label, rows, i18n }) { mode={mode} setMode={setMode} currentValue={currentValue} - setCurrentValue={setCurrentValue} - setError={setError} i18n={i18n} /> { - try { - setCurrentValue(getValueAsMode(currentValue, newMode)); - setMode(newMode); - } catch (err) { - setError(err); - } + setMode(newMode); }} /> diff --git a/awx/ui_next/src/components/CodeEditor/VariablesDetail.test.jsx b/awx/ui_next/src/components/CodeEditor/VariablesDetail.test.jsx index 2705f2b2cf..e616c1beaf 100644 --- a/awx/ui_next/src/components/CodeEditor/VariablesDetail.test.jsx +++ b/awx/ui_next/src/components/CodeEditor/VariablesDetail.test.jsx @@ -39,7 +39,7 @@ describe('', () => { wrapper.find('MultiButtonToggle').invoke('onChange')('yaml'); const input2 = wrapper.find('VariablesDetail___StyledCodeEditor'); expect(input2.prop('mode')).toEqual('yaml'); - expect(input2.prop('value')).toEqual('foo: bar\n'); + expect(input2.prop('value')).toEqual('---foo: bar'); }); test('should render label and value= --- when there are no values', () => {