1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

don't allow delete of the active cluster before setting to new active cluster (#3749)

* don't allow user to delete the active cluster before setting to new active cluster

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>

* fix lint issues

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>

* Show delete button if no more contexts available

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

Co-authored-by: Alex Andreev <alex.andreev.email@gmail.com>
Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Jim Ehrismann 2021-09-08 07:09:07 -04:00 committed by Alex Andreev
parent d238d0e8cb
commit e76f042bc0

View File

@ -20,7 +20,7 @@
*/
import styles from "./delete-cluster-dialog.module.css";
import { makeObservable, observable } from "mobx";
import { computed, makeObservable, observable } from "mobx";
import { observer } from "mobx-react";
import React from "react";
@ -122,6 +122,18 @@ export class DeleteClusterDialog extends React.Component {
this.onClose();
}
@computed get disableDelete() {
const { cluster, config } = dialogState;
const noContextsAvailable = config.contexts.filter(context => context.name !== cluster.contextName).length == 0;
const newContextNotSelected = this.newCurrentContext === "";
if (noContextsAvailable) {
return false;
}
return this.showContextSwitch && newContextNotSelected;
}
isCurrentContext() {
return dialogState.config.currentContext == dialogState.cluster.contextName;
}
@ -142,6 +154,7 @@ export class DeleteClusterDialog extends React.Component {
<div className="mt-4">
<Select
options={options}
value={this.newCurrentContext}
onChange={({ value }) => this.newCurrentContext = value}
themeName="light"
className="ml-[1px] mr-[1px]"
@ -150,6 +163,24 @@ export class DeleteClusterDialog extends React.Component {
);
}
renderDeleteMessage() {
const { cluster } = dialogState;
if (cluster.isInLocalKubeconfig()) {
return (
<div>
Delete the <b>{cluster.getMeta().name}</b> context from Lens&apos;s internal kubeconfig?
</div>
);
}
return (
<div>
Delete the <b>{cluster.getMeta().name}</b> context from <b>{cluster.kubeConfigPath}</b>?
</div>
);
}
getWarningMessage() {
const { cluster, config } = dialogState;
const contexts = config.contexts.filter(context => context.name !== cluster.contextName);
@ -207,9 +238,7 @@ export class DeleteClusterDialog extends React.Component {
onOpen={this.onOpen}
>
<div className={styles.dialogContent}>
<div>
Delete the <b>{cluster.getMeta().name}</b> context from <b>{cluster.kubeConfigPath}</b>?
</div>
{this.renderDeleteMessage()}
{this.renderWarning()}
<hr className={styles.hr}/>
{contexts.length > 0 && (
@ -225,7 +254,7 @@ export class DeleteClusterDialog extends React.Component {
</>
)}
value={this.showContextSwitch}
onChange={value => this.showContextSwitch = value}
onChange={value => this.showContextSwitch = this.isCurrentContext() ? true : value}
/>
</div>
{this.renderCurrentContextSwitch()}
@ -241,6 +270,7 @@ export class DeleteClusterDialog extends React.Component {
onClick={this.onDelete}
autoFocus accent
label="Delete Context"
disabled={this.disableDelete}
/>
</div>
</Dialog>