Files
peakskills/lib/evaluation-utils.ts
Julien Froidefond 4e82a6d860 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`.
2025-08-21 08:46:52 +02:00

79 lines
1.9 KiB
TypeScript

import {
SkillLevel,
SKILL_LEVEL_VALUES,
CategoryEvaluation,
RadarChartData,
UserEvaluation,
SkillCategory,
} from "./types";
import { apiClient } from "../services/api-client";
export function calculateCategoryScore(
categoryEvaluation: CategoryEvaluation
): number {
if (categoryEvaluation.skills.length === 0) return 0;
const evaluatedSkills = categoryEvaluation.skills.filter(
(skill) => skill.level !== null
);
if (evaluatedSkills.length === 0) return 0;
const totalScore = evaluatedSkills.reduce((sum, skill) => {
return sum + SKILL_LEVEL_VALUES[skill.level!];
}, 0);
return totalScore / evaluatedSkills.length;
}
export function generateRadarData(
evaluations: CategoryEvaluation[],
categories: SkillCategory[]
): RadarChartData[] {
const maxScore = 3; // Expert level
return categories.map((category) => {
const evaluation = evaluations.find(
(e) => e.category === category.category
);
const score = evaluation ? calculateCategoryScore(evaluation) : 0;
return {
category: category.category,
score: Math.round(score * 10) / 10, // Round to 1 decimal
maxScore,
};
});
}
export async function saveUserEvaluation(
evaluation: UserEvaluation
): Promise<void> {
try {
await apiClient.saveUserEvaluation(evaluation);
} catch (error) {
console.error("Failed to save user evaluation:", error);
throw error;
}
}
export async function loadUserEvaluation(
profile: UserEvaluation["profile"]
): Promise<UserEvaluation | null> {
try {
return await apiClient.loadUserEvaluation(profile);
} catch (error) {
console.error("Failed to load user evaluation:", error);
return null;
}
}
export function createEmptyEvaluation(
categories: SkillCategory[]
): CategoryEvaluation[] {
return categories.map((category) => ({
category: category.category,
skills: [],
selectedSkillIds: [],
}));
}