diff --git a/Frontend-Learner/pages/dashboard/index.vue b/Frontend-Learner/pages/dashboard/index.vue index 9b3f7fd7..11b51e8b 100644 --- a/Frontend-Learner/pages/dashboard/index.vue +++ b/Frontend-Learner/pages/dashboard/index.vue @@ -15,6 +15,15 @@ useHead({ }) const { currentUser } = useAuth() +const { fetchCourses } = useCourse() // Import useCourse +const { fetchCategories } = useCategory() // Import useCategory + +// Helper to get localized text +const getLocalizedText = (text: string | { th: string; en: string } | undefined) => { + if (!text) return '' + if (typeof text === 'string') return text + return text.th || text.en || '' +} // Mock data: Recent Course Progress const recentCourse = { @@ -24,25 +33,35 @@ const recentCourse = { image: 'https://images.unsplash.com/photo-1586717791821-3f44a563de4c?w=400&auto=format&fit=crop&q=60' } -// Mock data: Recommended Courses -const recommendedCourses = [ - { - title: 'Advanced React Patterns', - category: 'Development', - duration: '4h 30m', - image: 'https://images.unsplash.com/photo-1633356122544-f134324a6cee?w=400&auto=format&fit=crop&q=60', - badge: 'New', - badgeType: 'success' - }, - { - title: 'Data Science Fundamentals', - category: 'Data Science', - duration: '12h 0m', - image: 'https://images.unsplash.com/photo-1551288049-bbda38a0617f?w=400&auto=format&fit=crop&q=60', - badge: 'Popular', - badgeType: 'warning' +// Recommended Courses State +const recommendedCourses = ref([]) + +onMounted(async () => { + // 1. Fetch Categories for mapping + const catRes = await fetchCategories() + const catMap = new Map() + if (catRes.success) { + catRes.data.forEach((c: any) => catMap.set(c.id, c.name)) } -] + + // 2. Fetch All Courses and Randomize + const res = await fetchCourses() + if (res.success && res.data?.length) { + // Shuffle array + const shuffled = [...res.data].sort(() => 0.5 - Math.random()) + + // Pick first 3 + recommendedCourses.value = shuffled.slice(0, 3).map((c: any) => ({ + id: c.id, + title: getLocalizedText(c.title), + category: getLocalizedText(catMap.get(c.category_id)) || 'General', // Map Category ID to Name + duration: c.lessons ? `${c.lessons} บทเรียน` : 'พร้อมเรียน', // Use lesson count or default + image: c.thumbnail_url || 'https://images.unsplash.com/photo-1498050108023-c5249f4df085?w=400&q=80', + badge: 'Recommended', + badgeType: 'warning' + })) + } +})