From 78d510c6a839da2e0817aa38a255891d23a44f4d Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Wed, 15 Jan 2020 10:01:48 +0100 Subject: [PATCH] Add Tabs to output --- .../public/components/painless_playground.tsx | 155 +++++++----------- 1 file changed, 63 insertions(+), 92 deletions(-) diff --git a/x-pack/legacy/plugins/painless_playground/public/components/painless_playground.tsx b/x-pack/legacy/plugins/painless_playground/public/components/painless_playground.tsx index 7114c4b3beb41..3e3933d322d35 100644 --- a/x-pack/legacy/plugins/painless_playground/public/components/painless_playground.tsx +++ b/x-pack/legacy/plugins/painless_playground/public/components/painless_playground.tsx @@ -13,6 +13,8 @@ import { EuiPageContentBody, EuiSpacer, EuiFormRow, + EuiTabbedContent, + EuiCodeBlock, } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import { CodeEditor } from '../../../../../../src/plugins/kibana_react/public'; @@ -20,48 +22,39 @@ import { CodeEditor } from '../../../../../../src/plugins/kibana_react/public'; interface Props { executeCode: (payload: Record) => Promise; } +interface Response { + error?: { [key: string]: any }; + result?: string; +} +function getRequest(code: string) { + return { + script: { + source: code, + }, + }; +} -interface State { - code: string; - request?: string; - response?: string; - responseObj: Record | null; +function formatJson(text) { + try { + return JSON.stringify(text, null, 2); + } catch (e) { + return `Invalid JSON ${String(text)}`; + } } -export function PainlessPlayground(props: Props) { - const [state, setState] = useState({ - code: '', - request: '', - response: '', - responseObj: null, - }); + +export function PainlessPlayground({ executeCode }: Props) { + const [code, setCode] = useState(''); + const [response, setResponse] = useState({}); const submit = async () => { - const request = { - script: { - source: state.code, - }, - }; try { - const response = await props.executeCode(request); - setState({ - code: state.code, - request: JSON.stringify(request, null, 2), - response: JSON.stringify(response, null, 2), - responseObj: response, - }); + const res = await executeCode(getRequest(code)); + setResponse(res); } catch (e) { - setState({ - code: state.code, - response: JSON.stringify(e, null, 2), - request: JSON.stringify(request, null, 2), - responseObj: e, - }); + setResponse(e); } }; - const onSimulateClick = () => { - submit(); - }; return ( @@ -82,8 +75,8 @@ export function PainlessPlayground(props: Props) { setState({ code })} + value={code} + onChange={setCode} options={{ fontSize: 12, minimap: { @@ -99,8 +92,8 @@ export function PainlessPlayground(props: Props) { - - - } - fullWidth - data-test-subj="response" - > -
- {state.responseObject?.body?.error ? ( -
{state.responseObject?.body?.error}
- ) : ( - - )} -
-
- {state.request && ( - - } - fullWidth - data-test-subj="request" - > -
- -
-
- )} + {response.error || response.result ? ( + + {response?.result ? response?.result : formatJson(response?.error)} + + ), + }, + { + id: 'response', + name: 'Request', + content: ( + + {'POST _scripts/painless/_execute\n' + formatJson(getRequest(code))} + + ), + }, + { + id: 'request', + name: 'Response', + content: ( + + {formatJson(response)} + + ), + }, + ]} + /> + ) : null}