Passed
Push — task/applicant-classification-... ( a73fc7...ae016f )
by Yonathan
04:35
created

resources/assets/js/components/JobBuilder/Impact/JobImpactPage.tsx   A

Complexity

Total Complexity 5
Complexity/F 0

Size

Lines of Code 138
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 112
mnd 5
bc 5
fnc 0
dl 0
loc 138
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React from "react";
2
import { connect } from "react-redux";
3
import { useIntl } from "react-intl";
4
import ReactDOM from "react-dom";
5
import {
6
  Department,
7
  Job,
8
  JobPosterKeyTask,
9
  Criteria,
10
} from "../../../models/types";
11
import JobImpact from "./JobImpact";
12
import {
13
  jobBuilderTasks,
14
  jobBuilderEnv,
15
  jobBuilderReview,
16
} from "../../../helpers/routes";
17
import { RootState } from "../../../store/store";
18
import { DispatchType } from "../../../configureStore";
19
import { updateJob } from "../../../store/Job/jobActions";
20
import { getDepartments } from "../../../store/Department/deptSelector";
21
import RootContainer from "../../RootContainer";
22
import {
23
  getJob,
24
  getTasksByJob,
25
  getCriteriaByJob,
26
} from "../../../store/Job/jobSelector";
27
import JobBuilderStepContainer from "../JobBuilderStep";
28
import { isJobBuilderComplete } from "../jobBuilderHelpers";
29
import { navigate } from "../../../helpers/router";
30
import { getLocale } from "../../../helpers/localize";
31
32
interface JobImpactPageProps {
33
  jobId: number;
34
  job: Job | null;
35
  departments: Department[];
36
  // Tasks associated with the job, used to determine if its complete
37
  keyTasks: JobPosterKeyTask[];
38
  // Criteria associated with the job, used to determine if its complete
39
  criteria: Criteria[];
40
  handleUpdateJob: (newJob: Job) => Promise<boolean>;
41
}
42
43
const JobImpactPage: React.FunctionComponent<JobImpactPageProps> = ({
44
  jobId,
45
  job,
46
  departments,
47
  keyTasks,
48
  criteria,
49
  handleUpdateJob,
50
}): React.ReactElement => {
51
  const intl = useIntl();
52
  const locale = getLocale(intl.locale);
53
  const handleModalCancel = (): void => {
54
    // Do nothing on cancel
55
  };
56
  const handleModalConfirm = (): void => {
57
    navigate(jobBuilderTasks(locale, jobId));
58
  };
59
  const handleSubmit = handleUpdateJob;
60
  const handleReturn = (): void => {
61
    if (jobId !== null) {
62
      navigate(jobBuilderEnv(locale, jobId));
63
    }
64
  };
65
  const handleSkipToReview = async (): Promise<void> => {
66
    if (jobId !== null) {
67
      navigate(jobBuilderReview(locale, jobId));
68
    }
69
  };
70
  const jobIsComplete =
71
    job !== null && isJobBuilderComplete(job, keyTasks, criteria, locale);
72
  return (
73
    <JobBuilderStepContainer jobId={jobId} currentPage="impact">
74
      {job !== null && (
75
        <JobImpact
76
          job={job}
77
          departments={departments}
78
          handleSubmit={handleSubmit}
79
          handleReturn={handleReturn}
80
          handleModalCancel={handleModalCancel}
81
          handleModalConfirm={handleModalConfirm}
82
          jobIsComplete={jobIsComplete}
83
          handleSkipToReview={handleSkipToReview}
84
        />
85
      )}
86
    </JobBuilderStepContainer>
87
  );
88
};
89
90
const mapStateToProps = (
91
  state: RootState,
92
  ownProps: { jobId: number },
93
): {
94
  job: Job | null;
95
  departments: Department[];
96
  keyTasks: JobPosterKeyTask[];
97
  criteria: Criteria[];
98
} => ({
99
  job: getJob(state, ownProps),
100
  departments: getDepartments(state),
101
  keyTasks: getTasksByJob(state, ownProps),
102
  criteria: getCriteriaByJob(state, ownProps),
103
});
104
105
const mapDispatchToProps = (
106
  dispatch: DispatchType,
107
): {
108
  handleUpdateJob: (newJob: Job) => Promise<boolean>;
109
} => ({
110
  handleUpdateJob: async (newJob: Job): Promise<boolean> => {
111
    const result = await dispatch(updateJob(newJob));
112
    return !result.error;
113
  },
114
});
115
116
export const JobImpactPageContainer = connect(
117
  mapStateToProps,
118
  mapDispatchToProps,
119
)(JobImpactPage);
120
121
export default JobImpactPageContainer;
122
123
if (document.getElementById("job-builder-impact")) {
124
  const container = document.getElementById(
125
    "job-builder-impact",
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
        <JobImpactPageContainer jobId={jobId} />
133
      </RootContainer>,
134
      container,
135
    );
136
  }
137
}
138