Passed
Push — dependabot/npm_and_yarn/dev/fo... ( 45841c...99f026 )
by
unknown
06:24 queued 01:28
created

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

Complexity

Total Complexity 6
Complexity/F 0

Size

Lines of Code 134
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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