Passed
Push — dev ( 225573...d32eae )
by
unknown
04:59
created

resources/assets/js/components/Application/Review/ReviewPage.tsx   A

Complexity

Total Complexity 6
Complexity/F 0

Size

Lines of Code 161
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 131
mnd 6
bc 6
fnc 0
dl 0
loc 161
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
/* eslint-disable @typescript-eslint/camelcase */
2
/* eslint-disable camelcase */
3
import React from "react";
4
import { useIntl } from "react-intl";
5
import { useDispatch } from "react-redux";
6
import ProgressBar, { stepNames } from "../ProgressBar/ProgressBar";
7
import makeProgressBarSteps from "../ProgressBar/progressHelpers";
8
import Review, { ReviewFormValues } from "./Review";
9
import {
10
  applicationIndex,
11
  applicationFit,
12
  applicationSubmission,
13
} from "../../../helpers/routes";
14
import { getLocale } from "../../../helpers/localize";
15
import { navigate } from "../../../helpers/router";
16
import { DispatchType } from "../../../configureStore";
17
import {
18
  useApplication,
19
  useApplicationUser,
20
  useCriteria,
21
  useExperiences,
22
  useExperienceSkills,
23
  useFetchAllApplicationData,
24
  useJob,
25
  useJobApplicationAnswers,
26
  useJobPosterQuestions,
27
  useSkills,
28
  useJobApplicationSteps,
29
  useTouchApplicationStep,
30
} from "../../../hooks/applicationHooks";
31
import { loadingMessages } from "../applicationMessages";
32
import { updateApplication as updateApplicationAction } from "../../../store/Application/applicationActions";
33
34
interface ReviewPageProps {
35
  applicationId: number;
36
}
37
38
export const ReviewPage: React.FC<ReviewPageProps> = ({ applicationId }) => {
39
  const intl = useIntl();
40
  const locale = getLocale(intl.locale);
41
  const dispatch = useDispatch<DispatchType>();
42
43
  // Fetch all un-loaded data that may be required for the Application.
44
  const {
45
    experiencesLoaded,
46
    skillsLoaded,
47
    criteriaLoaded,
48
    experienceSkillsLoaded,
49
    jobQuestionsLoaded,
50
    applicationAnswersLoaded,
51
  } = useFetchAllApplicationData(applicationId, dispatch);
52
53
  const application = useApplication(applicationId);
54
  const user = useApplicationUser(applicationId);
55
  const jobId = application?.job_poster_id;
56
  const job = useJob(jobId);
57
  const criteria = useCriteria(jobId);
58
  const experiences = useExperiences(applicationId, application);
59
  const experienceSkills = useExperienceSkills(applicationId, application);
60
  const questions = useJobPosterQuestions(jobId);
61
  const answers = useJobApplicationAnswers(applicationId);
62
  const skills = useSkills();
63
  const steps = useJobApplicationSteps();
64
65
  const stepsAreUpdating = useTouchApplicationStep(
66
    applicationId,
67
    "review",
68
    dispatch,
69
  );
70
71
  const handleSave = (values: ReviewFormValues): Promise<void> => {
72
    if (application === null) {
73
      // We shouldn't expect this to handler to trigger before application is loaded, but just to be sure.
74
      return Promise.reject();
75
    }
76
    return dispatch(
77
      updateApplicationAction({
78
        ...application,
79
        share_with_managers: values.shareWithManagers,
80
      }),
81
    )
82
      .then(() => {
83
        navigate(applicationSubmission(locale, applicationId));
84
      })
85
      .catch(() => {
86
        // Do nothing on an error.
87
      });
88
  };
89
  const handleReturn = (): void => {
90
    navigate(applicationFit(locale, applicationId));
91
  };
92
  const handleQuit = (): void => {
93
    // Because the Applications Index is outside of the Application SPA, we navigate to it differently.
94
    window.location.href = applicationIndex(locale);
95
  };
96
  const handleContinue = async (): Promise<void> => {
97
    navigate(applicationSubmission(locale, applicationId));
98
  };
99
100
  const closeDate = job?.close_date_time ?? null;
101
102
  const allDataLoaded =
103
    application !== null &&
104
    job !== null &&
105
    user !== null &&
106
    criteriaLoaded &&
107
    experiencesLoaded &&
108
    experienceSkillsLoaded &&
109
    jobQuestionsLoaded &&
110
    applicationAnswersLoaded &&
111
    skillsLoaded;
112
113
  return (
114
    <>
115
      {application !== null && (
116
        <ProgressBar
117
          closeDateTime={closeDate}
118
          currentTitle={intl.formatMessage(stepNames.step05)}
119
          steps={makeProgressBarSteps(
120
            applicationId,
121
            steps,
122
            intl,
123
            "review",
124
            stepsAreUpdating,
125
          )}
126
        />
127
      )}
128
      {allDataLoaded &&
129
      application !== null &&
130
      job !== null &&
131
      user !== null ? (
132
        <Review
133
          application={application}
134
          criteria={criteria}
135
          experiences={experiences}
136
          experienceSkills={experienceSkills}
137
          job={job}
138
          jobQuestions={questions}
139
          jobApplicationAnswers={answers}
140
          skills={skills}
141
          user={user}
142
          handleSave={handleSave}
143
          handleContinue={handleContinue}
144
          handleQuit={handleQuit}
145
          handleReturn={handleReturn}
146
        />
147
      ) : (
148
        <h2
149
          data-c-heading="h2"
150
          data-c-align="center"
151
          data-c-padding="top(2) bottom(3)"
152
        >
153
          {intl.formatMessage(loadingMessages.loading)}
154
        </h2>
155
      )}
156
    </>
157
  );
158
};
159
160
export default ReviewPage;
161