Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 1dbb89c

Browse files
committed
Fixes issue #481 re-adding skill after adding and removing
1 parent ceb0097 commit 1dbb89c

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

client/src/components/EditProfileModal/index.jsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,27 @@ export default function EditProfileModal({
6262
*/
6363
const addSkill = (skill) => {
6464
// Verify that the skill does not already exist on the user
65-
const exists = localUser.skills.find(
65+
const index = localUser.skills.findIndex(
6666
(existingSkill) => existingSkill.externalId === skill.id
6767
);
68+
const exists = localUser.skills[index];
6869

69-
if (exists) {
70+
if (exists && !exists.isDeleted) {
7071
return;
7172
}
7273

7374
const skills = JSON.parse(JSON.stringify(localUser.skills));
7475

75-
skills.push({
76-
externalId: skill.id, // The skill id returned from EMSI becomes externalId in our db
77-
isNew: true,
78-
name: skill.name,
79-
skillProviderId: config.EMSI_SKILLPROVIDER_ID,
80-
});
76+
if (exists) {
77+
delete skills[index].isDeleted;
78+
} else {
79+
skills.push({
80+
externalId: skill.id, // The skill id returned from EMSI becomes externalId in our db
81+
isNew: true,
82+
name: skill.name,
83+
skillProviderId: config.EMSI_SKILLPROVIDER_ID,
84+
});
85+
}
8186

8287
setLocalUser({ ...localUser, skills });
8388
};

client/src/components/ProfileCard/helper.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,27 @@ export async function updateUserSkills(apiClient, { id, skills }) {
198198
for (let i = 0; i < skills.length; i++) {
199199
const skill = skills[i];
200200
if (skills[i].isDeleted) {
201-
url = `${config.API_URL}/users/${id}/skills/${skill.id}`;
201+
let skillId;
202+
// check if there is an id
203+
if (skills[i].id) {
204+
skillId = skills[i].id;
205+
} else {
206+
// find the existing skill id to delete
207+
// skill marked to be deleted can be without id when it came directly from react state (not database)
208+
const existingSkill = await checkIfSkillExists(
209+
apiClient,
210+
skill.skillProviderId,
211+
skill.externalId
212+
);
213+
if (existingSkill && existingSkill.length > 0) {
214+
skillId = existingSkill[0].id;
215+
}
216+
}
217+
if (skillId) {
218+
url = `${config.API_URL}/users/${id}/skills/${skillId}`;
202219

203-
await apiClient.delete(url);
220+
await apiClient.delete(url);
221+
}
204222
} else if (skills[i].isNew) {
205223
let userSkill;
206224
// We first check if the skill is already defined in the database, before we add it to the user

0 commit comments

Comments
 (0)