Jelajahi Sumber

Use current cluster instead of letting the user select a cluster

jnfrati 3 tahun lalu
induk
melakukan
97e1552045

+ 6 - 57
dashboard/src/main/home/cluster-dashboard/stacks/launch/Overview.tsx

@@ -18,20 +18,14 @@ import Loading from "components/Loading";
 const Overview = () => {
   const {
     newStack,
-    clusterId,
     namespace,
     setStackName,
     setStackNamespace,
-    setStackCluster,
     submit,
   } = useContext(StacksLaunchContext);
-  const { currentProject } = useContext(Context);
+  const { currentProject, currentCluster } = useContext(Context);
   const [isAuthorized] = useAuth();
 
-  const [clusterOptions, setClusterOptions] = useState<
-    { label: string; value: string }[]
-  >([]);
-
   const [namespaceOptions, setNamespaceOptions] = useState<
     { label: string; value: string }[]
   >([]);
@@ -40,31 +34,6 @@ const Overview = () => {
 
   const { pushFiltered } = useRouting();
 
-  const getClusters = () => {
-    return api
-      .getClusters("<token>", {}, { id: currentProject.id })
-      .then((res) => {
-        if (res.data) {
-          let clusterOptions: {
-            label: string;
-            value: string;
-          }[] = res.data.map((cluster: ClusterType, i: number) => ({
-            label: cluster.name,
-            value: `${cluster.id}`,
-          }));
-
-          if (res.data.length > 0) {
-            setClusterOptions(clusterOptions);
-            console.log({ clusterId });
-            if (isNaN(clusterId)) {
-              const newClusterId = res.data[0].id;
-              setStackCluster(newClusterId);
-            }
-          }
-        }
-      });
-  };
-
   const updateNamespaces = (cluster_id: number) => {
     api
       .getNamespaces(
@@ -108,23 +77,14 @@ const Overview = () => {
   };
 
   useEffect(() => {
-    getClusters();
-  }, []);
-
-  useEffect(() => {
-    if (isNaN(clusterId)) {
-      return;
-    }
-    updateNamespaces(clusterId);
-  }, [clusterId]);
+    updateNamespaces(currentCluster.id);
+  }, [currentCluster]);
 
   const isValid = useMemo(() => {
     if (namespace === "") {
       return false;
     }
-    if (isNaN(clusterId)) {
-      return false;
-    }
+
     if (newStack.name === "") {
       return false;
     }
@@ -138,7 +98,7 @@ const Overview = () => {
     }
 
     return true;
-  }, [namespace, clusterId, newStack.name]);
+  }, [namespace, newStack.name]);
 
   return (
     <div style={{ position: "relative" }}>
@@ -148,21 +108,10 @@ const Overview = () => {
         setValue={(newName: string) => setStackName(newName)}
       />
 
-      <Selector
-        activeValue={`${clusterId}`}
-        setActiveValue={(cluster: string) => {
-          setStackCluster(Number(cluster));
-        }}
-        options={clusterOptions}
-        width="250px"
-        dropdownWidth="335px"
-        closeOverlay={true}
-      />
-
       <Selector
         key={"namespace"}
         refreshOptions={() => {
-          updateNamespaces(clusterId);
+          updateNamespaces(currentCluster.id);
         }}
         addButton={isAuthorized("namespace", "", ["get", "create"])}
         activeValue={namespace}

+ 5 - 14
dashboard/src/main/home/cluster-dashboard/stacks/launch/Store.tsx

@@ -7,10 +7,8 @@ export type StacksLaunchContextType = {
   newStack: CreateStackBody;
 
   namespace: string;
-  clusterId: number;
 
   setStackName: (name: string) => void;
-  setStackCluster: (clusterId: number) => void;
   setStackNamespace: (namespace: string) => void;
 
   addSourceConfig: (
@@ -30,10 +28,8 @@ const defaultValues: StacksLaunchContextType = {
   },
 
   namespace: "",
-  clusterId: NaN,
 
   setStackName: (name: string) => {},
-  setStackCluster: (clusterId: number) => {},
   setStackNamespace: (namespace: string) => {},
 
   addSourceConfig: (
@@ -50,11 +46,12 @@ export const StacksLaunchContext = createContext<StacksLaunchContextType>(
 );
 
 const StacksLaunchContextProvider: React.FC<{}> = ({ children }) => {
-  const { currentProject, setCurrentError } = useContext(Context);
+  const { currentProject, currentCluster, setCurrentError } = useContext(
+    Context
+  );
   const [newStack, setNewStack] = useState<CreateStackBody>(
     defaultValues.newStack
   );
-  const [clusterId, setClusterId] = useState<number>(NaN);
   const [namespace, setNamespace] = useState("default");
 
   const setStackName: StacksLaunchContextType["setStackName"] = (name) => {
@@ -63,11 +60,7 @@ const StacksLaunchContextProvider: React.FC<{}> = ({ children }) => {
       name,
     }));
   };
-  const setStackCluster: StacksLaunchContextType["setStackCluster"] = (
-    newClusterId
-  ) => {
-    setClusterId(newClusterId);
-  };
+
   const setStackNamespace: StacksLaunchContextType["setStackNamespace"] = (
     namespace
   ) => {
@@ -106,7 +99,7 @@ const StacksLaunchContextProvider: React.FC<{}> = ({ children }) => {
   const submit: StacksLaunchContextType["submit"] = async () => {
     try {
       await api.createStack("<token>", newStack, {
-        cluster_id: clusterId,
+        cluster_id: currentCluster.id,
         namespace: namespace,
         project_id: currentProject.id,
       });
@@ -121,9 +114,7 @@ const StacksLaunchContextProvider: React.FC<{}> = ({ children }) => {
       value={{
         newStack,
         namespace,
-        clusterId,
         setStackName,
-        setStackCluster,
         setStackNamespace,
         addSourceConfig,
         addAppResource,