Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Painless] Minor state update model refactor #60532

Merged
merged 8 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const painlessContextOptions = [
<strong>{defaultLabel}</strong>
<EuiText size="s" color="subdued">
<p className="euiTextColor--subdued">
{i18n.translate('xpack.painlessLab.context.scoreLabel', {
{i18n.translate('xpack.painlessLab.context.defaultLabel', {
defaultMessage: 'The script result will be converted to a string',
})}
</p>
Expand All @@ -46,7 +46,7 @@ export const painlessContextOptions = [
<strong>{filterLabel}</strong>
<EuiText size="s" color="subdued">
<p className="euiTextColor--subdued">
{i18n.translate('xpack.painlessLab.context.scoreLabel', {
{i18n.translate('xpack.painlessLab.context.filterLabel', {
defaultMessage: "Use the context of a filter's script query",
})}
</p>
Expand Down
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