Skip to content

Commit

Permalink
[Workspace] Workspace assets page crashed after library_write permiss…
Browse files Browse the repository at this point in the history
…ion revoked (opensearch-project#8648)

* fix copy issue

Signed-off-by: yubonluo <[email protected]>

* Changeset file for PR opensearch-project#8648 created/updated

* fix: snapshot

Signed-off-by: SuZhou-Joe <[email protected]>

---------

Signed-off-by: yubonluo <[email protected]>
Signed-off-by: SuZhou-Joe <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
Co-authored-by: SuZhou-Joe <[email protected]>
  • Loading branch information
3 people authored and sejli committed Oct 21, 2024
1 parent 6974ebc commit f2e053f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 22 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8648.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [Workspace] Workspace assets page crashed after library_write permission revoked ([#8648](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8648))

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,13 @@ export class DuplicateResultFlyout extends React.Component<DuplicateResultFlyout
<EuiFlyout ownFocus onClose={onClose} size="s">
<EuiFlyoutHeader hasBorder>
<EuiTitle size="s">
<FormattedMessage
id="savedObjectsManagement.copyResult.title"
defaultMessage="Copy saved objects to {workspaceName}"
values={{ workspaceName }}
/>
<h2>
<FormattedMessage
id="savedObjectsManagement.copyResult.title"
defaultMessage="Copy saved objects to {workspaceName}"
values={{ workspaceName }}
/>
</h2>
</EuiTitle>
</EuiFlyoutHeader>
<EuiFlyoutBody>{this.copyResult({ failedCopies, successfulCopies })}</EuiFlyoutBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,38 @@ describe('SavedObjectsTable', () => {
});
});

it('should catch error when duplicating selected object is fail', async () => {
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 });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,13 +761,14 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
const { notifications, workspaces, useUpdatedUX } = this.props;
const workspaceClient = workspaces.client$.getValue();

const showErrorNotification = () => {
const showErrorNotification = (text?: string) => {
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) {
Expand All @@ -781,18 +782,22 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
targetWorkspace,
includeReferencesDeep
);

this.setState({
isShowingDuplicateResultFlyout: true,
failedCopies: result.success ? [] : result.errors,
successfulCopies: result.successCount > 0 ? result.successResults : [],
targetWorkspaceName,
});
if (result?.error) {
showErrorNotification(result.error);
} else {
this.setState({
isShowingDuplicateResultFlyout: true,
failedCopies: result?.errors || [],
successfulCopies: result?.successCount > 0 ? result.successResults : [],
targetWorkspaceName,
});
}
} catch (e) {
showErrorNotification();
} finally {
this.hideDuplicateModal();
await this.refreshObjects();
}
this.hideDuplicateModal();
await this.refreshObjects();
};

renderDuplicateModal() {
Expand Down

0 comments on commit f2e053f

Please sign in to comment.