feat: implement core e-learning pages, course composable, and i18n localization.

This commit is contained in:
supalerk-ar66 2026-02-09 11:40:41 +07:00
parent f736eb7f38
commit e94410d0e7
9 changed files with 235 additions and 138 deletions

View file

@ -15,17 +15,12 @@ useHead({
})
const { currentUser } = useAuth()
const { fetchCourses } = useCourse() // Import useCourse
const { fetchCourses, getLocalizedText } = useCourse() // Import useCourse
const { fetchCategories } = useCategory() // Import useCategory
const { t } = useI18n()
// 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 || ''
}
// Recommended Courses State
// ( 3 )
@ -33,28 +28,24 @@ const recommendedCourses = ref<any[]>([])
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
// 3
const res = await fetchCourses()
// 2. Fetch 3 Random Courses from Server
// Server ( API parameter random limit)
const res = await fetchCourses({ random: true, limit: 3, forceRefresh: true })
if (res.success && res.data?.length) {
// Shuffle array ()
const shuffled = [...res.data].sort(() => 0.5 - Math.random())
// Pick first 3 ( 3 )
recommendedCourses.value = shuffled.slice(0, 3).map((c: any) => ({
recommendedCourses.value = res.data.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} ${t('course.lessonsUnit')}` : '', // Use lesson count or empty
category: getLocalizedText(catMap.get(c.category_id)) || 'General',
duration: c.lessons ? `${c.lessons} ${t('course.lessonsUnit')}` : '',
image: c.thumbnail_url || '',
badge: '', // No mock badge
badge: '',
badgeType: ''
}))
}