Passed
Push — dev ( dbcddb...05b9a5 )
by
unknown
04:35
created

resources/assets/js/components/Application/BasicInfo/BasicInfoPage.tsx   A

Complexity

Total Complexity 5
Complexity/F 0

Size

Lines of Code 121
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 102
mnd 5
bc 5
fnc 0
dl 0
loc 121
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
/* eslint-disable camelcase */
2
import React from "react";
3
import { useIntl } from "react-intl";
4
import { useDispatch } from "react-redux";
5
import BasicInfo from "./BasicInfo";
6
import makeProgressBarSteps from "../ProgressBar/progressHelpers";
7
import ProgressBar, { stepNames } from "../ProgressBar/ProgressBar";
8
import { getLocale } from "../../../helpers/localize";
9
import { navigate } from "../../../helpers/router";
10
import {
11
  applicationIndex,
12
  applicationExperienceIntro,
13
  applicationWelcome,
14
} from "../../../helpers/routes";
15
import { ApplicationNormalized, Classification } from "../../../models/types";
16
import { DispatchType } from "../../../configureStore";
17
import { updateApplication as updateApplicationAction } from "../../../store/Application/applicationActions";
18
import { loadingMessages } from "../applicationMessages";
19
import {
20
  useApplication,
21
  useFetchAllApplicationData,
22
  useJob,
23
  useJobApplicationSteps,
24
  useTouchApplicationStep,
25
} from "../../../hooks/applicationHooks";
26
import { useLoadClassifications } from "../../../hooks/classificationHooks";
27
28
interface BasicInfoPageProps {
29
  applicationId: number;
30
}
31
32
const BasicInfoPage: React.FunctionComponent<BasicInfoPageProps> = ({
33
  applicationId,
34
}) => {
35
  const intl = useIntl();
36
  const locale = getLocale(intl.locale);
37
  const dispatch = useDispatch<DispatchType>();
38
39
  // Fetch all un-loaded data that may be required for the Application.
40
  useFetchAllApplicationData(applicationId, dispatch);
41
42
  const { classifications } = useLoadClassifications(dispatch);
43
  const application = useApplication(applicationId);
44
  const jobId = application?.job_poster_id;
45
  const job = useJob(jobId);
46
  const steps = useJobApplicationSteps();
47
  const classification: Classification | null =
48
    classifications.find(
49
      (item: Classification) => item.id === job?.classification_id,
50
    ) || null;
51
52
  const stepsAreUpdating = useTouchApplicationStep(
53
    applicationId,
54
    "basic",
55
    dispatch,
56
  );
57
58
  const updateApplication = async (
59
    editedApplication: ApplicationNormalized,
60
  ): Promise<void> => {
61
    await dispatch(updateApplicationAction(editedApplication));
62
  };
63
64
  const handleContinue = async (
65
    values: ApplicationNormalized,
66
  ): Promise<void> => {
67
    await updateApplication(values);
68
    navigate(applicationExperienceIntro(locale, applicationId));
69
  };
70
  const handleReturn = async (values: ApplicationNormalized): Promise<void> => {
71
    await updateApplication(values);
72
    navigate(applicationWelcome(locale, applicationId));
73
  };
74
  const handleQuit = async (values: ApplicationNormalized): Promise<void> => {
75
    await updateApplication(values);
76
    // Because the Applications Index is outside of the Application SPA, we navigate to it differently.
77
    window.location.href = applicationIndex(locale);
78
  };
79
80
  const closeDate = job?.close_date_time ?? null;
81
  const showLoadingState = application === null || job === null;
82
  return (
83
    <>
84
      {application !== null && (
85
        <ProgressBar
86
          closeDateTime={closeDate}
87
          currentTitle={intl.formatMessage(stepNames.step01)}
88
          steps={makeProgressBarSteps(
89
            applicationId,
90
            steps,
91
            intl,
92
            "basic",
93
            stepsAreUpdating,
94
          )}
95
        />
96
      )}
97
      {showLoadingState && (
98
        <h2
99
          data-c-heading="h2"
100
          data-c-align="center"
101
          data-c-padding="top(2) bottom(3)"
102
        >
103
          {intl.formatMessage(loadingMessages.loading)}
104
        </h2>
105
      )}
106
      {application !== null && job !== null && (
107
        <BasicInfo
108
          application={application}
109
          job={job}
110
          classification={classification}
111
          handleContinue={handleContinue}
112
          handleReturn={handleReturn}
113
          handleQuit={handleQuit}
114
        />
115
      )}
116
    </>
117
  );
118
};
119
120
export default BasicInfoPage;
121