- Introduced `updateSkillMentorStatus` and `updateSkillLearningStatus` functions to manage mentor and learning preferences for skills. - Updated `SkillEvaluationCard` to include buttons for toggling mentor and learning statuses with corresponding icons. - Enhanced `SkillEvaluationGrid` and `SkillEvaluation` components to pass new status update handlers. - Modified `SkillEvaluation` type to accommodate new properties for mentor and learning status.
278 lines
7.3 KiB
TypeScript
278 lines
7.3 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useEffect } from "react";
|
|
import {
|
|
UserEvaluation,
|
|
SkillCategory,
|
|
Team,
|
|
CategoryEvaluation,
|
|
UserProfile,
|
|
SkillLevel,
|
|
} from "@/lib/types";
|
|
import {
|
|
loadUserEvaluation,
|
|
saveUserEvaluation,
|
|
createEmptyEvaluation,
|
|
} from "@/lib/evaluation-utils";
|
|
import { loadSkillCategories, loadTeams } from "@/lib/data-loader";
|
|
|
|
// Fonction pour migrer une évaluation existante avec de nouvelles catégories
|
|
function migrateEvaluation(
|
|
evaluation: UserEvaluation,
|
|
allCategories: SkillCategory[]
|
|
): UserEvaluation {
|
|
const existingCategoryNames = evaluation.evaluations.map((e) => e.category);
|
|
const missingCategories = allCategories.filter(
|
|
(cat) => !existingCategoryNames.includes(cat.category)
|
|
);
|
|
|
|
if (missingCategories.length === 0) {
|
|
return evaluation; // Pas de migration nécessaire
|
|
}
|
|
|
|
console.log(
|
|
"🔄 Migrating evaluation with new categories:",
|
|
missingCategories.map((c) => c.category)
|
|
);
|
|
|
|
const newCategoryEvaluations: CategoryEvaluation[] = missingCategories.map(
|
|
(category) => ({
|
|
category: category.category,
|
|
skills: [],
|
|
selectedSkillIds: [],
|
|
})
|
|
);
|
|
|
|
return {
|
|
...evaluation,
|
|
evaluations: [...evaluation.evaluations, ...newCategoryEvaluations],
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
}
|
|
|
|
export function useEvaluation() {
|
|
const [userEvaluation, setUserEvaluation] = useState<UserEvaluation | null>(
|
|
null
|
|
);
|
|
const [skillCategories, setSkillCategories] = useState<SkillCategory[]>([]);
|
|
const [teams, setTeams] = useState<Team[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
// Load initial data
|
|
useEffect(() => {
|
|
async function initializeData() {
|
|
try {
|
|
const [categories, teamsData] = await Promise.all([
|
|
loadSkillCategories(),
|
|
loadTeams(),
|
|
]);
|
|
|
|
setSkillCategories(categories);
|
|
setTeams(teamsData);
|
|
|
|
// Try to load existing evaluation
|
|
const saved = loadUserEvaluation();
|
|
if (saved) {
|
|
// Migrate evaluation to include new categories if needed
|
|
const migratedEvaluation = migrateEvaluation(saved, categories);
|
|
setUserEvaluation(migratedEvaluation);
|
|
saveUserEvaluation(migratedEvaluation); // Save the migrated version
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to initialize data:", error);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
initializeData();
|
|
}, []);
|
|
|
|
const updateProfile = (profile: UserProfile) => {
|
|
const evaluations =
|
|
userEvaluation?.evaluations || createEmptyEvaluation(skillCategories);
|
|
const newEvaluation: UserEvaluation = {
|
|
profile,
|
|
evaluations,
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
setUserEvaluation(newEvaluation);
|
|
saveUserEvaluation(newEvaluation);
|
|
};
|
|
|
|
const updateSkillLevel = (
|
|
category: string,
|
|
skillId: string,
|
|
level: SkillLevel
|
|
) => {
|
|
if (!userEvaluation) return;
|
|
|
|
const updatedEvaluations = userEvaluation.evaluations.map((catEval) => {
|
|
if (catEval.category === category) {
|
|
const existingSkill = catEval.skills.find((s) => s.skillId === skillId);
|
|
const updatedSkills = existingSkill
|
|
? catEval.skills.map((skill) =>
|
|
skill.skillId === skillId ? { ...skill, level } : skill
|
|
)
|
|
: [...catEval.skills, { skillId, level }];
|
|
|
|
return {
|
|
...catEval,
|
|
skills: updatedSkills,
|
|
};
|
|
}
|
|
return catEval;
|
|
});
|
|
|
|
const newEvaluation: UserEvaluation = {
|
|
...userEvaluation,
|
|
evaluations: updatedEvaluations,
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
setUserEvaluation(newEvaluation);
|
|
saveUserEvaluation(newEvaluation);
|
|
};
|
|
|
|
const updateSkillMentorStatus = (
|
|
category: string,
|
|
skillId: string,
|
|
canMentor: boolean
|
|
) => {
|
|
if (!userEvaluation) return;
|
|
|
|
const updatedEvaluations = userEvaluation.evaluations.map((catEval) => {
|
|
if (catEval.category === category) {
|
|
const updatedSkills = catEval.skills.map((skill) =>
|
|
skill.skillId === skillId ? { ...skill, canMentor } : skill
|
|
);
|
|
|
|
return {
|
|
...catEval,
|
|
skills: updatedSkills,
|
|
};
|
|
}
|
|
return catEval;
|
|
});
|
|
|
|
const newEvaluation: UserEvaluation = {
|
|
...userEvaluation,
|
|
evaluations: updatedEvaluations,
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
setUserEvaluation(newEvaluation);
|
|
saveUserEvaluation(newEvaluation);
|
|
};
|
|
|
|
const updateSkillLearningStatus = (
|
|
category: string,
|
|
skillId: string,
|
|
wantsToLearn: boolean
|
|
) => {
|
|
if (!userEvaluation) return;
|
|
|
|
const updatedEvaluations = userEvaluation.evaluations.map((catEval) => {
|
|
if (catEval.category === category) {
|
|
const updatedSkills = catEval.skills.map((skill) =>
|
|
skill.skillId === skillId ? { ...skill, wantsToLearn } : skill
|
|
);
|
|
|
|
return {
|
|
...catEval,
|
|
skills: updatedSkills,
|
|
};
|
|
}
|
|
return catEval;
|
|
});
|
|
|
|
const newEvaluation: UserEvaluation = {
|
|
...userEvaluation,
|
|
evaluations: updatedEvaluations,
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
setUserEvaluation(newEvaluation);
|
|
saveUserEvaluation(newEvaluation);
|
|
};
|
|
|
|
const addSkillToEvaluation = (category: string, skillId: string) => {
|
|
if (!userEvaluation) return;
|
|
|
|
const updatedEvaluations = userEvaluation.evaluations.map((catEval) => {
|
|
if (catEval.category === category) {
|
|
if (!catEval.selectedSkillIds.includes(skillId)) {
|
|
return {
|
|
...catEval,
|
|
selectedSkillIds: [...catEval.selectedSkillIds, skillId],
|
|
skills: [...catEval.skills, { skillId, level: null }],
|
|
};
|
|
}
|
|
}
|
|
return catEval;
|
|
});
|
|
|
|
const newEvaluation: UserEvaluation = {
|
|
...userEvaluation,
|
|
evaluations: updatedEvaluations,
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
setUserEvaluation(newEvaluation);
|
|
saveUserEvaluation(newEvaluation);
|
|
};
|
|
|
|
const removeSkillFromEvaluation = (category: string, skillId: string) => {
|
|
if (!userEvaluation) return;
|
|
|
|
const updatedEvaluations = userEvaluation.evaluations.map((catEval) => {
|
|
if (catEval.category === category) {
|
|
return {
|
|
...catEval,
|
|
selectedSkillIds: catEval.selectedSkillIds.filter(
|
|
(id) => id !== skillId
|
|
),
|
|
skills: catEval.skills.filter((skill) => skill.skillId !== skillId),
|
|
};
|
|
}
|
|
return catEval;
|
|
});
|
|
|
|
const newEvaluation: UserEvaluation = {
|
|
...userEvaluation,
|
|
evaluations: updatedEvaluations,
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
setUserEvaluation(newEvaluation);
|
|
saveUserEvaluation(newEvaluation);
|
|
};
|
|
|
|
const initializeEmptyEvaluation = (profile: UserProfile) => {
|
|
const evaluations = createEmptyEvaluation(skillCategories);
|
|
const newEvaluation: UserEvaluation = {
|
|
profile,
|
|
evaluations,
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
setUserEvaluation(newEvaluation);
|
|
saveUserEvaluation(newEvaluation);
|
|
};
|
|
|
|
return {
|
|
userEvaluation,
|
|
skillCategories,
|
|
teams,
|
|
loading,
|
|
updateProfile,
|
|
updateSkillLevel,
|
|
updateSkillMentorStatus,
|
|
updateSkillLearningStatus,
|
|
addSkillToEvaluation,
|
|
removeSkillFromEvaluation,
|
|
initializeEmptyEvaluation,
|
|
};
|
|
}
|