From f322551c462683e75125a2bfa01af54681036bd6 Mon Sep 17 00:00:00 2001 From: maryia-lapata Date: Thu, 19 Mar 2020 15:58:04 +0300 Subject: [PATCH] Move saveWithConfirmation to a separate file --- .../saved_object/helpers/create_source.ts | 52 +---------- ...test.ts => save_with_confirmation.test.ts} | 2 +- .../helpers/save_with_confirmation.ts | 87 +++++++++++++++++++ .../public/saved_object/index.ts | 2 +- 4 files changed, 90 insertions(+), 53 deletions(-) rename src/plugins/saved_objects/public/saved_object/helpers/{create_source.test.ts => save_with_confirmation.test.ts} (98%) create mode 100644 src/plugins/saved_objects/public/saved_object/helpers/save_with_confirmation.ts diff --git a/src/plugins/saved_objects/public/saved_object/helpers/create_source.ts b/src/plugins/saved_objects/public/saved_object/helpers/create_source.ts index cdeb092940330..25ed4d527b833 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/create_source.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/create_source.ts @@ -18,12 +18,7 @@ */ import _ from 'lodash'; import { i18n } from '@kbn/i18n'; -import { - SavedObjectAttributes, - SavedObjectsCreateOptions, - OverlayStart, - SavedObjectsClientContract, -} from 'kibana/public'; +import { SavedObjectAttributes } from 'kibana/public'; import { SavedObject, SavedObjectKibanaServices } from '../../types'; import { OVERWRITE_REJECTED } from '../../constants'; import { confirmModalPromise } from './confirm_modal_promise'; @@ -85,48 +80,3 @@ export async function createSource( return await Promise.reject(err); } } - -export async function saveWithConfirmation( - source: SavedObjectAttributes, - savedObject: { - getEsType(): string; - title: string; - displayName: string; - }, - options: SavedObjectsCreateOptions, - services: { savedObjectsClient: SavedObjectsClientContract; overlays: OverlayStart } -) { - const { savedObjectsClient, overlays } = services; - try { - return await savedObjectsClient.create(savedObject.getEsType(), source, options); - } catch (err) { - // record exists, confirm overwriting - if (_.get(err, 'res.status') === 409) { - const confirmMessage = i18n.translate( - 'savedObjects.confirmModal.overwriteConfirmationMessage', - { - defaultMessage: 'Are you sure you want to overwrite {title}?', - values: { title: savedObject.title }, - } - ); - - const title = i18n.translate('savedObjects.confirmModal.overwriteTitle', { - defaultMessage: 'Overwrite {name}?', - values: { name: savedObject.displayName }, - }); - const confirmButtonText = i18n.translate('savedObjects.confirmModal.overwriteButtonLabel', { - defaultMessage: 'Overwrite', - }); - - return confirmModalPromise(confirmMessage, title, confirmButtonText, overlays) - .then(() => - savedObjectsClient.create(savedObject.getEsType(), source, { - overwrite: true, - ...options, - }) - ) - .catch(() => Promise.reject(new Error(OVERWRITE_REJECTED))); - } - return await Promise.reject(err); - } -} diff --git a/src/plugins/saved_objects/public/saved_object/helpers/create_source.test.ts b/src/plugins/saved_objects/public/saved_object/helpers/save_with_confirmation.test.ts similarity index 98% rename from src/plugins/saved_objects/public/saved_object/helpers/create_source.test.ts rename to src/plugins/saved_objects/public/saved_object/helpers/save_with_confirmation.test.ts index d09ee5cf15dcd..b05747a10ecb7 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/create_source.test.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/save_with_confirmation.test.ts @@ -19,7 +19,7 @@ import { SavedObjectAttributes, SavedObjectsCreateOptions, OverlayStart } from 'kibana/public'; import { SavedObjectsClientContract } from '../../../../../core/public'; -import { saveWithConfirmation } from './create_source'; +import { saveWithConfirmation } from './save_with_confirmation'; import * as deps from './confirm_modal_promise'; import { OVERWRITE_REJECTED } from '../../constants'; diff --git a/src/plugins/saved_objects/public/saved_object/helpers/save_with_confirmation.ts b/src/plugins/saved_objects/public/saved_object/helpers/save_with_confirmation.ts new file mode 100644 index 0000000000000..b413ea19a932d --- /dev/null +++ b/src/plugins/saved_objects/public/saved_object/helpers/save_with_confirmation.ts @@ -0,0 +1,87 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { get } from 'lodash'; +import { i18n } from '@kbn/i18n'; +import { + SavedObjectAttributes, + SavedObjectsCreateOptions, + OverlayStart, + SavedObjectsClientContract, +} from 'kibana/public'; +import { OVERWRITE_REJECTED } from '../../constants'; +import { confirmModalPromise } from './confirm_modal_promise'; + +/** + * Attempts to create the current object using the serialized source. If an object already + * exists, a warning message requests an overwrite confirmation. + * @param source - serialized version of this object what will be indexed into elasticsearch. + * @param savedObject - a simple object that contains properties title and displayName, and getEsType method + * @param options - options to pass to the saved object create method + * @param services - provides Kibana services savedObjectsClient and overlays + * @returns {Promise} - A promise that is resolved with the objects id if the object is + * successfully indexed. If the overwrite confirmation was rejected, an error is thrown with + * a confirmRejected = true parameter so that case can be handled differently than + * a create or index error. + * @resolved {SavedObject} + */ +export async function saveWithConfirmation( + source: SavedObjectAttributes, + savedObject: { + getEsType(): string; + title: string; + displayName: string; + }, + options: SavedObjectsCreateOptions, + services: { savedObjectsClient: SavedObjectsClientContract; overlays: OverlayStart } +) { + const { savedObjectsClient, overlays } = services; + try { + return await savedObjectsClient.create(savedObject.getEsType(), source, options); + } catch (err) { + // record exists, confirm overwriting + if (get(err, 'res.status') === 409) { + const confirmMessage = i18n.translate( + 'savedObjects.confirmModal.overwriteConfirmationMessage', + { + defaultMessage: 'Are you sure you want to overwrite {title}?', + values: { title: savedObject.title }, + } + ); + + const title = i18n.translate('savedObjects.confirmModal.overwriteTitle', { + defaultMessage: 'Overwrite {name}?', + values: { name: savedObject.displayName }, + }); + const confirmButtonText = i18n.translate('savedObjects.confirmModal.overwriteButtonLabel', { + defaultMessage: 'Overwrite', + }); + + return confirmModalPromise(confirmMessage, title, confirmButtonText, overlays) + .then(() => + savedObjectsClient.create(savedObject.getEsType(), source, { + overwrite: true, + ...options, + }) + ) + .catch(() => Promise.reject(new Error(OVERWRITE_REJECTED))); + } + return await Promise.reject(err); + } +} diff --git a/src/plugins/saved_objects/public/saved_object/index.ts b/src/plugins/saved_objects/public/saved_object/index.ts index 9e65eb6a6e9e7..178ffaf88f4be 100644 --- a/src/plugins/saved_objects/public/saved_object/index.ts +++ b/src/plugins/saved_objects/public/saved_object/index.ts @@ -20,5 +20,5 @@ export { createSavedObjectClass } from './saved_object'; export { SavedObjectLoader } from './saved_object_loader'; export { checkForDuplicateTitle } from './helpers/check_for_duplicate_title'; -export { saveWithConfirmation } from './helpers/create_source'; +export { saveWithConfirmation } from './helpers/save_with_confirmation'; export { isErrorNonFatal } from './helpers/save_saved_object';