Passed
Push — task/common-translation-packag... ( dbb970...52324d )
by Tristan
06:50 queued 11s
created

resources/assets/js/components/HRPortal/JobReviewHrPage.tsx   A

Complexity

Total Complexity 7
Complexity/F 0

Size

Lines of Code 208
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 169
mnd 7
bc 7
fnc 0
dl 0
loc 208
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React, { useEffect } from "react";
2
import { useIntl, FormattedMessage, defineMessages } from "react-intl";
3
import { useDispatch, useSelector } from "react-redux";
4
import ReactDOM from "react-dom";
5
import RootContainer from "../RootContainer";
6
import {
7
  Job,
8
  JobPosterKeyTask,
9
  Criteria,
10
  Skill,
11
  Manager,
12
  Department,
13
} from "../../models/types";
14
import { hrJobSummary } from "../../helpers/routes";
15
import { RootState } from "../../store/store";
16
import {
17
  getJob,
18
  getTasksByJob,
19
  getCriteriaByJob,
20
} from "../../store/Job/jobSelector";
21
import { getSkills } from "../../store/Skill/skillSelector";
22
import {
23
  fetchJob,
24
  fetchJobTasks,
25
  fetchCriteria,
26
} from "../../store/Job/jobActions";
27
import { getDepartments as fetchDepartments } from "../../store/Department/deptActions";
28
import { getDepartments } from "../../store/Department/deptSelector";
29
import { getManagerById } from "../../store/Manager/managerSelector";
30
import { fetchManager } from "../../store/Manager/managerActions";
31
import { JobReviewDisplay } from "../JobBuilder/Review/JobReview";
32
import { fetchSkills } from "../../store/Skill/skillActions";
33
import Icon from "../Icon";
34
import JobReviewActivityFeed from "../JobBuilder/Review/JobReviewActivityFeed";
35
36
interface JobReviewHrPageProps {
37
  jobId: number;
38
  job: Job | null;
39
  skills: Skill[];
40
  keyTasks: JobPosterKeyTask[];
41
  criteria: Criteria[];
42
  departments: Department[];
43
  manager: Manager | null;
44
}
45
46
const messages = defineMessages({
47
  loadingIcon: {
48
    id: "jobReviewHr.loadingIconText",
49
    defaultMessage: "Data is loading...",
50
    description: "Accessible text for the loading icon",
51
  },
52
});
53
54
const JobReviewHrPage: React.FunctionComponent<JobReviewHrPageProps> = ({
55
  jobId,
56
  job,
57
  skills,
58
  keyTasks,
59
  criteria,
60
  departments,
61
  manager,
62
}): React.ReactElement => {
63
  const intl = useIntl();
64
  const { locale } = intl;
65
  if (locale !== "en" && locale !== "fr") {
66
    throw new Error("Unexpected locale");
67
  }
68
  return (
69
    <div data-c-container="form" data-c-padding="top(triple) bottom(triple)">
70
      {job !== null ? (
71
        <>
72
          <h3
73
            data-c-font-size="h3"
74
            data-c-font-weight="bold"
75
            data-c-margin="bottom(double)"
76
          >
77
            <FormattedMessage
78
              id="jobReviewHr.reviewYourPoster"
79
              defaultMessage="Review Your Job Poster for:"
80
              description="Title for Review Job Poster section."
81
            />{" "}
82
            <span data-c-colour="c2">{job[locale].title}</span>
83
          </h3>
84
          <p>
85
            <FormattedMessage
86
              id="jobReviewHr.headsUp"
87
              defaultMessage="Just a heads up! We've rearranged some of your information to help you understand how an applicant will see it once published."
88
              description="Description under primary title of review section"
89
            />
90
          </p>
91
          <JobReviewActivityFeed jobId={job.id} isHrAdvisor />
92
          <JobReviewDisplay
93
            job={job}
94
            manager={manager}
95
            tasks={keyTasks}
96
            criteria={criteria}
97
            skills={skills}
98
            departments={departments}
99
            hideBuilderLinks
100
          />
101
        </>
102
      ) : (
103
        <div data-c-alignment="base(centre)">
104
          <Icon
105
            icon="fa fa-spinner fa-spin"
106
            accessibleText={intl.formatMessage(messages.loadingIcon)}
107
            sematicIcon
108
          />
109
        </div>
110
      )}
111
      <div data-c-grid="gutter">
112
        <div data-c-grid-item="base(1of1)">
113
          <hr data-c-margin="top(normal) bottom(normal)" />
114
        </div>
115
        <div
116
          data-c-alignment="base(centre) tp(left)"
117
          data-c-grid-item="tp(1of2)"
118
        >
119
          <a href={hrJobSummary(locale, jobId)} title="">
120
            <FormattedMessage
121
              id="jobReviewHr.summaryLink"
122
              defaultMessage="Return to Summary"
123
              description="Text for the Return to Summary link."
124
            />
125
          </a>
126
        </div>
127
      </div>
128
    </div>
129
  );
130
};
131
132
const JobReviewHrDataFetcher: React.FC<{ jobId: number }> = ({ jobId }) => {
133
  const dispatch = useDispatch();
134
135
  // Request and select the job
136
  useEffect(() => {
137
    dispatch(fetchJob(jobId));
138
  }, [dispatch, jobId]);
139
  const job = useSelector((state: RootState) => getJob(state, { jobId }));
140
141
  // Request and select the job tasks
142
  useEffect(() => {
143
    dispatch(fetchJobTasks(jobId));
144
  }, [dispatch, jobId]);
145
  const tasks = useSelector((state: RootState) =>
146
    getTasksByJob(state, { jobId }),
147
  );
148
149
  // Request and select job criteria
150
  useEffect(() => {
151
    dispatch(fetchCriteria(jobId));
152
  }, [dispatch, jobId]);
153
  const criteria = useSelector((state: RootState) =>
154
    getCriteriaByJob(state, { jobId }),
155
  );
156
157
  // Load all skills
158
  useEffect(() => {
159
    dispatch(fetchSkills());
160
  }, [dispatch]);
161
  const skills = useSelector(getSkills);
162
163
  // Load all departments
164
  useEffect(() => {
165
    dispatch(fetchDepartments());
166
  }, [dispatch]);
167
  const departments = useSelector(getDepartments);
168
169
  // Load manager after Job has loaded
170
  // eslint-disable-next-line camelcase
171
  const managerId = job?.manager_id;
172
  useEffect(() => {
173
    if (managerId) {
174
      dispatch(fetchManager(managerId));
175
    }
176
  }, [dispatch, managerId]);
177
  const manager = useSelector((state: RootState) =>
178
    managerId ? getManagerById(state, { managerId }) : null,
179
  );
180
181
  return (
182
    <JobReviewHrPage
183
      jobId={jobId}
184
      job={job}
185
      manager={manager}
186
      keyTasks={tasks}
187
      criteria={criteria}
188
      skills={skills}
189
      departments={departments}
190
    />
191
  );
192
};
193
194
export default JobReviewHrDataFetcher;
195
196
const container = document.getElementById("job-review-hr");
197
if (container !== null) {
198
  if ("jobId" in container.dataset) {
199
    const jobId = Number(container.dataset.jobId as string);
200
    ReactDOM.render(
201
      <RootContainer>
202
        <JobReviewHrDataFetcher jobId={jobId} />
203
      </RootContainer>,
204
      container,
205
    );
206
  }
207
}
208