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

Demonstrate naive way to make component not responsible of it's dependencies and not use global shared state

This is to make a step towards satisfaction of Dependency Inversion Principle. It's not fully satisfied because we still depend on concrete instances of classes instead of interfaces.

This will start chain of thought to pursue warm and friendly unit testing and introduction of SOLID principles.

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>
This commit is contained in:
Janne Savolainen 2021-11-04 09:23:59 +02:00
parent b07b8dc096
commit 8d0abcc9d5
5 changed files with 91 additions and 17 deletions

View File

@ -32,6 +32,7 @@ import { Table, TableCell, TableHead, TableRow } from "../table";
import { KubeObjectStatusIcon } from "../kube-object-status-icon";
import { replicaSetStore } from "../+workloads-replicasets/replicasets.store";
import { showDetails } from "../kube-detail-params";
import { InjectNaive } from "../kube-object-menu/inject-naive/inject-naive";
enum sortBy {
@ -115,6 +116,6 @@ export class DeploymentReplicaSets extends React.Component<Props> {
export function ReplicaSetMenu(props: KubeObjectMenuProps<ReplicaSet>) {
return (
<KubeObjectMenu {...props}/>
<InjectNaive Component={KubeObjectMenu} {...props}/>
);
}

View File

@ -35,6 +35,7 @@ import logger from "../../../main/logger";
import { CrdResourceDetails } from "../+custom-resources";
import { KubeObjectMeta } from "../kube-object-meta";
import { hideDetails, kubeDetailsUrlParam } from "../kube-detail-params";
import { InjectNaive } from "../kube-object-menu/inject-naive/inject-naive";
export interface KubeObjectDetailsProps<T extends KubeObject = KubeObject> {
@ -104,7 +105,7 @@ export class KubeObjectDetails extends React.Component {
className="KubeObjectDetails flex column"
open={isOpen}
title=""
toolbar={<KubeObjectMenu object={object} toolbar={true} />}
toolbar={<InjectNaive Component={KubeObjectMenu} object={object} toolbar={true} />}
onClose={hideDetails}
>
{isLoading && <Spinner center />}
@ -144,7 +145,7 @@ export class KubeObjectDetails extends React.Component {
className="KubeObjectDetails flex column"
open={isOpen}
title={title}
toolbar={<KubeObjectMenu object={object} toolbar={true}/>}
toolbar={<InjectNaive Component={KubeObjectMenu} object={object} toolbar={true}/>}
onClose={hideDetails}
>
{isLoading && <Spinner center/>}

View File

@ -36,6 +36,7 @@ import { kubeSelectedUrlParam, toggleDetails } from "../kube-detail-params";
import { Icon } from "../icon";
import { TooltipPosition } from "../tooltip";
import type { ClusterContext } from "../../../common/k8s-api/cluster-context";
import { InjectNaive } from "../kube-object-menu/inject-naive/inject-naive";
export interface KubeObjectListLayoutProps<K extends KubeObject> extends ItemListLayoutProps<K> {
store: KubeObjectStore<K>;
@ -140,7 +141,7 @@ export class KubeObjectListLayout<K extends KubeObject> extends React.Component<
}),
...[customizeHeader].flat(),
]}
renderItemMenu={item => <KubeObjectMenu object={item} />}
renderItemMenu={item => <InjectNaive Component={KubeObjectMenu} object={item} />}
/>
);
}

View File

@ -0,0 +1,46 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import React from "react";
import { editResourceTab } from "../../dock/edit-resource.store";
import { hideDetails } from "../../kube-detail-params";
import { apiManager } from "../../../../common/k8s-api/api-manager";
import { KubeObjectMenuRegistry } from "../../../../extensions/registries";
import { catalogEntityRegistry } from "../../../api/catalog-entity-registry";
interface Props {
Component: React.ReactType<any>
[key: string]: any,
}
export const InjectNaive = ({ Component, ...props }: Props) => {
const kubeObjectMenuRegistry = KubeObjectMenuRegistry.getInstance();
return (
<Component
editResourceTab={editResourceTab}
hideDetails={hideDetails}
apiManager={apiManager}
kubeObjectMenuRegistry={kubeObjectMenuRegistry}
catalogEntityRegistry={catalogEntityRegistry}
{...props} />);
};

View File

@ -22,13 +22,13 @@
import React from "react";
import { boundMethod, cssNames } from "../../utils";
import type { KubeObject } from "../../../common/k8s-api/kube-object";
import { editResourceTab } from "../dock/edit-resource.store";
import { MenuActions, MenuActionsProps } from "../menu/menu-actions";
import { hideDetails } from "../kube-detail-params";
import { apiManager } from "../../../common/k8s-api/api-manager";
import { KubeObjectMenuRegistry } from "../../../extensions/registries/kube-object-menu-registry";
import identity from "lodash/identity";
import { catalogEntityRegistry } from "../../api/catalog-entity-registry";
import type { KubeObjectMenuRegistry } from "../../../extensions/registries";
import type { ApiManager } from "../../../common/k8s-api/api-manager";
import type { CatalogEntityRegistry } from "../../api/catalog-entity-registry";
export interface KubeObjectMenuProps<T> extends MenuActionsProps {
object: T | null | undefined;
@ -36,13 +36,39 @@ export interface KubeObjectMenuProps<T> extends MenuActionsProps {
removable?: boolean;
}
export class KubeObjectMenu<T extends KubeObject> extends React.Component<KubeObjectMenuProps<T>> {
interface KubeObjectMenuDependencies<T> extends KubeObjectMenuProps<T>{
apiManager: ApiManager,
hideDetails: Function,
editResourceTab: Function,
catalogEntityRegistry: CatalogEntityRegistry,
kubeObjectMenuRegistry: KubeObjectMenuRegistry
}
export class KubeObjectMenu<T extends KubeObject> extends React.Component<KubeObjectMenuDependencies<T>> {
get dependencies() {
const {
apiManager,
hideDetails,
editResourceTab,
catalogEntityRegistry,
kubeObjectMenuRegistry,
} = this.props;
return {
apiManager,
editResourceTab,
hideDetails,
kubeObjectMenuRegistry,
catalogEntityRegistry,
};
}
get store() {
const { object } = this.props;
if (!object) return null;
return apiManager.getStore(object.selfLink);
return this.dependencies.apiManager.getStore(object.selfLink);
}
get isEditable() {
@ -55,13 +81,13 @@ export class KubeObjectMenu<T extends KubeObject> extends React.Component<KubeOb
@boundMethod
async update() {
hideDetails();
editResourceTab(this.props.object);
this.dependencies.hideDetails();
this.dependencies.editResourceTab(this.props.object);
}
@boundMethod
async remove() {
hideDetails();
this.dependencies.hideDetails();
const { object, removeAction } = this.props;
if (removeAction) await removeAction();
@ -77,7 +103,7 @@ export class KubeObjectMenu<T extends KubeObject> extends React.Component<KubeOb
}
const breadcrumbParts = [
catalogEntityRegistry.activeEntity?.metadata?.name,
this.dependencies.catalogEntityRegistry.activeEntity?.metadata?.name,
object.getNs(),
object.kind,
object.getName(),
@ -97,8 +123,7 @@ export class KubeObjectMenu<T extends KubeObject> extends React.Component<KubeOb
return [];
}
return KubeObjectMenuRegistry
.getInstance()
return this.dependencies.kubeObjectMenuRegistry
.getItemsForKind(object.kind, object.apiVersion)
.map(({ components: { MenuItem }}, index) => (
<MenuItem