فهرست منبع

namespace selector -> FC to update on cluster change

Justin Rhee 3 سال پیش
والد
کامیت
c18743fcbe

+ 1 - 1
dashboard/src/main/home/cluster-dashboard/ClusterDashboard.tsx

@@ -16,7 +16,7 @@ import {
 import DashboardHeader from "./DashboardHeader";
 import DashboardHeader from "./DashboardHeader";
 import ChartList from "./chart/ChartList";
 import ChartList from "./chart/ChartList";
 import EnvGroupDashboard from "./env-groups/EnvGroupDashboard";
 import EnvGroupDashboard from "./env-groups/EnvGroupDashboard";
-import NamespaceSelector from "./NamespaceSelector";
+import { NamespaceSelector } from "./NamespaceSelector";
 import SortSelector from "./SortSelector";
 import SortSelector from "./SortSelector";
 import ExpandedChartWrapper from "./expanded-chart/ExpandedChartWrapper";
 import ExpandedChartWrapper from "./expanded-chart/ExpandedChartWrapper";
 import { RouteComponentProps, withRouter } from "react-router";
 import { RouteComponentProps, withRouter } from "react-router";

+ 54 - 62
dashboard/src/main/home/cluster-dashboard/NamespaceSelector.tsx

@@ -1,4 +1,4 @@
-import React, { Component } from "react";
+import React, { useContext, useEffect, useMemo, useState } from "react";
 import styled from "styled-components";
 import styled from "styled-components";
 
 
 import { Context } from "shared/Context";
 import { Context } from "shared/Context";
@@ -6,7 +6,7 @@ import api from "shared/api";
 
 
 import Selector from "components/Selector";
 import Selector from "components/Selector";
 
 
-type PropsType = {
+type Props = {
   setNamespace: (x: string) => void;
   setNamespace: (x: string) => void;
   namespace: string;
   namespace: string;
 };
 };
@@ -16,15 +16,19 @@ type StateType = {
 };
 };
 
 
 // TODO: fix update to unmounted component
 // TODO: fix update to unmounted component
-export default class NamespaceSelector extends Component<PropsType, StateType> {
-  _isMounted = false;
-
-  state = {
-    namespaceOptions: [] as { label: string; value: string }[],
-  };
-
-  updateOptions = () => {
-    let { currentCluster, currentProject } = this.context;
+export const NamespaceSelector: React.FunctionComponent<Props> = ({
+  setNamespace,
+  namespace,
+}) => {
+  const context = useContext(Context);
+  let _isMounted = true;
+  const [namespaceOptions, setNamespaceOptions] = useState<{
+    label: string, value: string,
+  }[]>([]);
+  const [defaultNamespace, setDefaultNamespace] = useState<string>("default");
+
+  const updateOptions = () => {
+    let { currentCluster, currentProject } = context;
 
 
     api
     api
       .getNamespaces(
       .getNamespaces(
@@ -36,7 +40,7 @@ export default class NamespaceSelector extends Component<PropsType, StateType> {
         }
         }
       )
       )
       .then((res) => {
       .then((res) => {
-        if (this._isMounted) {
+        if (_isMounted) {
           let namespaceOptions: { label: string; value: string }[] = [
           let namespaceOptions: { label: string; value: string }[] = [
             { label: "All", value: "ALL" },
             { label: "All", value: "ALL" },
           ];
           ];
@@ -49,12 +53,12 @@ export default class NamespaceSelector extends Component<PropsType, StateType> {
             urlNamespace = "ALL";
             urlNamespace = "ALL";
           }
           }
 
 
-          let defaultNamespace = "default";
           const availableNamespaces = res.data.filter(
           const availableNamespaces = res.data.filter(
             (namespace: any) => {
             (namespace: any) => {
               return namespace.status !== "Terminating";
               return namespace.status !== "Terminating";
             }
             }
           );
           );
+          setDefaultNamespace("default");
           availableNamespaces.forEach(
           availableNamespaces.forEach(
             (x: { name: string }, i: number) => {
             (x: { name: string }, i: number) => {
               namespaceOptions.push({
               namespaceOptions.push({
@@ -62,72 +66,60 @@ export default class NamespaceSelector extends Component<PropsType, StateType> {
                 value: x.name,
                 value: x.name,
               });
               });
               if (x.name === urlNamespace) {
               if (x.name === urlNamespace) {
-                defaultNamespace = urlNamespace;
+                setDefaultNamespace(urlNamespace);
               }
               }
             }
             }
           );
           );
-          this.setState({ namespaceOptions }, () => {
-            if (
-              urlNamespace === "" ||
-              defaultNamespace === "" ||
-              urlNamespace === "ALL"
-            ) {
-              this.props.setNamespace("ALL");
-            } else if (this.props.namespace !== defaultNamespace) {
-              this.props.setNamespace(defaultNamespace);
-            }
-          });
+          setNamespaceOptions(namespaceOptions);
         }
         }
       })
       })
       .catch((err) => {
       .catch((err) => {
-        if (this._isMounted) {
-          this.setState({ namespaceOptions: [{ label: "All", value: "ALL" }] });
+        if (_isMounted) {
+          setNamespaceOptions([{ label: "All", value: "ALL" }]);
         }
         }
       });
       });
   };
   };
 
 
-  componentDidMount() {
-    this._isMounted = true;
-    this.updateOptions();
-  }
-
-  componentDidUpdate(prevProps: PropsType) {
-    if (prevProps.namespace !== this.props.namespace) {
-      this.updateOptions();
+  useEffect(() => {
+    let urlParams = new URLSearchParams(window.location.search);
+    let urlNamespace = urlParams.get("namespace");
+    if (
+      urlNamespace === "" ||
+      defaultNamespace === "" ||
+      urlNamespace === "ALL"
+    ) {
+      setNamespace("ALL");
+    } else if (namespace !== defaultNamespace) {
+      setNamespace(defaultNamespace);
     }
     }
-  }
+  }, [namespaceOptions]);
 
 
-  componentWillUnmount() {
-    this._isMounted = false;
-  }
+  useEffect(() => {
+    updateOptions();
+  }, [namespace, context.currentCluster]);
 
 
-  handleSetActive = (namespace: any) => {
-    // console.log("SELECTED", namespace);
-    this.props.setNamespace(namespace);
+  const handleSetActive = (namespace: any) => {
+    setNamespace(namespace);
   };
   };
 
 
-  render() {
-    return (
-      <StyledNamespaceSelector>
-        <Label>
-          <i className="material-icons">filter_alt</i> Namespace
-        </Label>
-        <Selector
-          activeValue={this.props.namespace}
-          setActiveValue={this.handleSetActive}
-          options={this.state.namespaceOptions}
-          dropdownLabel="Namespace"
-          width="150px"
-          dropdownWidth="230px"
-          closeOverlay={true}
-        />
-      </StyledNamespaceSelector>
-    );
-  }
+  return (
+    <StyledNamespaceSelector>
+      <Label>
+        <i className="material-icons">filter_alt</i> Namespace
+      </Label>
+      <Selector
+        activeValue={namespace}
+        setActiveValue={handleSetActive}
+        options={namespaceOptions}
+        dropdownLabel="Namespace"
+        width="150px"
+        dropdownWidth="230px"
+        closeOverlay={true}
+      />
+    </StyledNamespaceSelector>
+  );
 }
 }
 
 
-NamespaceSelector.contextType = Context;
-
 const Label = styled.div`
 const Label = styled.div`
   display: flex;
   display: flex;
   align-items: center;
   align-items: center;

+ 2 - 2
dashboard/src/main/home/cluster-dashboard/TagFilter.tsx

@@ -5,7 +5,7 @@ import { Context } from "shared/Context";
 import styled from "styled-components";
 import styled from "styled-components";
 
 
 const TagFilter = ({ onSelect }: { onSelect: (tag: any) => void }) => {
 const TagFilter = ({ onSelect }: { onSelect: (tag: any) => void }) => {
-  const { currentProject } = useContext(Context);
+  const { currentProject, currentCluster } = useContext(Context);
   const [selectedTag, setSelectedTag] = useState("none");
   const [selectedTag, setSelectedTag] = useState("none");
   const [tags, setTags] = useState([]);
   const [tags, setTags] = useState([]);
 
 
@@ -22,7 +22,7 @@ const TagFilter = ({ onSelect }: { onSelect: (tag: any) => void }) => {
     return () => {
     return () => {
       isSubscribed = false;
       isSubscribed = false;
     };
     };
-  }, [currentProject]);
+  }, [currentProject, currentCluster]);
 
 
   useEffect(() => {
   useEffect(() => {
     const currentTag = tags.find((tag) => tag.name === selectedTag);
     const currentTag = tags.find((tag) => tag.name === selectedTag);

+ 1 - 1
dashboard/src/main/home/cluster-dashboard/env-groups/EnvGroupDashboard.tsx

@@ -7,7 +7,7 @@ import { Context } from "shared/Context";
 import { ClusterType } from "shared/types";
 import { ClusterType } from "shared/types";
 
 
 import DashboardHeader from "../DashboardHeader";
 import DashboardHeader from "../DashboardHeader";
-import NamespaceSelector from "../NamespaceSelector";
+import { NamespaceSelector } from "../NamespaceSelector";
 import SortSelector from "../SortSelector";
 import SortSelector from "../SortSelector";
 import EnvGroupList from "./EnvGroupList";
 import EnvGroupList from "./EnvGroupList";
 import CreateEnvGroup from "./CreateEnvGroup";
 import CreateEnvGroup from "./CreateEnvGroup";

+ 1 - 1
dashboard/src/main/home/cluster-dashboard/stacks/Dashboard.tsx

@@ -5,7 +5,7 @@ import { useHistory, useLocation } from "react-router";
 import { useRouting } from "shared/routing";
 import { useRouting } from "shared/routing";
 import styled from "styled-components";
 import styled from "styled-components";
 import DashboardHeader from "../DashboardHeader";
 import DashboardHeader from "../DashboardHeader";
-import NamespaceSelector from "../NamespaceSelector";
+import { NamespaceSelector } from "../NamespaceSelector";
 import SortSelector from "../SortSelector";
 import SortSelector from "../SortSelector";
 import { Action } from "./components/styles";
 import { Action } from "./components/styles";
 import StackList from "./_StackList";
 import StackList from "./_StackList";