Passed
Push — dev ( 09eaa1...527775 )
by
unknown
04:28
created

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

Complexity

Total Complexity 6
Complexity/F 0

Size

Lines of Code 129
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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