Passed
Push — feature/applicant-profile-skil... ( 7227ab...39e9eb )
by
unknown
05:16
created

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

Complexity

Total Complexity 6
Complexity/F 0

Size

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