-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move saved object referfence extract and inject into the individual e…
…mbeddable factory methods."
- Loading branch information
1 parent
ee34012
commit 6a68f24
Showing
11 changed files
with
155 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/plugins/embeddable/common/lib/saved_object_id_references.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { Reference } from '@kbn/content-management-utils'; | ||
import { EmbeddableStateWithType } from '../types'; | ||
|
||
type EmbeddableStateWithTypeAndSavedObjectId = EmbeddableStateWithType & { savedObjectId?: string }; | ||
const stateHasSavedObjectId = ( | ||
state: EmbeddableStateWithType | ||
): state is EmbeddableStateWithTypeAndSavedObjectId => { | ||
return Boolean((state as EmbeddableStateWithTypeAndSavedObjectId).savedObjectId); | ||
}; | ||
|
||
const panelSignifier = 'panel_'; | ||
|
||
export const injectSavedObjectIdRef = ( | ||
state: EmbeddableStateWithType, | ||
references: Reference[] | ||
): EmbeddableStateWithType => { | ||
const savedObjectReference = references.find( | ||
(reference) => reference.name.indexOf(`${panelSignifier}${state.id}`) === 0 | ||
); | ||
if (!savedObjectReference) { | ||
return state; | ||
} | ||
|
||
const nextState: EmbeddableStateWithTypeAndSavedObjectId = { | ||
...state, | ||
savedObjectId: savedObjectReference.id, | ||
}; | ||
return nextState; | ||
}; | ||
|
||
export const extractSavedObjectIdRef = ( | ||
state: EmbeddableStateWithType, | ||
references: Reference[] | ||
): { state: EmbeddableStateWithType; references: Reference[] } => { | ||
if (!stateHasSavedObjectId(state) || !state.savedObjectId) return { state, references }; | ||
references.push({ | ||
name: `${panelSignifier}${state.id}`, | ||
type: state.type, | ||
id: state.savedObjectId, | ||
}); | ||
|
||
delete state.savedObjectId; | ||
return { state, references }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.