Passed
Push — task/application-profile-react... ( f3a6a9...9a6ac9 )
by Tristan
07:39 queued 11s
created

resources/assets/js/components/ApplicantProfile/Skills/ProfileSkillsPage.tsx   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 150
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 126
mnd 2
bc 2
fnc 0
dl 0
loc 150
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React from "react";
2
import ReactDOM from "react-dom";
3
import { FormattedMessage, useIntl } from "react-intl";
4
import { getId, notEmpty, toIdMap } from "../../../helpers/queries";
5
import {
6
  useApplicantExperience,
7
  useApplicantExperienceSkills,
8
  useApplicantSkillIds,
9
  useSkillCategories,
10
  useSkills,
11
} from "../../../hooks/apiResourceHooks";
12
import { Experience, ExperienceSkill, Skill } from "../../../models/types";
13
import { loadingMessages } from "../../Application/applicationMessages";
14
import FindSkillsModal, { FindSkillsModalTrigger } from "../../FindSkillsModal";
15
import RootContainer from "../../RootContainer";
16
import List from "./List";
17
18
export const ProfileExperiencePage: React.FC<{ applicantId: number }> = ({
19
  applicantId,
20
}) => {
21
  const intl = useIntl();
22
23
  const skillsResource = useSkills();
24
  const skillCategoriesResource = useSkillCategories();
25
  const applicantSkillsResource = useApplicantSkillIds(applicantId);
26
  const experienceResource = useApplicantExperience(applicantId);
27
  const experienceSkillResource = useApplicantExperienceSkills(applicantId);
28
29
  const idToSkill = toIdMap(skillsResource.value);
30
  const applicantSkills = applicantSkillsResource.value.skill_ids
31
    .map((skillId) => idToSkill.get(skillId))
32
    .filter(notEmpty);
33
  const experiences: Experience[] = Object.values(experienceResource.values);
34
  const experienceSkills: ExperienceSkill[] = Object.values(
35
    experienceSkillResource.values,
36
  );
37
38
  const submitNewSkills = async (newSkills: Skill[]): Promise<void> => {
39
    await applicantSkillsResource.update({
40
      skill_ids: [
41
        ...applicantSkillsResource.value.skill_ids,
42
        ...newSkills.map(getId),
43
      ],
44
    });
45
  };
46
47
  const removeSkill = async (skillId: number): Promise<void> => {
48
    await applicantSkillsResource.update({
49
      skill_ids: applicantSkillsResource.value.skill_ids.filter(
50
        (id) => id !== skillId,
51
      ),
52
    });
53
  };
54
55
  return (
56
    <div>
57
      <h2 data-h2-heading="b(h3)" data-h2-margin="b(bottom, 1)">
58
        <FormattedMessage
59
          id="profileSkillsPage.heading"
60
          defaultMessage="Your Skills"
61
        />
62
      </h2>
63
      <p data-h2-margin="b(bottom, 1)">
64
        <FormattedMessage
65
          id="profileSkillsPage.preamble"
66
          defaultMessage="This is your library of skills. Managers will try to match their needs to these skills when searching for talent, or after you apply for a job. Use the Add Skills button to start building your library of skills."
67
          description="Appears at the top of the Profile Skills page, explaining the purpose of the page."
68
        />
69
      </p>
70
      <div
71
        data-h2-grid="b(middle, expanded, flush, 1)"
72
        data-h2-margin="b(bottom, 1)"
73
      >
74
        <div data-h2-grid-item="b(1of1) m(5of6)">
75
          <div
76
            data-h2-bg-color="b(gray-1, 1)"
77
            data-h2-radius="b(round)"
78
            data-h2-padding="b(all, 1)"
79
            data-h2-grid-content
80
          >
81
            <h4 data-h2-heading="b(h4)">
82
              <FormattedMessage
83
                id="profileSkillsPage.addSkills.subtitle"
84
                defaultMessage="Find and Add Skills"
85
                description="Section title acompanying the button that opens the Explore Skills modal."
86
              />
87
            </h4>
88
            <p>
89
              <FormattedMessage
90
                id="profileSkillsPage.addSkills.explanation"
91
                defaultMessage="Explore the government's most sought after skills, find the ones that apply to you, and add them to your profile."
92
                description="Text accompanying the button that opens the Explore Skills modal."
93
              />
94
            </p>
95
          </div>
96
        </div>
97
        <div data-h2-grid-item="b(1of1) m(1of6)">
98
          <div data-h2-grid-content>
99
            <FindSkillsModalTrigger />
100
            <FindSkillsModal
101
              oldSkills={applicantSkills}
102
              portal="applicant"
103
              skills={skillsResource.value}
104
              skillCategories={skillCategoriesResource.value}
105
              handleSubmit={submitNewSkills}
106
            />
107
          </div>
108
        </div>
109
      </div>
110
      {(skillsResource.status === "pending" ||
111
        experienceResource.indexStatus === "pending" ||
112
        experienceSkillResource.indexStatus === "pending" ||
113
        skillCategoriesResource.status === "pending" ||
114
        applicantSkillsResource.status === "pending") && (
115
        <h4
116
          data-h2-heading="b(h4)"
117
          data-h2-align="center"
118
          data-h2-padding="b(bottom, 1)"
119
        >
120
          {intl.formatMessage(loadingMessages.loading)}
121
        </h4>
122
      )}
123
      <List
124
        experiences={experiences}
125
        experienceSkills={experienceSkills}
126
        skillCategories={skillCategoriesResource.value}
127
        skills={applicantSkills}
128
        applicantId={applicantId}
129
        handleDeleteSkill={removeSkill}
130
        updateInProgress={applicantSkillsResource.status === "pending"}
131
      />
132
    </div>
133
  );
134
};
135
136
if (document.getElementById("profile-skills")) {
137
  const root = document.getElementById("profile-skills");
138
  if (root && "applicantId" in root.dataset) {
139
    const applicantId = Number(root.dataset.applicantId as string);
140
    ReactDOM.render(
141
      <RootContainer>
142
        <ProfileExperiencePage applicantId={applicantId} />
143
      </RootContainer>,
144
      root,
145
    );
146
  }
147
}
148
149
export default ProfileExperiencePage;
150