Passed
Push — feature/skills-experience-tran... ( cd7b7e...d704a5 )
by Tristan
05:22
created

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

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 134
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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