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

[MD] Add delete an existing credential page button #2067

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import React from 'react';
import { FormattedMessage } from '@osd/i18n/react';

import { withRouter, RouteComponentProps } from 'react-router-dom';
import {
EuiHorizontalRule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import React from 'react';
import { FormattedMessage } from '@osd/i18n/react';

import { withRouter, RouteComponentProps } from 'react-router-dom';
import {
EuiHorizontalRule,
Expand All @@ -20,13 +19,18 @@ import {
EuiButton,
EuiPageContent,
EuiFieldPassword,
EuiFlexItem,
EuiToolTip,
EuiButtonIcon,
EuiConfirmModal,
} from '@elastic/eui';
import { DocLinksStart } from 'src/core/public';
import { getCreateBreadcrumbs } from '../breadcrumbs';
import { CredentialManagmentContextValue } from '../../types';
// TODO: Add Header https://github.com/opensearch-project/OpenSearch-Dashboards/issues/2051
import { context as contextType } from '../../../../opensearch_dashboards_react/public';
import { CredentialEditPageItem } from '../types';
import * as localizedContent from '../text_content/text_content';

interface EditCredentialState {
credentialName: string;
Expand All @@ -36,6 +40,7 @@ interface EditCredentialState {
dual: boolean;
toasts: EuiGlobalToastListToast[];
docLinks: DocLinksStart;
isVisible: boolean;
yibow98 marked this conversation as resolved.
Show resolved Hide resolved
}

export interface EditCredentialProps extends RouteComponentProps {
Expand All @@ -61,17 +66,89 @@ export class EditCredentialComponent extends React.Component<
dual: true,
toasts: [],
docLinks: context.services.docLinks,
isVisible: false,
};
}

confirmDelete = async () => {
const { savedObjects } = this.context.services;
try {
await savedObjects.client.delete('credential', this.props.credential.id);
this.props.history.push('');
} catch (e) {
const deleteCredentialFailMsg = (
<FormattedMessage
id="credentialManagement.editCredential.deleteCredentialFailMsg"
defaultMessage="The credential delete failed with some errors. Please try it again.'"
/>
);
this.setState((prevState) => ({
toasts: prevState.toasts.concat([
{
title: deleteCredentialFailMsg,
id: deleteCredentialFailMsg.props.id,
color: 'warning',
iconType: 'alert',
},
]),
}));
}
};

delelteButtonRender() {
return (
<>
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'end',
}}
Comment on lines +102 to +106
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor suggestion: can we seperate style from component?

>
<EuiFlexItem>
<EuiToolTip content={localizedContent.deleteCredentialButtonDescription}>
<EuiButtonIcon
color="danger"
onClick={this.removeCredential}
iconType="trash"
aria-label={localizedContent.deleteCredentialButtonDescription}
/>
</EuiToolTip>
</EuiFlexItem>
</div>

{this.state.isVisible ? (
<EuiConfirmModal
title={localizedContent.deleteButtonOnConfirmText}
onCancel={() => {
this.setState({ isVisible: false });
}}
onConfirm={this.confirmDelete}
cancelButtonText={localizedContent.cancelButtonOnDeleteCancelText}
confirmButtonText={localizedContent.confirmButtonOnDeleteComfirmText}
defaultFocusedButton="confirm"
>
<p>{localizedContent.deleteCredentialDescribeMsg}</p>
<p>{localizedContent.deleteCredentialConfirmMsg}</p>
<p>{localizedContent.deleteCredentialWarnMsg}</p>
</EuiConfirmModal>
) : null}
</>
);
}
// TODO: Add rendering spanner https://github.com/opensearch-project/OpenSearch-Dashboards/issues/2050
renderContent() {
const options = [
{ value: 'username_password_credential', text: 'Username and Password Credential' },
{
value: 'username_password_credential',
text: 'Username and Password Credential',
},
{ value: 'no_auth', text: 'No Auth' },
];

return (
<EuiPageContent>
{this.delelteButtonRender()}
<EuiHorizontalRule />
<EuiForm component="form">
<EuiDescribedFormGroup
Expand Down Expand Up @@ -165,10 +242,13 @@ export class EditCredentialComponent extends React.Component<
);
}

removeCredential = async () => {
this.setState({ isVisible: true });
};

updateCredential = async () => {
const { savedObjects } = this.context.services;
try {
// TODO: Add rendering spanner https://github.com/opensearch-project/OpenSearch-Dashboards/issues/2050
await savedObjects.client.update('credential', this.props.credential.id, {
title: this.state.credentialName,
credentialMaterials: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { i18n } from '@osd/i18n';

export const deleteCredentialDescribeMsg = i18n.translate(
'credentialManagement.textContent.deleteCredentialDescribeMsg',
{
defaultMessage:
'This will also delete their credential materials. All data sources using this credential will be invalid for access, and they must choose new credentials.',
}
);

export const deleteCredentialConfirmMsg = i18n.translate(
'credentialManagement.textContent.deleteCredentialConfirmMsg',
{
defaultMessage: 'To confirm deletion, click delete button.',
}
);

export const deleteCredentialWarnMsg = i18n.translate(
'credentialManagement.textContent.deleteCredentialWarnMsg',
{
defaultMessage: 'Note: this action is irrevocable!',
}
);

export const deleteButtonOnConfirmText = i18n.translate(
'credentialManagement.textContent.deleteButtonOnConfirmText',
{
defaultMessage: 'Delete credential permanently?',
}
);

export const deleteCredentialButtonDescription = i18n.translate(
'credentialManagement.textContent.deleteCredentialButtonDescription',
{
defaultMessage: 'Remove this credential',
}
);

export const cancelButtonOnDeleteCancelText = i18n.translate(
'credentialManagement.textContent.cancelButtonOnDeleteCancelText',
{
defaultMessage: 'Cancel',
}
);

export const confirmButtonOnDeleteComfirmText = i18n.translate(
'credentialManagement.textContent.confirmButtonOnDeleteComfirmText',
{
defaultMessage: 'Delete',
}
);