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

Fix: editing resource is broken when another resource is being editing (#2364)

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2021-03-23 14:11:11 +02:00 committed by GitHub
parent b066fb3527
commit e70ac87c52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,7 @@
import "./edit-resource.scss"; import "./edit-resource.scss";
import React from "react"; import React from "react";
import { observable, when } from "mobx"; import { action, computed, observable } from "mobx";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
import jsYaml from "js-yaml"; import jsYaml from "js-yaml";
import { IDockTab } from "./dock.store"; import { IDockTab } from "./dock.store";
@ -11,6 +11,7 @@ import { InfoPanel } from "./info-panel";
import { Badge } from "../badge"; import { Badge } from "../badge";
import { EditorPanel } from "./editor-panel"; import { EditorPanel } from "./editor-panel";
import { Spinner } from "../spinner"; import { Spinner } from "../spinner";
import { KubeObject } from "../../api/kube-object";
interface Props { interface Props {
className?: string; className?: string;
@ -21,14 +22,6 @@ interface Props {
export class EditResource extends React.Component<Props> { export class EditResource extends React.Component<Props> {
@observable error = ""; @observable error = "";
async componentDidMount() {
await when(() => this.isReady);
if (!this.tabData.draft) {
this.saveDraft(this.resource); // make initial dump to editor
}
}
get tabId() { get tabId() {
return this.props.tab.id; return this.props.tab.id;
} }
@ -37,20 +30,32 @@ export class EditResource extends React.Component<Props> {
return editResourceStore.isReady(this.tabId); return editResourceStore.isReady(this.tabId);
} }
get tabData() { get resource(): KubeObject | undefined {
return editResourceStore.getData(this.tabId);
}
get resource() {
return editResourceStore.getResource(this.tabId); return editResourceStore.getResource(this.tabId);
} }
saveDraft(draft: string | object) { @computed get draft(): string {
if (!this.isReady) {
return ""; // wait until tab's data and kube-object resource are loaded
}
const { draft } = editResourceStore.getData(this.tabId);
if (typeof draft === "string") {
return draft;
}
return jsYaml.dump(this.resource); // dump resource first time
}
@action
saveDraft(draft: string | KubeObject) {
if (typeof draft === "object") { if (typeof draft === "object") {
draft = draft ? jsYaml.dump(draft) : undefined; draft = draft ? jsYaml.dump(draft) : undefined;
} }
editResourceStore.setData(this.tabId, { editResourceStore.setData(this.tabId, {
...this.tabData, ...editResourceStore.getData(this.tabId),
draft, draft,
}); });
} }
@ -64,9 +69,8 @@ export class EditResource extends React.Component<Props> {
if (this.error) { if (this.error) {
return; return;
} }
const { draft } = this.tabData;
const store = editResourceStore.getStore(this.tabId); const store = editResourceStore.getStore(this.tabId);
const updatedResource = await store.update(this.resource, jsYaml.safeLoad(draft)); const updatedResource = await store.update(this.resource, jsYaml.safeLoad(this.draft));
this.saveDraft(updatedResource); // update with new resourceVersion to avoid further errors on save this.saveDraft(updatedResource); // update with new resourceVersion to avoid further errors on save
const resourceType = updatedResource.kind; const resourceType = updatedResource.kind;
@ -80,14 +84,12 @@ export class EditResource extends React.Component<Props> {
}; };
render() { render() {
if (!this.isReady) { const { tabId, error, onChange, save, draft, isReady, resource } = this;
if (!isReady) {
return <Spinner center/>; return <Spinner center/>;
} }
const { tabId, error, onChange, save } = this;
const { kind, getNs, getName } = this.resource;
const { draft } = this.tabData;
return ( return (
<div className={cssNames("EditResource flex column", this.props.className)}> <div className={cssNames("EditResource flex column", this.props.className)}>
<InfoPanel <InfoPanel
@ -98,9 +100,9 @@ export class EditResource extends React.Component<Props> {
submittingMessage="Applying.." submittingMessage="Applying.."
controls={( controls={(
<div className="resource-info flex gaps align-center"> <div className="resource-info flex gaps align-center">
<span>Kind:</span> <Badge label={kind}/> <span>Kind:</span> <Badge label={resource.kind}/>
<span>Name:</span><Badge label={getName()}/> <span>Name:</span><Badge label={resource.getName()}/>
<span>Namespace:</span> <Badge label={getNs() || "global"}/> <span>Namespace:</span> <Badge label={resource.getNs() || "global"}/>
</div> </div>
)} )}
/> />