feat: add new composables for category, course, and authentication management.
This commit is contained in:
parent
fb4f345483
commit
8b403f906a
3 changed files with 59 additions and 12 deletions
|
|
@ -77,22 +77,41 @@ export const useCourse = () => {
|
|||
const API_BASE_URL = config.public.apiBase as string
|
||||
const { token } = useAuth()
|
||||
|
||||
// ใช้ useState เพื่อเก็บรายชื่อคอร์สทั้งหมดใน Memory
|
||||
const coursesState = useState<Course[]>('courses_cache', () => [])
|
||||
const isCoursesLoaded = useState<boolean>('courses_loaded', () => false)
|
||||
|
||||
// ฟังก์ชันดึงรายชื่อคอร์สทั้งหมด (Catalog)
|
||||
// ใช้สำหรับหน้า Discover/Browse
|
||||
// Endpoint: GET /courses
|
||||
const fetchCourses = async () => {
|
||||
const fetchCourses = async (forceRefresh = false) => {
|
||||
// ถ้าโหลดไปแล้ว และไม่ได้บังคับ Refresh ให้ใช้ข้อมูลจาก State
|
||||
if (isCoursesLoaded.value && !forceRefresh && coursesState.value.length > 0) {
|
||||
return {
|
||||
success: true,
|
||||
data: coursesState.value,
|
||||
total: coursesState.value.length
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await $fetch<CourseResponse>(`${API_BASE_URL}/courses`, {
|
||||
method: 'GET',
|
||||
// ส่ง Token ไปด้วยถ้ามี (เผื่อ Logic ในอนาคตที่ต้องเช็คสิทธิ์)
|
||||
// ส่ง Token ไปด้วยถ้ามี
|
||||
headers: token.value ? {
|
||||
Authorization: `Bearer ${token.value}`
|
||||
} : {}
|
||||
})
|
||||
|
||||
const courses = data.data || []
|
||||
|
||||
// เก็บลง State
|
||||
coursesState.value = courses
|
||||
isCoursesLoaded.value = true
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: data.data || [],
|
||||
data: courses,
|
||||
total: data.total || 0
|
||||
}
|
||||
} catch (err: any) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue