feat: add PostgreSQL support and enhance evaluation loading
- Added `pg` and `@types/pg` dependencies for PostgreSQL integration. - Updated `useEvaluation` hook to load user evaluations from the API, improving data management. - Refactored `saveUserEvaluation` and `loadUserEvaluation` functions to interact with the API instead of localStorage. - Introduced error handling for profile loading and evaluation saving to enhance robustness. - Added new methods for managing user profiles and evaluations, including `clearUserProfile` and `loadEvaluationForProfile`.
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
||||
saveUserEvaluation,
|
||||
createEmptyEvaluation,
|
||||
} from "@/lib/evaluation-utils";
|
||||
import { apiClient } from "@/services/api-client";
|
||||
import { loadSkillCategories, loadTeams } from "@/lib/data-loader";
|
||||
|
||||
// Fonction pour migrer une évaluation existante avec de nouvelles catégories
|
||||
@@ -70,13 +71,25 @@ export function useEvaluation() {
|
||||
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
|
||||
// Try to load user profile from localStorage and then load evaluation from API
|
||||
try {
|
||||
const savedProfile = localStorage.getItem("peakSkills_userProfile");
|
||||
if (savedProfile) {
|
||||
const profile: UserProfile = JSON.parse(savedProfile);
|
||||
const saved = await loadUserEvaluation(profile);
|
||||
if (saved) {
|
||||
// Migrate evaluation to include new categories if needed
|
||||
const migratedEvaluation = migrateEvaluation(saved, categories);
|
||||
setUserEvaluation(migratedEvaluation);
|
||||
if (migratedEvaluation !== saved) {
|
||||
await saveUserEvaluation(migratedEvaluation); // Save the migrated version
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (profileError) {
|
||||
console.error("Failed to load user profile:", profileError);
|
||||
// Clear invalid profile data
|
||||
localStorage.removeItem("peakSkills_userProfile");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to initialize data:", error);
|
||||
@@ -88,7 +101,35 @@ export function useEvaluation() {
|
||||
initializeData();
|
||||
}, []);
|
||||
|
||||
const updateProfile = (profile: UserProfile) => {
|
||||
const loadEvaluationForProfile = async (profile: UserProfile) => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const saved = await loadUserEvaluation(profile);
|
||||
if (saved) {
|
||||
// Migrate evaluation to include new categories if needed
|
||||
const migratedEvaluation = migrateEvaluation(saved, skillCategories);
|
||||
setUserEvaluation(migratedEvaluation);
|
||||
if (migratedEvaluation !== saved) {
|
||||
await saveUserEvaluation(migratedEvaluation); // Save the migrated version
|
||||
}
|
||||
} else {
|
||||
// Create new evaluation
|
||||
const evaluations = createEmptyEvaluation(skillCategories);
|
||||
const newEvaluation: UserEvaluation = {
|
||||
profile,
|
||||
evaluations,
|
||||
lastUpdated: new Date().toISOString(),
|
||||
};
|
||||
setUserEvaluation(newEvaluation);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to load evaluation for profile:", error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const updateProfile = async (profile: UserProfile) => {
|
||||
const evaluations =
|
||||
userEvaluation?.evaluations || createEmptyEvaluation(skillCategories);
|
||||
const newEvaluation: UserEvaluation = {
|
||||
@@ -97,159 +138,214 @@ export function useEvaluation() {
|
||||
lastUpdated: new Date().toISOString(),
|
||||
};
|
||||
|
||||
// Save profile to localStorage for auto-login
|
||||
localStorage.setItem("peakSkills_userProfile", JSON.stringify(profile));
|
||||
|
||||
setUserEvaluation(newEvaluation);
|
||||
saveUserEvaluation(newEvaluation);
|
||||
await saveUserEvaluation(newEvaluation);
|
||||
};
|
||||
|
||||
const updateSkillLevel = (
|
||||
const updateSkillLevel = async (
|
||||
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 }];
|
||||
try {
|
||||
// Optimistic update
|
||||
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;
|
||||
});
|
||||
return {
|
||||
...catEval,
|
||||
skills: updatedSkills,
|
||||
};
|
||||
}
|
||||
return catEval;
|
||||
});
|
||||
|
||||
const newEvaluation: UserEvaluation = {
|
||||
...userEvaluation,
|
||||
evaluations: updatedEvaluations,
|
||||
lastUpdated: new Date().toISOString(),
|
||||
};
|
||||
const newEvaluation: UserEvaluation = {
|
||||
...userEvaluation,
|
||||
evaluations: updatedEvaluations,
|
||||
lastUpdated: new Date().toISOString(),
|
||||
};
|
||||
|
||||
setUserEvaluation(newEvaluation);
|
||||
saveUserEvaluation(newEvaluation);
|
||||
setUserEvaluation(newEvaluation);
|
||||
|
||||
// Update via API
|
||||
await apiClient.updateSkillLevel(
|
||||
userEvaluation.profile,
|
||||
category,
|
||||
skillId,
|
||||
level
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Failed to update skill level:", error);
|
||||
// Revert optimistic update if needed
|
||||
}
|
||||
};
|
||||
|
||||
const updateSkillMentorStatus = (
|
||||
const updateSkillMentorStatus = async (
|
||||
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
|
||||
);
|
||||
try {
|
||||
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;
|
||||
});
|
||||
return {
|
||||
...catEval,
|
||||
skills: updatedSkills,
|
||||
};
|
||||
}
|
||||
return catEval;
|
||||
});
|
||||
|
||||
const newEvaluation: UserEvaluation = {
|
||||
...userEvaluation,
|
||||
evaluations: updatedEvaluations,
|
||||
lastUpdated: new Date().toISOString(),
|
||||
};
|
||||
const newEvaluation: UserEvaluation = {
|
||||
...userEvaluation,
|
||||
evaluations: updatedEvaluations,
|
||||
lastUpdated: new Date().toISOString(),
|
||||
};
|
||||
|
||||
setUserEvaluation(newEvaluation);
|
||||
saveUserEvaluation(newEvaluation);
|
||||
setUserEvaluation(newEvaluation);
|
||||
await apiClient.updateSkillMentorStatus(
|
||||
userEvaluation.profile,
|
||||
category,
|
||||
skillId,
|
||||
canMentor
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Failed to update skill mentor status:", error);
|
||||
}
|
||||
};
|
||||
|
||||
const updateSkillLearningStatus = (
|
||||
const updateSkillLearningStatus = async (
|
||||
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
|
||||
);
|
||||
try {
|
||||
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 }],
|
||||
skills: updatedSkills,
|
||||
};
|
||||
}
|
||||
}
|
||||
return catEval;
|
||||
});
|
||||
return catEval;
|
||||
});
|
||||
|
||||
const newEvaluation: UserEvaluation = {
|
||||
...userEvaluation,
|
||||
evaluations: updatedEvaluations,
|
||||
lastUpdated: new Date().toISOString(),
|
||||
};
|
||||
const newEvaluation: UserEvaluation = {
|
||||
...userEvaluation,
|
||||
evaluations: updatedEvaluations,
|
||||
lastUpdated: new Date().toISOString(),
|
||||
};
|
||||
|
||||
setUserEvaluation(newEvaluation);
|
||||
saveUserEvaluation(newEvaluation);
|
||||
setUserEvaluation(newEvaluation);
|
||||
await apiClient.updateSkillLearningStatus(
|
||||
userEvaluation.profile,
|
||||
category,
|
||||
skillId,
|
||||
wantsToLearn
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Failed to update skill learning status:", error);
|
||||
}
|
||||
};
|
||||
|
||||
const removeSkillFromEvaluation = (category: string, skillId: string) => {
|
||||
const addSkillToEvaluation = async (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;
|
||||
});
|
||||
try {
|
||||
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(),
|
||||
};
|
||||
const newEvaluation: UserEvaluation = {
|
||||
...userEvaluation,
|
||||
evaluations: updatedEvaluations,
|
||||
lastUpdated: new Date().toISOString(),
|
||||
};
|
||||
|
||||
setUserEvaluation(newEvaluation);
|
||||
saveUserEvaluation(newEvaluation);
|
||||
setUserEvaluation(newEvaluation);
|
||||
await apiClient.addSkillToEvaluation(
|
||||
userEvaluation.profile,
|
||||
category,
|
||||
skillId
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Failed to add skill to evaluation:", error);
|
||||
}
|
||||
};
|
||||
|
||||
const initializeEmptyEvaluation = (profile: UserProfile) => {
|
||||
const removeSkillFromEvaluation = async (
|
||||
category: string,
|
||||
skillId: string
|
||||
) => {
|
||||
if (!userEvaluation) return;
|
||||
|
||||
try {
|
||||
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);
|
||||
await apiClient.removeSkillFromEvaluation(
|
||||
userEvaluation.profile,
|
||||
category,
|
||||
skillId
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Failed to remove skill from evaluation:", error);
|
||||
}
|
||||
};
|
||||
|
||||
const initializeEmptyEvaluation = async (profile: UserProfile) => {
|
||||
const evaluations = createEmptyEvaluation(skillCategories);
|
||||
const newEvaluation: UserEvaluation = {
|
||||
profile,
|
||||
@@ -257,8 +353,16 @@ export function useEvaluation() {
|
||||
lastUpdated: new Date().toISOString(),
|
||||
};
|
||||
|
||||
// Save profile to localStorage for auto-login
|
||||
localStorage.setItem("peakSkills_userProfile", JSON.stringify(profile));
|
||||
|
||||
setUserEvaluation(newEvaluation);
|
||||
saveUserEvaluation(newEvaluation);
|
||||
await saveUserEvaluation(newEvaluation);
|
||||
};
|
||||
|
||||
const clearUserProfile = () => {
|
||||
localStorage.removeItem("peakSkills_userProfile");
|
||||
setUserEvaluation(null);
|
||||
};
|
||||
|
||||
return {
|
||||
@@ -266,6 +370,7 @@ export function useEvaluation() {
|
||||
skillCategories,
|
||||
teams,
|
||||
loading,
|
||||
loadEvaluationForProfile,
|
||||
updateProfile,
|
||||
updateSkillLevel,
|
||||
updateSkillMentorStatus,
|
||||
@@ -273,5 +378,6 @@ export function useEvaluation() {
|
||||
addSkillToEvaluation,
|
||||
removeSkillFromEvaluation,
|
||||
initializeEmptyEvaluation,
|
||||
clearUserProfile,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user