Skip to content

Commit

Permalink
[Lens] Fix when clicking "Save and Return" on a Lens visualization th…
Browse files Browse the repository at this point in the history
…e visualization's description gets erased (#108669)

Closes: #107841
  • Loading branch information
alexwizp authored Aug 17, 2021
1 parent 0e547e4 commit 8d5c3c8
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions x-pack/plugins/lens/public/app_plugin/save_modal_container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
*/

import React, { useEffect, useState } from 'react';
import { ChromeStart, NotificationsStart } from 'kibana/public';
import { i18n } from '@kbn/i18n';
import { METRIC_TYPE } from '@kbn/analytics';
import { partition } from 'lodash';

import type { ChromeStart, NotificationsStart, SavedObjectReference } from 'kibana/public';
import { SaveModal } from './save_modal';
import { LensAppProps, LensAppServices } from './types';
import type { LensAppProps, LensAppServices } from './types';
import type { SaveProps } from './app';
import { Document, injectFilterReferences } from '../persistence';
import { LensByReferenceInput, LensEmbeddableInput } from '../embeddable';
import { LensAttributeService } from '../lens_attribute_service';
import type { LensByReferenceInput, LensEmbeddableInput } from '../embeddable';
import type { LensAttributeService } from '../lens_attribute_service';
import { DataPublicPluginStart, esFilters } from '../../../../../src/plugins/data/public';
import { APP_ID, getFullPath, LENS_EMBEDDABLE_TYPE } from '../../common';
import { trackUiEvent } from '../lens_ui_telemetry';
import { checkForDuplicateTitle } from '../../../../../src/plugins/saved_objects/public';
import { LensAppState } from '../state_management';
import type { LensAppState } from '../state_management';

type ExtraProps = Pick<LensAppProps, 'initialInput'> &
Partial<Pick<LensAppProps, 'redirectToOrigin' | 'redirectTo' | 'onAppLeave'>>;
Expand Down Expand Up @@ -181,6 +182,27 @@ const redirectToDashboard = ({
});
};

const getDocToSave = (
lastKnownDoc: Document,
saveProps: SaveProps,
references: SavedObjectReference[]
) => {
const docToSave = {
...getLastKnownDocWithoutPinnedFilters(lastKnownDoc)!,
references,
};

if (saveProps.newDescription !== undefined) {
docToSave.description = saveProps.newDescription;
}

if (saveProps.newTitle !== undefined) {
docToSave.title = saveProps.newTitle;
}

return docToSave;
};

export const runSaveLensVisualization = async (
props: {
lastKnownDoc?: Document;
Expand All @@ -192,10 +214,6 @@ export const runSaveLensVisualization = async (
saveProps: SaveProps,
options: { saveToLibrary: boolean }
): Promise<Partial<LensAppState> | undefined> => {
if (!props.lastKnownDoc) {
return;
}

const {
chrome,
initialInput,
Expand All @@ -216,28 +234,29 @@ export const runSaveLensVisualization = async (
dashboardFeatureFlag,
} = props;

const tagsIds =
persistedDoc && savedObjectsTagging
? savedObjectsTagging.ui.getTagIdsFromReferences(persistedDoc.references)
: [];
if (!lastKnownDoc) {
return;
}

if (usageCollection) {
usageCollection.reportUiCounter(originatingApp || 'visualize', METRIC_TYPE.CLICK, 'lens:save');
}

let references = lastKnownDoc.references;

if (savedObjectsTagging) {
const tagsIds =
persistedDoc && savedObjectsTagging
? savedObjectsTagging.ui.getTagIdsFromReferences(persistedDoc.references)
: [];

references = savedObjectsTagging.ui.updateTagsReferences(
references,
saveProps.newTags || tagsIds
);
}

const docToSave = {
...getLastKnownDocWithoutPinnedFilters(lastKnownDoc)!,
description: saveProps.newDescription,
title: saveProps.newTitle,
references,
};
const docToSave = getDocToSave(lastKnownDoc, saveProps, references);

// Required to serialize filters in by value mode until
// https://github.com/elastic/kibana/issues/77588 is fixed
Expand Down

0 comments on commit 8d5c3c8

Please sign in to comment.