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] Disable buttons in sample data cards for read-only users #9127

Merged
merged 1 commit into from
Dec 27, 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/9042.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Disable buttons in sample data cards for read-only users ([#9042](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9042))
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/

import React from 'react';
import useObservable from 'react-use/lib/useObservable';
import PropTypes from 'prop-types';
import {
EuiCard,
Expand All @@ -46,62 +47,63 @@
import { FormattedMessage } from '@osd/i18n/react';

import { SampleDataViewDataButton } from './sample_data_view_data_button';

export class SampleDataSetCard extends React.Component {
isInstalled = () => {
if (this.props.status === INSTALLED_STATUS) {
return true;
}

return false;
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public';

export const SampleDataSetCard = (props) => {
const {
services: { workspaces },
} = useOpenSearchDashboards();
const isInstalled = props.status === INSTALLED_STATUS;
const currentWorkspace = useObservable(workspaces.currentWorkspace$);
const isReadOnly = !!(currentWorkspace && currentWorkspace.readonly);

Check warning on line 58 in src/plugins/home/public/application/components/sample_data_set_card.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/home/public/application/components/sample_data_set_card.js#L55-L58

Added lines #L55 - L58 were not covered by tests

const install = () => {
props.onInstall(props.id, props.dataSourceId);

Check warning on line 61 in src/plugins/home/public/application/components/sample_data_set_card.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/home/public/application/components/sample_data_set_card.js#L60-L61

Added lines #L60 - L61 were not covered by tests
};

install = () => {
this.props.onInstall(this.props.id, this.props.dataSourceId);
const uninstall = () => {
props.onUninstall(props.id, props.dataSourceId);

Check warning on line 65 in src/plugins/home/public/application/components/sample_data_set_card.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/home/public/application/components/sample_data_set_card.js#L64-L65

Added lines #L64 - L65 were not covered by tests
};

uninstall = () => {
this.props.onUninstall(this.props.id, this.props.dataSourceId);
};

renderBtn = () => {
const dataSourceEnabled = this.props.isDataSourceEnabled;
const hideLocalCluster = this.props.isLocalClusterHidden;
const dataSourceId = this.props.dataSourceId;
const renderBtn = () => {
const dataSourceEnabled = props.isDataSourceEnabled;
const hideLocalCluster = props.isLocalClusterHidden;
const dataSourceId = props.dataSourceId;

Check warning on line 71 in src/plugins/home/public/application/components/sample_data_set_card.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/home/public/application/components/sample_data_set_card.js#L68-L71

Added lines #L68 - L71 were not covered by tests

let buttonDisabled = false;
if (dataSourceEnabled && hideLocalCluster) {
buttonDisabled = dataSourceId === undefined;
}

switch (this.props.status) {
switch (props.status) {

Check warning on line 78 in src/plugins/home/public/application/components/sample_data_set_card.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/home/public/application/components/sample_data_set_card.js#L78

Added line #L78 was not covered by tests
case INSTALLED_STATUS:
return (
<EuiFlexGroup gutterSize="none" justifyContent="spaceBetween">
<EuiFlexItem grow={false}>
<EuiSmallButtonEmpty
isLoading={this.props.isProcessing}
onClick={this.uninstall}
isDisabled={isReadOnly}
isLoading={props.isProcessing}
onClick={uninstall}
color="danger"
data-test-subj={`removeSampleDataSet${this.props.id}`}
data-test-subj={`removeSampleDataSet${props.id}`}
flush="left"
aria-label={
this.props.isProcessing
props.isProcessing
? i18n.translate('home.sampleDataSetCard.removingButtonAriaLabel', {
defaultMessage: 'Removing {datasetName}',
values: {
datasetName: this.props.name,
datasetName: props.name,
},
})
: i18n.translate('home.sampleDataSetCard.removeButtonAriaLabel', {
defaultMessage: 'Remove {datasetName}',
values: {
datasetName: this.props.name,
datasetName: props.name,
},
})
}
>
{this.props.isProcessing ? (
{props.isProcessing ? (
<FormattedMessage
id="home.sampleDataSetCard.removingButtonLabel"
defaultMessage="Removing"
Expand All @@ -116,11 +118,11 @@
</EuiFlexItem>
<EuiFlexItem grow={false}>
<SampleDataViewDataButton
id={this.props.id}
dataSourceId={this.props.dataSourceId}
name={this.props.name}
overviewDashboard={this.props.overviewDashboard}
appLinks={this.props.appLinks}
id={props.id}
dataSourceId={props.dataSourceId}
name={props.name}
overviewDashboard={props.overviewDashboard}
appLinks={props.appLinks}
/>
</EuiFlexItem>
</EuiFlexGroup>
Expand All @@ -131,27 +133,27 @@
<EuiFlexGroup justifyContent="flexEnd">
<EuiFlexItem grow={false}>
<EuiSmallButton
isDisabled={buttonDisabled}
isLoading={this.props.isProcessing}
onClick={this.install}
data-test-subj={`addSampleDataSet${this.props.id}`}
isDisabled={buttonDisabled || isReadOnly}
isLoading={props.isProcessing}
onClick={install}
data-test-subj={`addSampleDataSet${props.id}`}
aria-label={
this.props.isProcessing
props.isProcessing
? i18n.translate('home.sampleDataSetCard.addingButtonAriaLabel', {
defaultMessage: 'Adding {datasetName}',
values: {
datasetName: this.props.name,
datasetName: props.name,
},
})
: i18n.translate('home.sampleDataSetCard.addButtonAriaLabel', {
defaultMessage: 'Add {datasetName}',
values: {
datasetName: this.props.name,
datasetName: props.name,
},
})
}
>
{this.props.isProcessing ? (
{props.isProcessing ? (
<FormattedMessage
id="home.sampleDataSetCard.addingButtonLabel"
defaultMessage="Adding"
Expand All @@ -178,18 +180,18 @@
<FormattedMessage
id="home.sampleDataSetCard.default.unableToVerifyErrorMessage"
defaultMessage="Unable to verify dataset status, error: {statusMsg}"
values={{ statusMsg: this.props.statusMsg }}
values={{ statusMsg: props.statusMsg }}
/>
</p>
}
>
<EuiSmallButton
isDisabled={buttonDisabled}
data-test-subj={`addSampleDataSet${this.props.id}`}
isDisabled={buttonDisabled || isReadOnly}
data-test-subj={`addSampleDataSet${props.id}`}
aria-label={i18n.translate('home.sampleDataSetCard.default.addButtonAriaLabel', {
defaultMessage: 'Add {datasetName}',
values: {
datasetName: this.props.name,
datasetName: props.name,
},
})}
>
Expand All @@ -206,21 +208,19 @@
}
};

render() {
return (
<EuiCard
textAlign="left"
className="homSampleDataSetCard"
image={this.props.previewUrl}
title={this.props.name}
description={this.props.description}
betaBadgeLabel={this.isInstalled() ? 'INSTALLED' : null}
footer={this.renderBtn()}
data-test-subj={`sampleDataSetCard${this.props.id}`}
/>
);
}
}
return (

Check warning on line 211 in src/plugins/home/public/application/components/sample_data_set_card.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/home/public/application/components/sample_data_set_card.js#L211

Added line #L211 was not covered by tests
<EuiCard
textAlign="left"
className="homSampleDataSetCard"
image={props.previewUrl}
title={props.name}
description={props.description}
betaBadgeLabel={isInstalled ? 'INSTALLED' : null}
footer={renderBtn()}
data-test-subj={`sampleDataSetCard${props.id}`}
/>
);
};

SampleDataSetCard.propTypes = {
id: PropTypes.string.isRequired,
Expand Down
Loading