Passed
Push — dev ( e765b6...b9d1fa )
by
unknown
05:15
created

resources/assets/js/components/Application/FinalSubmit/FinalSubmitPage.tsx   A

Complexity

Total Complexity 5
Complexity/F 0

Size

Lines of Code 143
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 118
mnd 5
bc 5
fnc 0
dl 0
loc 143
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
/* eslint-disable camelcase */
2
import React, { useState } 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
  applicationNextSteps,
10
  applicationReview,
11
} from "../../../helpers/routes";
12
import makeProgressBarSteps from "../ProgressBar/progressHelpers";
13
import ProgressBar, { stepNames } from "../ProgressBar/ProgressBar";
14
import { ApplicationNormalized } from "../../../models/types";
15
import FinalSubmit from "./FinalSubmit";
16
import {
17
  useApplication,
18
  useFetchAllApplicationData,
19
  useJob,
20
  useJobApplicationSteps,
21
  useTouchApplicationStep,
22
} from "../../../hooks/applicationHooks";
23
import { submitApplication as submitApplicationAction } from "../../../store/Application/applicationActions";
24
import { loadingMessages } from "../applicationMessages";
25
import keyFromString from "../../../helpers/components";
26
27
interface FinalSubmitPageProps {
28
  applicationId: number;
29
}
30
31
export const FinalSubmitPage: React.FunctionComponent<FinalSubmitPageProps> = ({
32
  applicationId,
33
}) => {
34
  const intl = useIntl();
35
  const locale = getLocale(intl.locale);
36
  const dispatch = useDispatch();
37
  const [applicationErrors, setApplicationErrors] = useState<string[]>([]);
38
39
  // Fetch all un-loaded data that may be required for the Application.
40
  useFetchAllApplicationData(applicationId, dispatch);
41
42
  const application = useApplication(applicationId);
43
  const jobId = application?.job_poster_id;
44
  const job = useJob(jobId);
45
  const steps = useJobApplicationSteps();
46
  const applicationIsValid = steps.review === "complete";
47
48
  const stepsAreUpdating = useTouchApplicationStep(
49
    applicationId,
50
    "submission",
51
    dispatch,
52
  );
53
54
  const submitApplication = async (
55
    completeApplication: ApplicationNormalized,
56
  ): Promise<void> => {
57
    const result = await dispatch(submitApplicationAction(completeApplication));
58
    if (result.error) {
59
      const errors = await result.payload.response.errors;
60
      setApplicationErrors(errors);
61
      return Promise.reject(result.payload);
62
    }
63
    return Promise.resolve();
64
  };
65
66
  const handleSubmit = async (
67
    completeApplication: ApplicationNormalized,
68
  ): Promise<void> => {
69
    await submitApplication(completeApplication).then(() => {
70
      // If the application had been submitted successfully then move to congrats page.
71
      // Because the Applications Index is outside of the Application SPA, we navigate to it differently.
72
      window.location.href = applicationNextSteps(locale, applicationId);
73
    });
74
  };
75
  const handleReturn = (): void => {
76
    navigate(applicationReview(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.step06)}
91
          steps={makeProgressBarSteps(
92
            applicationId,
93
            steps,
94
            intl,
95
            "submission",
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
        <>
111
          {applicationErrors.length !== 0 && (
112
            <div
113
              data-c-container="medium"
114
              data-c-margin="top(1)"
115
              data-c-alert="error"
116
              data-c-radius="rounded"
117
              role="alert"
118
            >
119
              <div data-c-padding="all(.5)">
120
                <ul>
121
                  {applicationErrors.flat().map((error) => {
122
                    const key = keyFromString(error);
123
                    return <li key={key}>{error}</li>;
124
                  })}
125
                </ul>
126
              </div>
127
            </div>
128
          )}
129
          <FinalSubmit
130
            application={application}
131
            applicationIsValid={applicationIsValid}
132
            submitApplication={handleSubmit}
133
            handleReturn={handleReturn}
134
            handleQuit={handleQuit}
135
          />
136
        </>
137
      )}
138
    </>
139
  );
140
};
141
142
export default FinalSubmitPage;
143