Passed
Push — task/applicant-skill-save-api ( 2ebd5d...fa7a9c )
by
unknown
06:31 queued 10s
created

resources/assets/js/components/Application/Fit/FitPage.tsx   A

Complexity

Total Complexity 6
Complexity/F 0

Size

Lines of Code 125
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 105
mnd 6
bc 6
fnc 0
dl 0
loc 125
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 { getLocale } from "../../../helpers/localize";
6
import { navigate } from "../../../helpers/router";
7
import {
8
  applicationIndex,
9
  applicationReview,
10
  applicationSkills,
11
} from "../../../helpers/routes";
12
import makeProgressBarSteps from "../ProgressBar/progressHelpers";
13
import ProgressBar, { stepNames } from "../ProgressBar/ProgressBar";
14
import Fit from "./Fit";
15
import { JobApplicationAnswer } from "../../../models/types";
16
import { fetchApplication } from "../../../store/Application/applicationActions";
17
import {
18
  createJobApplicationAnswer,
19
  updateJobApplicationAnswer,
20
} from "../../../store/JobApplicationAnswer/jobApplicationAnswerActions";
21
import { loadingMessages } from "../applicationMessages";
22
import {
23
  useApplication,
24
  useFetchAllApplicationData,
25
  useJob,
26
  useJobApplicationAnswers,
27
  useJobPosterQuestions,
28
  useJobApplicationSteps,
29
  useTouchApplicationStep,
30
} from "../../../hooks/applicationHooks";
31
32
interface FitPageProps {
33
  applicationId: number;
34
}
35
36
export const FitPage: React.FunctionComponent<FitPageProps> = ({
37
  applicationId,
38
}) => {
39
  const intl = useIntl();
40
  const locale = getLocale(intl.locale);
41
  const dispatch = useDispatch();
42
43
  // Fetch all un-loaded data that may be required for the Application.
44
  useFetchAllApplicationData(applicationId, dispatch);
45
46
  const application = useApplication(applicationId);
47
  const jobId = application?.job_poster_id;
48
  const job = useJob(jobId);
49
  const jobPosterQuestions = useJobPosterQuestions(jobId);
50
  const answers = useJobApplicationAnswers(applicationId);
51
  const steps = useJobApplicationSteps();
52
53
  const stepsAreUpdating = useTouchApplicationStep(
54
    applicationId,
55
    "fit",
56
    dispatch,
57
  );
58
59
  const handleSubmit = async (answer: JobApplicationAnswer): Promise<void> => {
60
    const exists = answer.id !== -1;
61
    const result = exists
62
      ? await dispatch(updateJobApplicationAnswer(answer))
63
      : await dispatch(createJobApplicationAnswer(answer));
64
65
    if (!result.error) {
66
      const payload = await result.payload;
67
      return payload;
68
    }
69
    return Promise.reject(result.payload);
70
  };
71
72
  const handleContinue = async (): Promise<void> => {
73
    navigate(applicationReview(locale, applicationId));
74
  };
75
  const handleReturn = (): void => {
76
    navigate(applicationSkills(locale, applicationId));
77
  };
78
  const handleQuit = (): void => {
79
    // Because the Applications Index is outside of the Application SPA, we navigate to it differently.
80
    window.location.href = applicationIndex(locale);
81
  };
82
83
  const closeDate = job?.close_date_time ?? null;
84
  const showLoadingState = application === null || job === null;
85
  return (
86
    <>
87
      {application !== null && (
88
        <ProgressBar
89
          closeDateTime={closeDate}
90
          currentTitle={intl.formatMessage(stepNames.step04)}
91
          steps={makeProgressBarSteps(
92
            applicationId,
93
            steps,
94
            intl,
95
            "fit",
96
            stepsAreUpdating,
97
          )}
98
        />
99
      )}
100
      {showLoadingState && (
101
        <h2
102
          data-c-heading="h2"
103
          data-c-align="center"
104
          data-c-padding="top(2) bottom(3)"
105
        >
106
          {intl.formatMessage(loadingMessages.loading)}
107
        </h2>
108
      )}
109
      {application !== null && job !== null && (
110
        <Fit
111
          applicationId={applicationId}
112
          jobQuestions={jobPosterQuestions || []}
113
          jobApplicationAnswers={answers || []}
114
          handleSubmit={handleSubmit}
115
          handleContinue={handleContinue}
116
          handleReturn={handleReturn}
117
          handleQuit={handleQuit}
118
        />
119
      )}
120
    </>
121
  );
122
};
123
124
export default FitPage;
125