Преглед изворни кода

Refactored job list to be a functional component

jnfrati пре 4 година
родитељ
комит
9af8e7cc07
1 измењених фајлова са 59 додато и 78 уклоњено
  1. 59 78
      dashboard/src/main/home/cluster-dashboard/expanded-chart/jobs/JobList.tsx

+ 59 - 78
dashboard/src/main/home/cluster-dashboard/expanded-chart/jobs/JobList.tsx

@@ -1,13 +1,12 @@
-import React, { Component } from "react";
+import React, { useContext, useState } from "react";
 import styled from "styled-components";
 import styled from "styled-components";
 
 
 import api from "shared/api";
 import api from "shared/api";
 import { Context } from "shared/Context";
 import { Context } from "shared/Context";
 import JobResource from "./JobResource";
 import JobResource from "./JobResource";
-import ConfirmOverlay from "components/ConfirmOverlay";
-import { withAuth, WithAuthProps } from "shared/auth/AuthorizationHoc";
+import useAuth from "shared/auth/useAuth";
 
 
-type PropsType = WithAuthProps & {
+type PropsType = {
   jobs: any[];
   jobs: any[];
   setJobs: (job: any) => void;
   setJobs: (job: any) => void;
   expandJob: any;
   expandJob: any;
@@ -17,72 +16,20 @@ type PropsType = WithAuthProps & {
   repositoryUrl?: string;
   repositoryUrl?: string;
 };
 };
 
 
-type StateType = {
-  deletionCandidate: any;
-  deletionJob: any;
-};
-
-class JobList extends Component<PropsType, StateType> {
-  state = {
-    deletionCandidate: null as any,
-    deletionJob: null as any,
-  };
-
-  renderJobList = () => {
-    if (this.props.jobs.length === 0) {
-      return (
-        <Placeholder>
-          <i className="material-icons">category</i>
-          There are no jobs currently running.
-        </Placeholder>
-      );
-    } else {
-      return (
-        <>
-          {this.props.jobs.map((job: any, i: number) => {
-            return (
-              <JobResource
-                key={job?.metadata?.name}
-                expandJob={this.props.expandJob}
-                job={job}
-                handleDelete={() => {
-                  this.setState({ deletionCandidate: job });
-                  this.context.setCurrentOverlay({
-                    message: `Are you sure you want to delete this job run?`,
-                    onYes: this.deleteJob,
-                    onNo: () => {
-                      this.setState({ deletionCandidate: null });
-                      this.context.setCurrentOverlay(null);
-                    },
-                  });
-                }}
-                deleting={
-                  this.state.deletionJob?.metadata?.name == job.metadata?.name
-                }
-                readOnly={
-                  !this.props.isAuthorized("job", "", [
-                    "get",
-                    "update",
-                    "delete",
-                  ])
-                }
-                isDeployedFromGithub={this.props.isDeployedFromGithub}
-                repositoryUrl={this.props.repositoryUrl}
-                currentChartVersion={this.props.currentChartVersion}
-                latestChartVersion={this.props.latestChartVersion}
-              />
-            );
-          })}
-        </>
-      );
-    }
-  };
-
-  deleteJob = () => {
-    let { currentCluster, currentProject, setCurrentError } = this.context;
-    let job = this.state.deletionCandidate;
-    this.context.setCurrentOverlay(null);
+const JobListFC = (props: PropsType): JSX.Element => {
+  const [isAuthorized] = useAuth();
+  const {
+    currentCluster,
+    currentProject,
+    setCurrentOverlay,
+    setCurrentError,
+  } = useContext(Context);
+  const [deletionCandidate, setDeletionCandidate] = useState(null);
+  const [deletionJob, setDeletionJob] = useState(null);
 
 
+  const deleteJob = () => {
+    let job = deletionCandidate;
+    setCurrentOverlay(null);
     api
     api
       .deleteJob(
       .deleteJob(
         "<token>",
         "<token>",
@@ -95,10 +42,8 @@ class JobList extends Component<PropsType, StateType> {
         }
         }
       )
       )
       .then((res) => {
       .then((res) => {
-        this.setState({
-          deletionJob: this.state.deletionCandidate,
-          deletionCandidate: null,
-        });
+        setDeletionJob(deletionCandidate);
+        setDeletionCandidate(null);
       })
       })
       .catch((err) => {
       .catch((err) => {
         let parsedErr = err?.response?.data?.error;
         let parsedErr = err?.response?.data?.error;
@@ -109,14 +54,50 @@ class JobList extends Component<PropsType, StateType> {
       });
       });
   };
   };
 
 
-  render() {
-    return <JobListWrapper>{this.renderJobList()}</JobListWrapper>;
+  if (!props.jobs?.length) {
+    return (
+      <JobListWrapper>
+        <Placeholder>
+          <i className="material-icons">category</i>
+          There are no jobs currently running.
+        </Placeholder>
+      </JobListWrapper>
+    );
   }
   }
-}
 
 
-JobList.contextType = Context;
+  return (
+    <JobListWrapper>
+      {props.jobs.map((job: any, i: number) => {
+        return (
+          <JobResource
+            key={job?.metadata?.name}
+            expandJob={props.expandJob}
+            job={job}
+            handleDelete={() => {
+              setDeletionCandidate(job);
+              setCurrentOverlay({
+                message: "Are you sure you want to delete this job run?",
+                onYes: deleteJob,
+                onNo: () => {
+                  setDeletionCandidate(null);
+                  setCurrentOverlay(null);
+                },
+              });
+            }}
+            deleting={deletionJob?.metadata?.name == job.metadata?.name}
+            readOnly={!isAuthorized("job", "", ["get", "update", "delete"])}
+            isDeployedFromGithub={props.isDeployedFromGithub}
+            repositoryUrl={props.repositoryUrl}
+            currentChartVersion={props.currentChartVersion}
+            latestChartVersion={props.latestChartVersion}
+          />
+        );
+      })}
+    </JobListWrapper>
+  );
+};
 
 
-export default withAuth(JobList);
+export default JobListFC;
 
 
 const Placeholder = styled.div`
 const Placeholder = styled.div`
   width: 100%;
   width: 100%;