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

[Workspace] Fix: Show a error toast when workspace read only user delete saved objects #6756

Merged
merged 13 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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))
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,42 @@ describe('SavedObjectsTable', () => {
);
expect(component.state('selectedSavedObjects').length).toBe(0);
});

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().delete();

expect(notifications.toasts.addDanger).toHaveBeenCalled();
yubonluo marked this conversation as resolved.
Show resolved Hide resolved
});
});

describe('workspace filter', () => {
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) {
yubonluo marked this conversation as resolved.
Show resolved Hide resolved
notifications.toasts.addDanger({
yubonluo marked this conversation as resolved.
Show resolved Hide resolved
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