feat: implement core e-learning pages, course composable, and i18n localization.
This commit is contained in:
parent
f736eb7f38
commit
e94410d0e7
9 changed files with 235 additions and 138 deletions
|
|
@ -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: ''
|
||||
}))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue