Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] [Workspace] Fix: Show a error toast when workspace read only user delete saved objects #6783

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/6756.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Show error toast when fail to delete saved objects ([#6756](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6756))

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 @@ -596,6 +596,9 @@ describe('SavedObjectsTable', () => {

// Set some as selected
component.instance().onSelectionChanged(mockSelectedSavedObjects);
await component.instance().onDelete();
component.update();
expect(component.state('isShowingDeleteConfirmModal')).toBe(true);

await component.instance().delete();

Expand All @@ -612,6 +615,53 @@ describe('SavedObjectsTable', () => {
{ force: true }
);
expect(component.state('selectedSavedObjects').length).toBe(0);
expect(notifications.toasts.addDanger).not.toHaveBeenCalled();
expect(component.state('isDeleting')).toBe(false);
expect(component.state('isShowingDeleteConfirmModal')).toBe(false);
});

it('should show error toast when failing to delete saved objects', async () => {
const mockSelectedSavedObjects = [
{ id: '1', type: 'index-pattern' },
] as SavedObjectWithMetadata[];

const mockSavedObjects = mockSelectedSavedObjects.map((obj) => ({
id: obj.id,
type: obj.type,
source: {},
}));

const mockSavedObjectsClient = {
...defaultProps.savedObjectsClient,
bulkGet: jest.fn().mockImplementation(() => ({
savedObjects: mockSavedObjects,
})),
delete: jest.fn().mockImplementation(() => {
throw new Error('Unable to delete saved objects');
}),
};

const component = shallowRender({ savedObjectsClient: mockSavedObjectsClient });

// Ensure all promises resolve
await new Promise((resolve) => process.nextTick(resolve));
// Ensure the state changes are reflected
component.update();

// Set some as selected
component.instance().onSelectionChanged(mockSelectedSavedObjects);
await component.instance().onDelete();
component.update();
expect(component.state('isShowingDeleteConfirmModal')).toBe(true);
expect(component.find('EuiConfirmModal')).toMatchSnapshot();

await component.instance().delete();
component.update();
expect(notifications.toasts.addDanger).toHaveBeenCalled();
// If user fail to delete the saved objects, the delete modal will continue to display
expect(component.state('isShowingDeleteConfirmModal')).toBe(true);
expect(component.find('EuiConfirmModal')).toMatchSnapshot();
expect(component.state('isDeleting')).toBe(false);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
};

delete = async () => {
const { savedObjectsClient } = this.props;
const { savedObjectsClient, notifications } = this.props;
const { selectedSavedObjects, isDeleting } = this.state;

if (isDeleting) {
Expand All @@ -599,30 +599,38 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
this.setState({ isDeleting: true });

const indexPatterns = selectedSavedObjects.filter((object) => object.type === 'index-pattern');
if (indexPatterns.length) {
await this.props.indexPatterns.clearCache();
}

const objects = await savedObjectsClient.bulkGet(selectedSavedObjects);
const deletes = objects.savedObjects.map((object) =>
savedObjectsClient.delete(object.type, object.id, { force: true })
);
await Promise.all(deletes);

// Unset this
this.setState({
selectedSavedObjects: [],
});
try {
if (indexPatterns.length) {
await this.props.indexPatterns.clearCache();
}
const objects = await savedObjectsClient.bulkGet(selectedSavedObjects);
const deletes = objects.savedObjects.map((object) =>
savedObjectsClient.delete(object.type, object.id, { force: true })
);
await Promise.all(deletes);
// Unset this
this.setState({
selectedSavedObjects: [],
});
// Fetching all data
await this.fetchSavedObjects();
await this.fetchCounts();

// Fetching all data
await this.fetchSavedObjects();
await this.fetchCounts();
// Allow the user to interact with the table once the saved objects have been re-fetched.
// If the user fails to delete the saved objects, the delete modal will continue to display.
this.setState({ isShowingDeleteConfirmModal: false });
} catch (error) {
notifications.toasts.addDanger({
title: i18n.translate(
'savedObjectsManagement.objectsTable.unableDeleteSavedObjectsNotificationMessage',
{ defaultMessage: 'Unable to delete saved objects' }
),
text: `${error}`,
});
}

// Allow the user to interact with the table once the saved objects have been re-fetched.
this.setState({
isShowingDeleteConfirmModal: false,
isDeleting: false,
});
this.setState({ isDeleting: false });
};

getRelationships = async (type: string, id: string) => {
Expand Down
Loading