Passed
Push — task/application-handle-step-s... ( f0d296...3e8455 )
by Yonathan
04:28
created

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

Complexity

Total Complexity 5
Complexity/F 0

Size

Lines of Code 119
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 99
mnd 5
bc 5
fnc 0
dl 0
loc 119
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 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 } 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
27
interface BasicInfoPageProps {
28
  applicationId: number;
29
}
30
31
const BasicInfoPage: React.FunctionComponent<BasicInfoPageProps> = ({
32
  applicationId,
33
}) => {
34
  const intl = useIntl();
35
  const locale = getLocale(intl.locale);
36
  const dispatch = useDispatch<DispatchType>();
37
38
  // Fetch all un-loaded data that may be required for the Application.
39
  useFetchAllApplicationData(applicationId, dispatch);
40
41
  const application = useApplication(applicationId);
42
  const jobId = application?.job_poster_id;
43
  const job = useJob(jobId);
44
  const steps = useJobApplicationSteps();
45
46
  const stepsAreUpdating = useTouchApplicationStep(
47
    applicationId,
48
    "basic",
49
    dispatch,
50
  );
51
52
  const updateApplication = async (
53
    editedApplication: ApplicationNormalized,
54
  ): Promise<ApplicationNormalized> => {
55
    const result = await dispatch(updateApplicationAction(editedApplication));
56
    if (!result.error) {
57
      const payload = await result.payload;
58
      return payload;
59
    }
60
    return Promise.reject(result.payload);
61
  };
62
63
  const handleContinue = async (
64
    values: ApplicationNormalized,
65
  ): Promise<void> => {
66
    await updateApplication(values);
67
    navigate(applicationExperienceIntro(locale, applicationId));
68
  };
69
  const handleReturn = async (values: ApplicationNormalized): Promise<void> => {
70
    await updateApplication(values);
71
    navigate(applicationWelcome(locale, applicationId));
72
  };
73
  const handleQuit = async (values: ApplicationNormalized): Promise<void> => {
74
    await updateApplication(values);
75
    // Because the Applications Index is outside of the Application SPA, we navigate to it differently.
76
    window.location.href = applicationIndex(locale);
77
  };
78
79
  const closeDate = job?.close_date_time ?? null;
80
  const showLoadingState = application === null || job === null;
81
  return (
82
    <>
83
      {application !== null && (
84
        <ProgressBar
85
          closeDateTime={closeDate}
86
          currentTitle={intl.formatMessage(stepNames.step01)}
87
          steps={makeProgressBarSteps(
88
            applicationId,
89
            steps,
90
            intl,
91
            "basic",
92
            stepsAreUpdating,
93
          )}
94
        />
95
      )}
96
      {showLoadingState && (
97
        <h2
98
          data-c-heading="h2"
99
          data-c-align="center"
100
          data-c-padding="top(2) bottom(3)"
101
        >
102
          {intl.formatMessage(loadingMessages.loading)}
103
        </h2>
104
      )}
105
      {application !== null && job !== null && (
106
        <BasicInfo
107
          application={application}
108
          job={job}
109
          handleContinue={handleContinue}
110
          handleReturn={handleReturn}
111
          handleQuit={handleQuit}
112
        />
113
      )}
114
    </>
115
  );
116
};
117
118
export default BasicInfoPage;
119