From bf333c66f45aecf754c0c7f4cbaed95c5897421a Mon Sep 17 00:00:00 2001 From: DomWane Date: Thu, 3 Oct 2024 16:41:44 +0200 Subject: [PATCH] fix: final percentage --- composables/useData.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/composables/useData.ts b/composables/useData.ts index 3d5a95d..b444959 100644 --- a/composables/useData.ts +++ b/composables/useData.ts @@ -61,10 +61,16 @@ export const useData = defineStore('data', () => { ranks: Rank[] }>('/api/data') data.projects.forEach(project => project.ratings = generateProjectRating(project)) - projects.value = data.projects.map(project => ({ - ...project, - percentage: Math.round((project.ratings?.reduce((a, b) => a + b.points, 0) || 0) / 1.5), - })).filter(p => p.name) + projects.value = data.projects.map((project) => { + const totalPoints = project.ratings?.reduce((a, b) => a + b.percentagePoints, 0) || 0 + const numberOfRatings = project.ratings?.length || 1 // Avoid division by zero + const averagePercentage = totalPoints / numberOfRatings + return { + ...project, + percentage: Math.min(Math.max(Math.round(averagePercentage), 0), 100), // Ensure within 0-100% + } + }).filter(p => p.name) + console.log(projects.value.filter(p => p.name === 'ETHBerlin')) const projectCategories = projects.value.map(p => p.categories).flat() categories.value = data.categories.filter(c => projectCategories.includes(c.id)) usecases.value = data.usecases