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 error toasts in sample data page #8842

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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/8842.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [Workspace]Fix error toasts in sample data page ([#8842](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8842))
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class SampleDataSetCard extends React.Component {
<EuiFlexGroup gutterSize="none" justifyContent="spaceBetween">
<EuiFlexItem grow={false}>
<EuiSmallButtonEmpty
isDisabled={this.props.uninstallButtonDisabled}
isLoading={this.props.isProcessing}
onClick={this.uninstall}
color="danger"
Expand Down Expand Up @@ -240,4 +241,5 @@ SampleDataSetCard.propTypes = {
previewUrl: PropTypes.string.isRequired,
onInstall: PropTypes.func.isRequired,
onUninstall: PropTypes.func.isRequired,
uninstallButtonDisabled: PropTypes.bool,
};
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import { SampleDataSetCard, INSTALLED_STATUS, UNINSTALLED_STATUS } from './sample_data_set_card';

import { getServices } from '../opensearch_dashboards_services';
import { isAbleToUpdateUiSettings } from '../utils';

import {
listSampleDataSets,
Expand Down Expand Up @@ -114,7 +115,12 @@
}));

try {
await installSampleDataSet(id, targetSampleDataSet.defaultIndex, dataSourceId);
await installSampleDataSet(

Check warning on line 118 in src/plugins/home/public/application/components/sample_data_set_cards.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/home/public/application/components/sample_data_set_cards.js#L118

Added line #L118 was not covered by tests
id,
targetSampleDataSet.defaultIndex,
dataSourceId,
isAbleToUpdateUiSettings(getServices())
);
} catch (fetchError) {
if (this._isMounted) {
this.setState((prevState) => ({
Expand Down Expand Up @@ -162,7 +168,12 @@
}));

try {
await uninstallSampleDataSet(id, targetSampleDataSet.defaultIndex, dataSourceId);
await uninstallSampleDataSet(

Check warning on line 171 in src/plugins/home/public/application/components/sample_data_set_cards.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/home/public/application/components/sample_data_set_cards.js#L171

Added line #L171 was not covered by tests
id,
targetSampleDataSet.defaultIndex,
dataSourceId,
isAbleToUpdateUiSettings(getServices())
);
} catch (fetchError) {
if (this._isMounted) {
this.setState((prevState) => ({
Expand Down Expand Up @@ -216,6 +227,8 @@
};

render() {
const ableToUpdateUiSettings = isAbleToUpdateUiSettings(getServices());

return (
<EuiFlexGrid
columns={3}
Expand Down Expand Up @@ -243,6 +256,7 @@
dataSourceId={this.state.dataSourceId}
isDataSourceEnabled={this.props.isDataSourceEnabled}
isLocalClusterHidden={this.props.isLocalClusterHidden}
uninstallButtonDisabled={!ableToUpdateUiSettings && !!sampleDataSet.defaultIndex}
/>
</EuiFlexItem>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
SavedObjectsClientContract,
IUiSettingsClient,
ApplicationStart,
WorkspacesStart,
} from 'opensearch-dashboards/public';
import { UiStatsMetricType } from '@osd/analytics';
import { TelemetryPluginStart } from '../../../telemetry/public';
Expand Down Expand Up @@ -77,6 +78,7 @@ export interface HomeOpenSearchDashboardsServices {
};
dataSource?: DataSourcePluginStart;
sectionTypes: SectionTypeService;
workspaces: WorkspacesStart;
}

let services: HomeOpenSearchDashboardsServices | null = null;
Expand Down
17 changes: 14 additions & 3 deletions src/plugins/home/public/application/sample_data_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,35 @@
return await getServices().http.get(sampleDataUrl, { query });
}

export async function installSampleDataSet(id, sampleDataDefaultIndex, dataSourceId) {
export async function installSampleDataSet(
id,
sampleDataDefaultIndex,
dataSourceId,
ableToUpdateUiSettings
) {
const query = buildQuery(dataSourceId);
await getServices().http.post(`${sampleDataUrl}/${id}`, { query });

if (getServices().uiSettings.isDefault('defaultIndex')) {
if (ableToUpdateUiSettings && getServices().uiSettings.isDefault('defaultIndex')) {

Check warning on line 53 in src/plugins/home/public/application/sample_data_client.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/home/public/application/sample_data_client.js#L53

Added line #L53 was not covered by tests
getServices().uiSettings.set('defaultIndex', sampleDataDefaultIndex);
}

clearIndexPatternsCache();
}

export async function uninstallSampleDataSet(id, sampleDataDefaultIndex, dataSourceId) {
export async function uninstallSampleDataSet(
id,
sampleDataDefaultIndex,
dataSourceId,
ableToUpdateUiSettings
) {
const query = buildQuery(dataSourceId);
await getServices().http.delete(`${sampleDataUrl}/${id}`, { query });

const uiSettings = getServices().uiSettings;

if (
ableToUpdateUiSettings &&
!uiSettings.isDefault('defaultIndex') &&
uiSettings.get('defaultIndex') === sampleDataDefaultIndex
) {
Expand Down
106 changes: 106 additions & 0 deletions src/plugins/home/public/application/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { isAbleToUpdateUiSettings } from './utils';

describe('isAbleToUpdateUiSettings', () => {
it('should return true when workspace is not enabled and permission is not enabled', () => {
const application = {
capabilities: {
workspaces: {
enabled: false,
permissionEnabled: false,
},
},
};
const workspaces = null;

const result = isAbleToUpdateUiSettings({ workspaces, application });

expect(result).toBe(true);
});

it('should return true when workspace is enabled but permission is not enabled', () => {
const application = {
capabilities: {
workspaces: {
enabled: true,
permissionEnabled: false,
},
},
};
const workspaces = null;

const result = isAbleToUpdateUiSettings({ workspaces, application });

expect(result).toBe(true);
});

it('should return true when current workspace owner', () => {
const application = {
capabilities: {
workspaces: {
enabled: true,
permissionEnabled: true,
},
},
};
const workspaces = {
currentWorkspace$: {
getValue: () => ({ owner: true }),
},
};

const result = isAbleToUpdateUiSettings({ workspaces, application });

expect(result).toBe(true);
});

it('should return true when user is dashboards admin', () => {
const application = {
capabilities: {
workspaces: {
enabled: true,
permissionEnabled: true,
},
dashboards: {
isDashboardAdmin: true,
},
},
};
const workspaces = {
currentWorkspace$: {
getValue: () => ({ owner: false }),
},
};

const result = isAbleToUpdateUiSettings({ workspaces, application });

expect(result).toBe(true);
});

it('should return false when user is not current workspace owner and not dashboards admin', () => {
const application = {
capabilities: {
workspaces: {
enabled: true,
permissionEnabled: true,
},
dashboards: {
isDashboardAdmin: false,
},
},
};
const workspaces = {
currentWorkspace$: {
getValue: () => ({ owner: false }),
},
};

const result = isAbleToUpdateUiSettings({ workspaces, application });

expect(result).toBe(false);
});
});
35 changes: 35 additions & 0 deletions src/plugins/home/public/application/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { HomeOpenSearchDashboardsServices } from './opensearch_dashboards_services';

export const isAbleToUpdateUiSettings = ({
workspaces,
application,
}: Pick<HomeOpenSearchDashboardsServices, 'workspaces' | 'application'>) => {
// Able to update ui settings when workspace not enabled and permission not enabled
if (
!application.capabilities ||
!application.capabilities.workspaces ||
!application.capabilities.workspaces.enabled ||
!application.capabilities.workspaces.permissionEnabled
) {
return true;
}

// Able to update ui settings when is current workspace owner
if (workspaces) {
const currentWorkspace = workspaces.currentWorkspace$.getValue();
if (currentWorkspace && currentWorkspace.owner) {
return true;
}
}

// Able to update ui settings when is dashboards admin
return (
application.capabilities.dashboards &&
application.capabilities.dashboards.isDashboardAdmin !== false
);
};
1 change: 1 addition & 0 deletions src/plugins/home/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export class HomePublicPlugin
injectedMetadata: coreStart.injectedMetadata,
dataSource,
sectionTypes: this.sectionTypeService,
workspaces: coreStart.workspaces,
...homeOpenSearchDashboardsServices,
});
};
Expand Down
Loading