Passed
Push — dev ( 9c3709...a6a8e9 )
by
unknown
04:51 queued 10s
created

resources/assets/js/components/JobBuilder/Details/JobDetailsPage.tsx   A

Complexity

Total Complexity 6
Complexity/F 0

Size

Lines of Code 140
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 113
c 0
b 0
f 0
dl 0
loc 140
rs 10
mnd 6
bc 6
fnc 0
bpm 0
cpm 0
noi 0
1
import React, { useEffect, useMemo } from "react";
2
import ReactDOM from "react-dom";
3
import { connect } from "react-redux";
4
import { WrappedComponentProps, injectIntl } from "react-intl";
5
import { Job, JobPosterKeyTask, Criteria, Classification } from "../../../models/types";
6
import { JobDetails } from "./JobDetails";
7
import { RootState } from "../../../store/store";
8
import { DispatchType } from "../../../configureStore";
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
  jobBuilderEnv,
18
  jobBuilderIntro,
19
  jobBuilderReview,
20
} from "../../../helpers/routes";
21
import JobBuilderStepContainer from "../JobBuilderStep";
22
import { isJobBuilderComplete } from "../jobBuilderHelpers";
23
import { navigate } from "../../../helpers/router";
24
import { useDispatch } from "react-redux";
25
import { getClassifications } from "../../../../js/store/Classification/classificationSelector"
26
27
interface JobDetailsPageProps {
28
  jobId: number;
29
  job: Job | null;
30
  // Tasks associated with the job, used to determine if its complete
31
  keyTasks: JobPosterKeyTask[];
32
  // Criteria associated with the job, used to determine if its complete
33
  criteria: Criteria[];
34
  classifications: Classification[];
35
  handleUpdateJob: (newJob: Job) => Promise<boolean>;
36
}
37
38
39
const JobDetailsPage: React.FunctionComponent<JobDetailsPageProps &
40
  WrappedComponentProps> = ({
41
  jobId,
42
  job,
43
  classifications,
44
  handleUpdateJob,
45
  keyTasks,
46
  criteria,
47
  intl,
48
}): React.ReactElement => {
49
50
  const dispatch = useDispatch();
51
52
  const { locale } = intl;
53
  if (locale !== "en" && locale !== "fr") {
54
    throw Error("Unexpected intl.locale"); // TODO: Deal with this more elegantly.
55
  }
56
  const handleModalCancel = (): void => {};
57
  const handleModalConfirm = (): void => {
58
    if (job) {
59
      navigate(jobBuilderEnv(intl.locale, jobId));
60
    }
61
  };
62
  const handleSubmit = handleUpdateJob;
63
  const handleReturn = (): void => {
64
    navigate(jobBuilderIntro(locale, jobId));
65
  };
66
  const handleSkipToReview = async (): Promise<void> => {
67
    if (jobId !== null) {
68
      navigate(jobBuilderReview(locale, jobId));
69
    }
70
  };
71
72
  const jobIsComplete =
73
    job !== null && isJobBuilderComplete(job, keyTasks, criteria, locale);
74
  return (
75
    <JobBuilderStepContainer jobId={jobId} currentPage="details">
76
      {job !== null && (
77
        <JobDetails
78
          job={job}
79
          classifications={classifications}
80
          handleSubmit={handleSubmit}
81
          handleReturn={handleReturn}
82
          handleModalCancel={handleModalCancel}
83
          handleModalConfirm={handleModalConfirm}
84
          jobIsComplete={jobIsComplete}
85
          handleSkipToReview={handleSkipToReview}
86
        />
87
      )}
88
    </JobBuilderStepContainer>
89
  );
90
};
91
92
const mapStateToPropsPage = (
93
  state: RootState,
94
  ownProps: { jobId: number },
95
): {
96
  job: Job | null;
97
  keyTasks: JobPosterKeyTask[];
98
  criteria: Criteria[];
99
  classifications: Classification[]
100
} => ({
101
  job: getJob(state, ownProps),
102
  keyTasks: getTasksByJob(state, ownProps),
103
  criteria: getCriteriaByJob(state, ownProps),
104
  classifications: getClassifications(state),
105
});
106
107
const mapDispatchToPropsPage = (
108
  dispatch: DispatchType,
109
): {
110
  handleUpdateJob: (newJob: Job) => Promise<boolean>;
111
} => ({
112
  handleUpdateJob: async (newJob: Job): Promise<boolean> => {
113
    const result = await dispatch(updateJob(newJob));
114
    return !result.error;
115
  },
116
});
117
118
const JobDetailsPageContainer = connect(
119
  mapStateToPropsPage,
120
  mapDispatchToPropsPage,
121
)(injectIntl(JobDetailsPage));
122
123
if (document.getElementById("job-builder-details")) {
124
  const container = document.getElementById(
125
    "job-builder-details",
126
  ) as HTMLElement;
127
  const jobIdAttr = container.getAttribute("data-job-id");
128
  const jobId = jobIdAttr ? Number(jobIdAttr) : null;
129
  if (jobId) {
130
    ReactDOM.render(
131
      <RootContainer>
132
        <JobDetailsPageContainer jobId={jobId} />
133
      </RootContainer>,
134
      container,
135
    );
136
  }
137
}
138
139
export default JobDetailsPageContainer;
140