|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import React from 'react'; |
| 20 | +import { OverlayStart, SavedObjectsClientContract } from 'kibana/public'; |
| 21 | +import { i18n } from '@kbn/i18n'; |
| 22 | +import { NoteSavedObjectAttributes, NOTE_SAVED_OBJECT } from '../common'; |
| 23 | +import { createAction } from '../../../../src/plugins/ui_actions/public'; |
| 24 | +import { toMountPoint } from '../../../../src/plugins/kibana_react/public'; |
| 25 | +import { ViewMode } from '../../../../src/plugins/embeddable/public'; |
| 26 | +import { CreateEditNoteComponent } from './create_edit_note_component'; |
| 27 | +import { NoteEmbeddable, NOTE_EMBEDDABLE } from './note_embeddable'; |
| 28 | + |
| 29 | +interface StartServices { |
| 30 | + openModal: OverlayStart['openModal']; |
| 31 | + savedObjectsClient: SavedObjectsClientContract; |
| 32 | +} |
| 33 | + |
| 34 | +interface ActionContext { |
| 35 | + embeddable: NoteEmbeddable; |
| 36 | +} |
| 37 | + |
| 38 | +export const ACTION_EDIT_NOTE = 'ACTION_EDIT_NOTE'; |
| 39 | + |
| 40 | +export const createEditNoteAction = (getStartServices: () => Promise<StartServices>) => |
| 41 | + createAction({ |
| 42 | + getDisplayName: () => |
| 43 | + i18n.translate('embeddableExamples.note.edit', { defaultMessage: 'Edit' }), |
| 44 | + type: ACTION_EDIT_NOTE, |
| 45 | + isCompatible: async ({ embeddable }: ActionContext) => { |
| 46 | + return ( |
| 47 | + embeddable.type === NOTE_EMBEDDABLE && embeddable.getInput().viewMode === ViewMode.EDIT |
| 48 | + ); |
| 49 | + }, |
| 50 | + execute: async ({ embeddable }: ActionContext) => { |
| 51 | + const { openModal, savedObjectsClient } = await getStartServices(); |
| 52 | + const onSave = async (attributes: NoteSavedObjectAttributes, includeInLibrary: boolean) => { |
| 53 | + if (includeInLibrary) { |
| 54 | + if (embeddable.getInput().savedObjectId) { |
| 55 | + await savedObjectsClient.update( |
| 56 | + NOTE_SAVED_OBJECT, |
| 57 | + embeddable.getInput().savedObjectId!, |
| 58 | + attributes |
| 59 | + ); |
| 60 | + embeddable.updateInput({ attributes: undefined }); |
| 61 | + embeddable.reload(); |
| 62 | + } else { |
| 63 | + const savedItem = await savedObjectsClient.create(NOTE_SAVED_OBJECT, attributes); |
| 64 | + embeddable.updateInput({ savedObjectId: savedItem.id }); |
| 65 | + } |
| 66 | + } else { |
| 67 | + embeddable.updateInput({ attributes }); |
| 68 | + } |
| 69 | + }; |
| 70 | + const overlay = openModal( |
| 71 | + toMountPoint( |
| 72 | + <CreateEditNoteComponent |
| 73 | + savedObjectId={embeddable.getInput().savedObjectId} |
| 74 | + attributes={embeddable.getInput().attributes ?? embeddable.getOutput().savedAttributes} |
| 75 | + onSave={(attributes: NoteSavedObjectAttributes, includeInLibrary: boolean) => { |
| 76 | + overlay.close(); |
| 77 | + onSave(attributes, includeInLibrary); |
| 78 | + }} |
| 79 | + /> |
| 80 | + ) |
| 81 | + ); |
| 82 | + }, |
| 83 | + }); |
0 commit comments