import React from 'react' ; import { OverlayStart } from 'opensearch-dashboards/public' ; import { i18n } from '@osd/i18n' ; import { BookSavedObjectAttributes , BOOK_SAVED_OBJECT } from '../../common' ; import { createAction } from '../../../../src/plugins/ui_actions/public' ; import { toMountPoint } from '../../../../src/plugins/opensearch_dashboards_react/public' ; import { ViewMode , SavedObjectEmbeddableInput } from '../../../../src/plugins/embeddable/public' ; import { BookEmbeddable , BOOK_EMBEDDABLE , BookByReferenceInput , BookByValueInput , } from './book_embeddable' ; import { CreateEditBookComponent } from './create_edit_book_component' ; import { DashboardStart } from '../../../../src/plugins/dashboard/public' ; import { OnSaveProps } from '../../../../src/plugins/saved_objects/public' ; import { SavedObjectsClientContract } from '../../../../src/core/target/types/public/saved_objects' ; interface StartServices { openModal : OverlayStart [ 'openModal' ] ; getAttributeService : DashboardStart [ 'getAttributeService' ] ; savedObjectsClient : SavedObjectsClientContract ; } interface ActionContext { embeddable : BookEmbeddable ; } export const ACTION_EDIT_BOOK = 'ACTION_EDIT_BOOK' ; export const createEditBookAction = ( getStartServices ) => createAction ( { getDisplayName : ( ) => i18n . translate ( 'embeddableExamples.book.edit' , { defaultMessage : 'Edit Book' } ) , type : ACTION_EDIT_BOOK , order : 100 , getIconType : ( ) => 'documents' , isCompatible : async ( { embeddable } ) => { return ( embeddable . type === BOOK_EMBEDDABLE && embeddable . getInput ( ) . viewMode === ViewMode . EDIT ) ; } , execute : async ( { embeddable } ) => { const { openModal , getAttributeService , savedObjectsClient } = await getStartServices ( ) ; const attributeService = getAttributeService < BookSavedObjectAttributes > ( BOOK_SAVED_OBJECT , { saveMethod : async ( attributes , savedObjectId ? ) => { if ( savedObjectId ) { return savedObjectsClient . update ( BOOK_EMBEDDABLE , savedObjectId , attributes ) ; } return savedObjectsClient . create ( BOOK_EMBEDDABLE , attributes ) ; } , checkForDuplicateTitle : ( props ) => { return new Promise ( ( ) => { return true ; } ) ; } , } ) ; const onSave = async ( attributes , useRefType ) => { const newInput = await attributeService . wrapAttributes ( attributes , useRefType , attributeService . getExplicitInputFromEmbeddable ( embeddable ) ) ; if ( ! useRefType && ( embeddable . getInput ( ) as SavedObjectEmbeddableInput ) . savedObjectId ) { ( newInput as BookByValueInput & { savedObjectId : unknown ; } ) . savedObjectId = null ; } embeddable . updateInput ( newInput ) ; if ( useRefType ) { embeddable . getRoot ( ) . reload ( ) ; } } ; const overlay = openModal ( toMountPoint ( < CreateEditBookComponent savedObjectId = { ( embeddable . getInput ( ) as BookByReferenceInput ) . savedObjectId } attributes = { embeddable . getOutput ( ) . attributes } onSave = { ( attributes , useRefType ) => { overlay . close ( ) ; onSave ( attributes , useRefType ) ; } } />)); } , } ) ;