Passed
Push — task/security-fixes ( 62effe...fd3b60 )
by Grant
05:50
created

resources/assets/js/components/JobBuilder/WorkEnv/JobWorkEnvPage.tsx   A

Complexity

Total Complexity 5
Complexity/F 0

Size

Lines of Code 131
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 107
mnd 5
bc 5
fnc 0
dl 0
loc 131
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React from "react";
2
import { useIntl } from "react-intl";
3
import { connect } from "react-redux";
4
import ReactDOM from "react-dom";
5
import JobWorkEnv from "./JobWorkEnv";
6
import { Job, JobPosterKeyTask, Criteria } from "../../../models/types";
7
import { DispatchType } from "../../../configureStore";
8
import { RootState } from "../../../store/store";
9
import { updateJob } from "../../../store/Job/jobActions";
10
import {
11
  getJob,
12
  getTasksByJob,
13
  getCriteriaByJob,
14
} from "../../../store/Job/jobSelector";
15
import RootContainer from "../../RootContainer";
16
import {
17
  jobBuilderImpact,
18
  jobBuilderDetails,
19
  jobBuilderReview,
20
} from "../../../helpers/routes";
21
import JobBuilderStepContainer from "../JobBuilderStep";
22
import { isJobBuilderComplete } from "../jobBuilderHelpers";
23
import { navigate } from "../../../helpers/router";
24
import { getLocale } from "../../../helpers/localize";
25
26
interface JobWorkEnvPageProps {
27
  // The id of the edited job, or null for a new job.
28
  jobId: number;
29
  // If not null, used to prepopulate form values.
30
  // Note: its possible for jobId to be non-null, but job to be null, if the data hasn't been loaded yet.
31
  job: Job | null;
32
  // Tasks associated with the job, used to determine if its complete
33
  keyTasks: JobPosterKeyTask[];
34
  // Criteria associated with the job, used to determine if its complete
35
  criteria: Criteria[];
36
  // Updates an existing job. Must return the updated job if successful.
37
  handleUpdateJob: (newJob: Job) => Promise<Job>;
38
}
39
40
const JobWorkEnvPage: React.FunctionComponent<JobWorkEnvPageProps> = ({
41
  jobId,
42
  job,
43
  handleUpdateJob,
44
  keyTasks,
45
  criteria,
46
}): React.ReactElement => {
47
  const intl = useIntl();
48
  const locale = getLocale(intl.locale);
49
  const handleSubmit = handleUpdateJob;
50
  const handleModalCancel = (): void => {};
51
  const handleModalConfirm = (): void => {
52
    navigate(jobBuilderImpact(intl.locale, jobId));
53
  };
54
  const handleReturn = (): void => {
55
    navigate(jobBuilderDetails(locale, jobId));
56
  };
57
  const handleSkipToReview = async (): Promise<void> => {
58
    if (jobId) {
59
      navigate(jobBuilderReview(locale, jobId));
60
    }
61
  };
62
  const jobIsComplete =
63
    job !== null && isJobBuilderComplete(job, keyTasks, criteria, locale);
64
  return (
65
    <JobBuilderStepContainer jobId={jobId} currentPage="env">
66
      {job !== null && (
67
        <JobWorkEnv
68
          job={job}
69
          handleSubmit={handleSubmit}
70
          handleReturn={handleReturn}
71
          handleModalCancel={handleModalCancel}
72
          handleModalConfirm={handleModalConfirm}
73
          jobIsComplete={jobIsComplete}
74
          handleSkipToReview={handleSkipToReview}
75
        />
76
      )}
77
    </JobBuilderStepContainer>
78
  );
79
};
80
81
const mapStateToProps = (
82
  state: RootState,
83
  ownProps: { jobId: number },
84
): {
85
  job: Job | null;
86
  keyTasks: JobPosterKeyTask[];
87
  criteria: Criteria[];
88
} => ({
89
  job: getJob(state, ownProps),
90
  keyTasks: getTasksByJob(state, ownProps),
91
  criteria: getCriteriaByJob(state, ownProps),
92
});
93
94
const mapDispatchToProps = (
95
  dispatch: DispatchType,
96
): {
97
  handleUpdateJob: (newJob: Job) => Promise<Job>;
98
} => ({
99
  handleUpdateJob: async (newJob: Job): Promise<Job> => {
100
    const result = await dispatch(updateJob(newJob));
101
    if (!result.error) {
102
      const resultJob = await result.payload;
103
      return resultJob;
104
    }
105
    return Promise.reject(result.payload);
106
  },
107
});
108
109
const JobWorkEnvPageContainer = connect(
110
  mapStateToProps,
111
  mapDispatchToProps,
112
)(JobWorkEnvPage);
113
114
if (document.getElementById("job-builder-work-env")) {
115
  const container: HTMLElement = document.getElementById(
116
    "job-builder-work-env",
117
  ) as HTMLElement;
118
  const jobIdAttr = container.getAttribute("data-job-id");
119
  const jobId = jobIdAttr ? Number(jobIdAttr) : null;
120
  if (jobId) {
121
    ReactDOM.render(
122
      <RootContainer>
123
        <JobWorkEnvPageContainer jobId={jobId} />
124
      </RootContainer>,
125
      container,
126
    );
127
  }
128
}
129
130
export default JobWorkEnvPageContainer;
131