import { UserEvaluation, UserProfile, SkillLevel } from "../lib/types"; export class ApiClient { private baseUrl: string; constructor() { this.baseUrl = process.env.NEXT_PUBLIC_API_URL || ""; } /** * Charge une évaluation utilisateur depuis l'API */ async loadUserEvaluation( profile: UserProfile ): Promise { try { const params = new URLSearchParams({ firstName: profile.firstName, lastName: profile.lastName, teamId: profile.teamId, }); const response = await fetch(`${this.baseUrl}/api/evaluations?${params}`); if (!response.ok) { throw new Error("Erreur lors du chargement de l'évaluation"); } const data = await response.json(); return data.evaluation; } catch (error) { console.error("Erreur lors du chargement de l'évaluation:", error); return null; } } /** * Sauvegarde une évaluation utilisateur via l'API */ async saveUserEvaluation(evaluation: UserEvaluation): Promise { try { const response = await fetch(`${this.baseUrl}/api/evaluations`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ evaluation }), }); if (!response.ok) { throw new Error("Erreur lors de la sauvegarde de l'évaluation"); } } catch (error) { console.error("Erreur lors de la sauvegarde de l'évaluation:", error); throw error; } } /** * Met à jour le niveau d'une skill */ async updateSkillLevel( profile: UserProfile, category: string, skillId: string, level: SkillLevel ): Promise { await this.updateSkill(profile, category, skillId, { action: "updateLevel", level, }); } /** * Met à jour le statut de mentorat d'une skill */ async updateSkillMentorStatus( profile: UserProfile, category: string, skillId: string, canMentor: boolean ): Promise { await this.updateSkill(profile, category, skillId, { action: "updateMentorStatus", canMentor, }); } /** * Met à jour le statut d'apprentissage d'une skill */ async updateSkillLearningStatus( profile: UserProfile, category: string, skillId: string, wantsToLearn: boolean ): Promise { await this.updateSkill(profile, category, skillId, { action: "updateLearningStatus", wantsToLearn, }); } /** * Ajoute une skill à l'évaluation */ async addSkillToEvaluation( profile: UserProfile, category: string, skillId: string ): Promise { await this.updateSkill(profile, category, skillId, { action: "addSkill", }); } /** * Supprime une skill de l'évaluation */ async removeSkillFromEvaluation( profile: UserProfile, category: string, skillId: string ): Promise { await this.updateSkill(profile, category, skillId, { action: "removeSkill", }); } /** * Méthode utilitaire pour mettre à jour une skill */ private async updateSkill( profile: UserProfile, category: string, skillId: string, options: { action: | "updateLevel" | "updateMentorStatus" | "updateLearningStatus" | "addSkill" | "removeSkill"; level?: SkillLevel; canMentor?: boolean; wantsToLearn?: boolean; } ): Promise { try { const response = await fetch(`${this.baseUrl}/api/evaluations/skills`, { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ profile, category, skillId, ...options, }), }); if (!response.ok) { throw new Error("Erreur lors de la mise à jour de la skill"); } } catch (error) { console.error("Erreur lors de la mise à jour de la skill:", error); throw error; } } } // Instance singleton export const apiClient = new ApiClient();