mirror of
https://github.com/ansible/awx.git
synced 2026-04-07 02:59:21 -02:30
VariablesDetail: don't evaluate YAML expressions
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import 'styled-components/macro';
|
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 { node, number, oneOfType, shape, string, arrayOf } from 'prop-types';
|
||||||
import { withI18n } from '@lingui/react';
|
import { withI18n } from '@lingui/react';
|
||||||
import { t } from '@lingui/macro';
|
import { t } from '@lingui/macro';
|
||||||
@@ -23,39 +23,42 @@ import {
|
|||||||
import CodeEditor from './CodeEditor';
|
import CodeEditor from './CodeEditor';
|
||||||
import { JSON_MODE, YAML_MODE } from './constants';
|
import { JSON_MODE, YAML_MODE } from './constants';
|
||||||
|
|
||||||
function getValueAsMode(value, mode) {
|
function VariablesDetail({
|
||||||
if (!value) {
|
dataCy,
|
||||||
if (mode === JSON_MODE) {
|
helpText,
|
||||||
return '{}';
|
value,
|
||||||
}
|
label,
|
||||||
return '---';
|
rows,
|
||||||
}
|
fullHeight,
|
||||||
const modeMatches = isJsonString(value) === (mode === JSON_MODE);
|
i18n,
|
||||||
if (modeMatches) {
|
}) {
|
||||||
return value;
|
|
||||||
}
|
|
||||||
return mode === YAML_MODE ? jsonToYaml(value) : yamlToJson(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
function VariablesDetail({ dataCy, helpText, value, label, rows, i18n }) {
|
|
||||||
const [mode, setMode] = useState(
|
const [mode, setMode] = useState(
|
||||||
isJsonObject(value) || isJsonString(value) ? JSON_MODE : YAML_MODE
|
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 [isExpanded, setIsExpanded] = useState(false);
|
||||||
const [error, setError] = useState(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
let currentValue = value;
|
||||||
setCurrentValue(
|
let error;
|
||||||
getValueAsMode(
|
|
||||||
isJsonObject(value) ? JSON.stringify(value, null, 2) : value,
|
const getValueInCurrentMode = () => {
|
||||||
mode
|
if (!value) {
|
||||||
)
|
if (mode === JSON_MODE) {
|
||||||
);
|
return '{}';
|
||||||
/* eslint-disable-next-line react-hooks/exhaustive-deps */
|
}
|
||||||
}, [value]);
|
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 labelCy = dataCy ? `${dataCy}-label` : null;
|
||||||
const valueCy = dataCy ? `${dataCy}-value` : null;
|
const valueCy = dataCy ? `${dataCy}-value` : null;
|
||||||
@@ -76,8 +79,6 @@ function VariablesDetail({ dataCy, helpText, value, label, rows, i18n }) {
|
|||||||
mode={mode}
|
mode={mode}
|
||||||
setMode={setMode}
|
setMode={setMode}
|
||||||
currentValue={currentValue}
|
currentValue={currentValue}
|
||||||
setCurrentValue={setCurrentValue}
|
|
||||||
setError={setError}
|
|
||||||
onExpand={() => setIsExpanded(true)}
|
onExpand={() => setIsExpanded(true)}
|
||||||
i18n={i18n}
|
i18n={i18n}
|
||||||
/>
|
/>
|
||||||
@@ -93,6 +94,7 @@ function VariablesDetail({ dataCy, helpText, value, label, rows, i18n }) {
|
|||||||
value={currentValue}
|
value={currentValue}
|
||||||
readOnly
|
readOnly
|
||||||
rows={rows}
|
rows={rows}
|
||||||
|
fullHeight={fullHeight}
|
||||||
css="margin-top: 10px"
|
css="margin-top: 10px"
|
||||||
/>
|
/>
|
||||||
{error && (
|
{error && (
|
||||||
@@ -129,8 +131,6 @@ function VariablesDetail({ dataCy, helpText, value, label, rows, i18n }) {
|
|||||||
mode={mode}
|
mode={mode}
|
||||||
setMode={setMode}
|
setMode={setMode}
|
||||||
currentValue={currentValue}
|
currentValue={currentValue}
|
||||||
setCurrentValue={setCurrentValue}
|
|
||||||
setError={setError}
|
|
||||||
i18n={i18n}
|
i18n={i18n}
|
||||||
/>
|
/>
|
||||||
<CodeEditor
|
<CodeEditor
|
||||||
@@ -163,11 +163,8 @@ function ModeToggle({
|
|||||||
label,
|
label,
|
||||||
helpText,
|
helpText,
|
||||||
dataCy,
|
dataCy,
|
||||||
currentValue,
|
|
||||||
setCurrentValue,
|
|
||||||
mode,
|
mode,
|
||||||
setMode,
|
setMode,
|
||||||
setError,
|
|
||||||
onExpand,
|
onExpand,
|
||||||
i18n,
|
i18n,
|
||||||
}) {
|
}) {
|
||||||
@@ -196,12 +193,7 @@ function ModeToggle({
|
|||||||
]}
|
]}
|
||||||
value={mode}
|
value={mode}
|
||||||
onChange={newMode => {
|
onChange={newMode => {
|
||||||
try {
|
setMode(newMode);
|
||||||
setCurrentValue(getValueAsMode(currentValue, newMode));
|
|
||||||
setMode(newMode);
|
|
||||||
} catch (err) {
|
|
||||||
setError(err);
|
|
||||||
}
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</SplitItem>
|
</SplitItem>
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ describe('<VariablesDetail>', () => {
|
|||||||
wrapper.find('MultiButtonToggle').invoke('onChange')('yaml');
|
wrapper.find('MultiButtonToggle').invoke('onChange')('yaml');
|
||||||
const input2 = wrapper.find('VariablesDetail___StyledCodeEditor');
|
const input2 = wrapper.find('VariablesDetail___StyledCodeEditor');
|
||||||
expect(input2.prop('mode')).toEqual('yaml');
|
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', () => {
|
test('should render label and value= --- when there are no values', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user