Passed
Push — task/application-handle-step-s... ( 5f39f9 )
by Yonathan
08:23
created

resources/assets/js/components/Application/RedirectToLastTouchedStep.tsx   A

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 111
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 97
c 0
b 0
f 0
dl 0
loc 111
rs 10
mnd 4
bc 4
fnc 0
bpm 0
cpm 0
noi 0
1
import React, { useEffect } from "react";
2
import { useIntl } from "react-intl";
3
import { useDispatch } from "react-redux";
4
import { getLocale } from "../../helpers/localize";
5
import { redirect } from "../../helpers/router";
6
import {
7
  applicationBasic,
8
  applicationExperience,
9
  applicationFit,
10
  applicationReview,
11
  applicationSkills,
12
  applicationSubmission,
13
  applicationWelcome,
14
} from "../../helpers/routes";
15
import {
16
  useApplication,
17
  useFetchAllApplicationData,
18
  useFetchNormalizedApplication,
19
  useJob,
20
  useSteps,
21
} from "../../hooks/applicationHooks";
22
import {
23
  ApplicationStepId,
24
  ApplicationStep,
25
} from "../../models/lookupConstants";
26
import { loadingMessages } from "./applicationMessages";
27
import BasicInfoPage from "./BasicInfo/BasicInfoPage";
28
import ExperiencePage from "./Experience/ExperiencePage";
29
import FinalSubmitPage from "./FinalSubmit/FinalSubmitPage";
30
import FitPage from "./Fit/FitPage";
31
import IntroPage from "./Intro/IntroPage";
32
import ReviewPage from "./Review/ReviewPage";
33
import SkillsPage from "./Skills/SkillsPage";
34
35
interface RedirectToLastTouchedStepProps {
36
  applicationId: number;
37
}
38
39
const RedirectToLastTouchedStep: React.FunctionComponent<RedirectToLastTouchedStepProps> = ({
40
  applicationId,
41
}) => {
42
  const intl = useIntl();
43
  const locale = getLocale(intl.locale);
44
  const dispatch = useDispatch();
45
46
  useFetchAllApplicationData(applicationId, dispatch);
47
  const application = useApplication(applicationId);
48
  const job = useJob(application?.job_poster_id);
49
  const steps = useSteps();
50
51
  const stepOrder: ApplicationStep[] = [
52
    "basic",
53
    "experience",
54
    "skills",
55
    "fit",
56
    "review",
57
    "submission",
58
  ];
59
60
  const getStepComponent = (step: ApplicationStep | null) => {
61
    switch (step) {
62
      case "basic":
63
        return <BasicInfoPage applicationId={applicationId} />;
64
      case "experience":
65
        return <ExperiencePage applicationId={applicationId} />;
66
      case "skills":
67
        return <SkillsPage applicationId={applicationId} />;
68
      case "fit":
69
        return <FitPage applicationId={applicationId} />;
70
      case "review":
71
        return <ReviewPage applicationId={applicationId} />;
72
      case "submission":
73
        return <FinalSubmitPage applicationId={applicationId} />;
74
      default:
75
        return <IntroPage applicationId={applicationId} />;
76
    }
77
  };
78
79
  const lastTouchedStep = (): React.ReactElement => {
80
    const reversedStepOrder = stepOrder.reverse();
81
    for (let i = 0; i <= reversedStepOrder.length; i += 1) {
82
      const step = reversedStepOrder[i];
83
      if (steps[step] === "complete" || steps[step] === "error") {
84
        return getStepComponent(step);
85
      }
86
    }
87
    return getStepComponent(null);
88
  };
89
90
  const showLoadingState = application === null || job === null;
91
  return (
92
    <>
93
      {showLoadingState && (
94
        <h2
95
          data-c-heading="h2"
96
          data-c-align="center"
97
          data-c-padding="top(2) bottom(3)"
98
        >
99
          {intl.formatMessage(loadingMessages.loading)}
100
        </h2>
101
      )}
102
      {application !== null &&
103
        job !== null &&
104
        steps !== null &&
105
        lastTouchedStep()}
106
    </>
107
  );
108
};
109
110
export default RedirectToLastTouchedStep;
111