feat: Implement course detail viewing and enrollment functionality with a new useCourse composable.

This commit is contained in:
supalerk-ar66 2026-02-11 17:06:18 +07:00
parent a65ded02f9
commit 23d9e44cc9
4 changed files with 65 additions and 27 deletions

View file

@ -18,6 +18,7 @@ export interface Course {
approved_at?: string
approved_by?: number
rejection_reason?: string
enrolled?: boolean
rating?: string
@ -253,21 +254,20 @@ export const useCourse = () => {
message: data.message
}
} catch (err: any) {
console.error('Enroll course failed:', err)
// เช็ค Error 409 Conflict หรือ 400 Bad Request (กรณีลงทะเบียนไปแล้ว)
// API ใหม่ส่ง 400 พร้อม error.code = "VALIDATION_ERROR" และ message "Already enrolled..."
const status = err.statusCode || err.status || err.response?.status
const errorData = err.data?.error || err.data
// เช็ค Error 409 Conflict หรือ 400 Bad Request (กรณีลงทะเบียนไปแล้ว)
// สำหรับกรณีนี้ เราจะไม่ log console.error ให้รกหน้าจอเพราะเป็นเรื่องที่ดักจับได้
if (status === 409 || (status === 400 && errorData?.message?.includes('Already enrolled'))) {
return {
success: false,
error: 'ท่านได้ลงทะเบียนไปแล้ว',
code: 409 // Treat as conflict logic internally
code: 409 // treat internally as conflict
}
}
console.error('Enroll course failed:', err)
return {
success: false,
error: errorData?.message || err.message || 'Error enrolling in course',