Passed
Push — dev ( a62c70...d00fee )
by
unknown
04:29
created

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

Complexity

Total Complexity 6
Complexity/F 0

Size

Lines of Code 141
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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