Skip to content

Commit

Permalink
Disable buttons in sample data cards for read-only users (#9042)
Browse files Browse the repository at this point in the history
* Disable buttons in sample data cards for read-only users

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

* Changeset file for PR #9042 created/updated

* Undisable the view data button

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

* Remove redundant code

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

---------

Signed-off-by: Kapian1234 <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 3e69fe0 commit 87011f7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 58 deletions.
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))
116 changes: 58 additions & 58 deletions src/plugins/home/public/application/components/sample_data_set_card.js
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 { i18n } from '@osd/i18n';
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);

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

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

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;

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

switch (this.props.status) {
switch (props.status) {
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 @@ export class SampleDataSetCard extends React.Component {
</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 @@ export class SampleDataSetCard extends React.Component {
<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 @@ export class SampleDataSetCard extends React.Component {
<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 @@ export class SampleDataSetCard extends React.Component {
}
};

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 (
<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

0 comments on commit 87011f7

Please sign in to comment.