diff --git a/src/core/public/workspace/types.ts b/src/core/public/workspace/types.ts index d3afb0d4833f..16ca3ec97a74 100644 --- a/src/core/public/workspace/types.ts +++ b/src/core/public/workspace/types.ts @@ -4,6 +4,7 @@ */ import { WorkspaceAttribute } from '../../types'; +import { SavedObjectsImportResponse } from '../saved_objects'; export type WorkspaceObject = WorkspaceAttribute & { readonly?: boolean; owner?: boolean }; @@ -35,9 +36,13 @@ export interface IWorkspaceClient { * @param {Array<{ id: string; type: string }>} objects * @param {string} targetWorkspace * @param {boolean} includeReferencesDeep - * @returns {Promise>} result for this operation + * @returns {Promise} result for this operation */ - copy(objects: any[], targetWorkspace: string, includeReferencesDeep?: boolean): Promise; + copy( + objects: Array<{ id: string; type: string }>, + targetWorkspace: string, + includeReferencesDeep?: boolean + ): Promise; /** * Associates a list of objects with the given workspace ID. diff --git a/src/plugins/saved_objects_management/public/management_section/objects_table/components/duplicate_result_flyout.tsx b/src/plugins/saved_objects_management/public/management_section/objects_table/components/duplicate_result_flyout.tsx index dc20c10737ca..5da3c8c9a987 100644 --- a/src/plugins/saved_objects_management/public/management_section/objects_table/components/duplicate_result_flyout.tsx +++ b/src/plugins/saved_objects_management/public/management_section/objects_table/components/duplicate_result_flyout.tsx @@ -55,8 +55,7 @@ export interface DuplicateResultFlyoutProps { savedObjects: DuplicateObject[], includeReferencesDeep: boolean, targetWorkspace: string, - targetWorkspaceName: string, - isSolveErrorsOperation: boolean + targetWorkspaceName: string ) => Promise; targetWorkspace: string; useUpdatedUX: boolean; @@ -355,7 +354,7 @@ export class DuplicateResultFlyout extends React.Component { }); }); - it('should catch error when duplicating selected object is failed', async () => { - const component = shallowRender({ applications, workspaces }); - component.setState({ isShowingDuplicateModal: true }); - - const mockCopy = jest.fn().mockResolvedValue({ error: 'error' }); - workspaces.client$.next({ copy: mockCopy }); - const client = workspaces.client$.getValue(); - - // Ensure all promises resolve - await new Promise((resolve) => process.nextTick(resolve)); - // Ensure the state changes are reflected - component.update(); - - await component.instance().onDuplicate(mockSelectedSavedObjects, false, 'workspace2', 'bar'); - - expect(client?.copy).toHaveBeenCalledWith( - [ - { id: '1', type: 'dashboard' }, - { id: '2', type: 'dashboard' }, - ], - 'workspace2', - false - ); - component.update(); - - expect(notifications.toasts.addDanger).toHaveBeenCalledWith({ - title: 'Unable to copy 2 saved objects.', - text: 'error', - }); - }); - it('should show error toast when copy is fail', async () => { const component = shallowRender({ applications, workspaces }); component.setState({ isShowingDuplicateModal: true }); diff --git a/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx b/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx index 8ed063e93555..0d9c04e17e84 100644 --- a/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx +++ b/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx @@ -759,20 +759,18 @@ export class SavedObjectsTable extends Component { const { notifications, workspaces, useUpdatedUX } = this.props; const workspaceClient = workspaces.client$.getValue(); - const showErrorNotification = (text?: string) => { + const showErrorNotification = () => { notifications.toasts.addDanger({ title: i18n.translate('savedObjectsManagement.objectsTable.duplicate.dangerNotification', { defaultMessage: 'Unable to copy {useUpdatedUX, select, true {{errorCount, plural, one {# asset} other {# assets}}} other {{errorCount, plural, one {# saved object} other {# saved objects}}}}.', values: { errorCount: savedObjects.length, useUpdatedUX }, }), - ...(text && { text }), }); }; if (!workspaceClient) { @@ -786,25 +784,13 @@ export class SavedObjectsTable extends Component 0 ? result.successResults : [], - targetWorkspaceName, - }); - } else { - this.setState({ - isShowingDuplicateResultFlyout: true, - failedCopies: result?.errors || [], - successfulCopies: result?.successCount > 0 ? result.successResults : [], - targetWorkspace, - targetWorkspaceName, - }); - } - } + this.setState({ + isShowingDuplicateResultFlyout: true, + failedCopies: result?.errors || [], + successfulCopies: result?.successResults || [], + targetWorkspace, + targetWorkspaceName, + }); } catch (e) { showErrorNotification(); } finally { diff --git a/src/plugins/workspace/public/workspace_client.test.ts b/src/plugins/workspace/public/workspace_client.test.ts index 9cccf3597391..561ed72236eb 100644 --- a/src/plugins/workspace/public/workspace_client.test.ts +++ b/src/plugins/workspace/public/workspace_client.test.ts @@ -216,14 +216,14 @@ describe('#WorkspaceClient', () => { const { workspaceClient, httpSetupMock } = getWorkspaceClient(); httpSetupMock.fetch.mockResolvedValue({ success: true, - result: {}, + successCount: 1, }); const body = JSON.stringify({ - objects: [{ id: 1, type: 'url' }], + objects: [{ id: 'url_id', type: 'url' }], targetWorkspace: 'workspace-1', includeReferencesDeep: false, }); - await workspaceClient.copy([{ id: 1, type: 'url' }], 'workspace-1', false); + await workspaceClient.copy([{ id: 'url_id', type: 'url' }], 'workspace-1', false); expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/_duplicate_saved_objects', { body, method: 'POST', diff --git a/src/plugins/workspace/public/workspace_client.ts b/src/plugins/workspace/public/workspace_client.ts index 262cdeafd209..29a081550837 100644 --- a/src/plugins/workspace/public/workspace_client.ts +++ b/src/plugins/workspace/public/workspace_client.ts @@ -12,6 +12,7 @@ import { WorkspacesSetup, IWorkspaceClient, IWorkspaceResponse as IResponse, + SavedObjectsImportResponse, } from '../../../core/public'; import { WorkspacePermissionMode } from '../common/constants'; import { SavedObjectPermissions, WorkspaceAttributeWithPermission } from '../../../core/types'; @@ -328,13 +329,14 @@ export class WorkspaceClient implements IWorkspaceClient { * @param {Array<{ id: string; type: string }>} objects * @param {string} targetWorkspace * @param {boolean} includeReferencesDeep - * @returns {Promise>} result for this operation + * @returns {Promise} result for this operation */ public async copy( objects: Array<{ id: string; type: string }>, targetWorkspace: string, includeReferencesDeep: boolean = true - ): Promise> { + ): Promise { + // throw 'err'; const path = this.getPath('_duplicate_saved_objects'); const body = { objects, @@ -342,7 +344,7 @@ export class WorkspaceClient implements IWorkspaceClient { includeReferencesDeep, }; - const result = await this.safeFetch(path, { + const result = await this.http.fetch(path, { method: 'POST', body: JSON.stringify(body), });