Skip to content

Commit

Permalink
[Painless] Minor state update model refactor (#60532)
Browse files Browse the repository at this point in the history
* Fix typo and i18n

* Make state init lazy

Otherwise we are needlessly reading and JSON.parse'ing on every
state update

* Support the query parameter in requests to Painless

* WiP on state refactor

* Some cleanup after manual testing

* Fix types and i18n
  • Loading branch information
jloleysens authored Mar 19, 2020
1 parent 8733480 commit c206320
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@
// This should be an enumerated list
export type Context = string;

export interface RequestPayloadConfig {
code?: string;
context?: string;
parameters?: string;
index?: string;
document?: string;
query?: string;
}

export enum PayloadFormat {
UGLY = 'ugly',
PRETTY = 'pretty',
Expand Down
69 changes: 12 additions & 57 deletions x-pack/plugins/painless_lab/public/application/components/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,34 @@
*/

import { HttpSetup } from 'kibana/public';
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, FunctionComponent } from 'react';
import { EuiFlexGroup, EuiFlexItem, EuiTitle } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { formatRequestPayload, formatJson } from '../lib/format';
import { painlessContextOptions, exampleScript } from '../common/constants';
import { exampleScript } from '../common/constants';
import { PayloadFormat } from '../common/types';
import { useSubmitCode } from '../hooks';
import { OutputPane } from './output_pane';
import { MainControls } from './main_controls';
import { Editor } from './editor';
import { RequestFlyout } from './request_flyout';
import { useAppContext } from '../context';

interface Props {
http: HttpSetup;
}

const PAINLESS_LAB_KEY = 'painlessLabState';

export function Main({ http }: Props) {
const [state, setState] = useState(() => ({
code: exampleScript,
context: painlessContextOptions[0].value,
parameters: '',
index: '',
document: '',
query: '',
...JSON.parse(localStorage.getItem(PAINLESS_LAB_KEY) || '{}'),
}));
export const Main: FunctionComponent<Props> = ({ http }) => {
const { state, updateState } = useAppContext();

const [isRequestFlyoutOpen, setRequestFlyoutOpen] = useState(false);
const { inProgress, response, submit } = useSubmitCode(http);

// Live-update the output and persist state as the user changes it.
const { code, context, parameters, index, document, query } = state;
useEffect(() => {
submit(state);
localStorage.setItem(PAINLESS_LAB_KEY, JSON.stringify(state));
}, [state, submit]);

const onCodeChange = (newCode: string) => {
setState({ ...state, code: newCode });
};

const onContextChange = (newContext: string) => {
setState({ ...state, context: newContext });
};

const onParametersChange = (newParameters: string) => {
setState({ ...state, parameters: newParameters });
};

const onIndexChange = (newIndex: string) => {
setState({ ...state, index: newIndex });
};

const onDocumentChange = (newDocument: string) => {
setState({ ...state, document: newDocument });
};

const onQueryChange = (newQuery: string) => {
setState({ ...state, query: newQuery });
};

const toggleRequestFlyout = () => {
setRequestFlyoutOpen(!isRequestFlyoutOpen);
};
Expand All @@ -84,32 +49,22 @@ export function Main({ http }: Props) {
</h1>
</EuiTitle>

<Editor code={code} onChange={onCodeChange} />
<Editor
code={state.code}
onChange={nextCode => updateState(() => ({ code: nextCode }))}
/>
</EuiFlexItem>

<EuiFlexItem>
<OutputPane
isLoading={inProgress}
response={response}
context={context}
parameters={parameters}
index={index}
document={document}
query={query}
onContextChange={onContextChange}
onParametersChange={onParametersChange}
onIndexChange={onIndexChange}
onDocumentChange={onDocumentChange}
onQueryChange={onQueryChange}
/>
<OutputPane isLoading={inProgress} response={response} />
</EuiFlexItem>
</EuiFlexGroup>

<MainControls
isLoading={inProgress}
toggleRequestFlyout={toggleRequestFlyout}
isRequestFlyoutOpen={isRequestFlyoutOpen}
reset={() => onCodeChange(exampleScript)}
reset={() => updateState(() => ({ code: exampleScript }))}
/>

{isRequestFlyoutOpen && (
Expand All @@ -121,4 +76,4 @@ export function Main({ http }: Props) {
)}
</div>
);
}
};
Loading

0 comments on commit c206320

Please sign in to comment.