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

[D&D] Refactor and cleanup embeddables #1947

Merged
Show file tree
Hide file tree
Changes from all 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
91 changes: 58 additions & 33 deletions src/plugins/wizard/public/assets/fields_bg.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 12 additions & 12 deletions src/plugins/wizard/public/assets/hand_field.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions src/plugins/wizard/public/assets/wizard_icon_secondary_fill.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 16 additions & 50 deletions src/plugins/wizard/public/embeddable/wizard_component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,32 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useEffect, useState } from 'react';
import React from 'react';

import { SavedObjectEmbeddableInput, withEmbeddableSubscription } from '../../../embeddable/public';
import { WizardEmbeddable, WizardOutput } from './wizard_embeddable';
import { validateSchemaState } from '../application/utils/validate_schema_state';
import { getReactExpressionRenderer } from '../plugin_services';

interface Props {
embeddable: WizardEmbeddable;
input: SavedObjectEmbeddableInput;
output: WizardOutput;
}

function WizardEmbeddableComponentInner({
embeddable,
input: {},
output: { savedAttributes },
}: Props) {
const { ReactExpressionRenderer, toasts, types, indexPatterns, aggs } = embeddable;
const [expression, setExpression] = useState<string>();

useEffect(() => {
const { visualizationState: visualization, styleState: style } = savedAttributes || {};
if (savedAttributes === undefined || visualization === undefined || style === undefined) {
return;
}

const rootState = {
visualization: JSON.parse(visualization),
style: JSON.parse(style),
};

const visualizationType = types.get(rootState.visualization?.activeVisualization?.name ?? '');
if (!visualizationType) {
throw new Error(`Invalid visualization type ${visualizationType}`);
}
const { toExpression, ui } = visualizationType;

async function loadExpression() {
const schemas = ui.containerConfig.data.schemas;
const [valid, errorMsg] = validateSchemaState(schemas, rootState);

if (!valid) {
if (errorMsg) {
toasts.addWarning(errorMsg);
}
setExpression(undefined);
return;
}
const exp = await toExpression(rootState, indexPatterns, aggs);
setExpression(exp);
}

if (savedAttributes !== undefined) {
loadExpression();
}
}, [aggs, indexPatterns, savedAttributes, toasts, types]);

return <ReactExpressionRenderer expression={expression ?? ''} />;

// TODO: add correct loading and error states
function WizardEmbeddableComponentInner({ embeddable, input: {}, output: { error } }: Props) {
const { expression } = embeddable;
const ReactExpressionRenderer = getReactExpressionRenderer();

return (
<>
{error?.message ? (
// TODO: add correct loading and error states
<div>{error.message}</div>
) : (
<ReactExpressionRenderer expression={expression ?? ''} />
)}
</>
);
}

export const WizardEmbeddableComponent = withEmbeddableSubscription<
Expand Down
Loading