-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor Backend API Interface #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 15 commits
853b69a
dfd9a95
f95b6da
58f9f6b
c5715e2
b05eb18
5b355d5
5646dd6
3bddc17
5e22464
774f01a
9fea7c1
4c5142d
86eb459
a493f33
a313364
9b97ac5
50038f0
74d8e0b
24c7199
047c379
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| # the env variable used for vite-processed frontend should begin with `VITE_` | ||
| VITE_TURNSTILE_SITE_KEY=0x4AAAAAABz2ci0ZN9OaO-dg | ||
| VITE_AUTH_OTP_TIMEOUT=120 | ||
| VITE_AUTH_TEMP_TOKEN_TIMEOUT=600 | ||
| VITE_AUTH_TEMP_TOKEN_TIMEOUT=600 | ||
| VITE_API_BASE_URL= |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { ref, reactive } from "vue"; | ||
| import { apiFetch } from "../utils/api"; | ||
|
|
||
| export function useCourses() { | ||
| const courses = ref([]); | ||
|
|
@@ -10,7 +11,6 @@ export function useCourses() { | |
| current_page: 1, | ||
| total_pages: 1, | ||
| total_courses: 0, | ||
| limit: 20, | ||
| }); | ||
|
|
||
| const filters = reactive({ | ||
|
|
@@ -27,11 +27,11 @@ export function useCourses() { | |
|
|
||
| const fetchDepartments = async () => { | ||
| try { | ||
| const response = await fetch("/api/departments/"); | ||
| const response = await apiFetch("/api/departments/"); | ||
| if (!response.ok) throw new Error("Failed to fetch departments"); | ||
| departments.value = await response.json(); | ||
| } catch (e) { | ||
| console.error("useCourses: Error fetching departments:", e); | ||
| error.value = e.message; | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -44,12 +44,15 @@ export function useCourses() { | |
| if (filters.code) params.append("code", filters.code.trim()); | ||
| if (filters.min_quality && isAuth) | ||
| params.append("min_quality", filters.min_quality); | ||
| if (filters.min_difficulty && isAuth) | ||
| params.append("min_difficulty", filters.min_difficulty); | ||
|
A-lexisL marked this conversation as resolved.
|
||
|
|
||
| params.append("sort_by", sorting.sort_by); | ||
| params.append("sort_order", sorting.sort_order); | ||
| params.append("page", pagination.current_page); | ||
|
|
||
| try { | ||
| const response = await fetch(`/api/courses/?${params.toString()}`); | ||
| const response = await apiFetch(`/api/courses/?${params.toString()}`); | ||
| if (!response.ok) { | ||
| const errorData = await response | ||
| .json() | ||
|
|
@@ -59,20 +62,38 @@ export function useCourses() { | |
| ); | ||
| } | ||
| const data = await response.json(); | ||
| courses.value = data.courses; | ||
| pagination.current_page = data.pagination.current_page; | ||
| pagination.total_pages = data.pagination.total_pages; | ||
| pagination.total_courses = data.pagination.total_courses; | ||
| pagination.limit = data.pagination.limit; | ||
| // DRF pagination: { count, next, previous, results } | ||
| courses.value = data.results || []; | ||
| const totalCount = data.count || 0; | ||
| pagination.total_courses = totalCount; | ||
| // TODO: let backend return total pages | ||
| if (pagination.current_page == 1) { | ||
|
A-lexisL marked this conversation as resolved.
|
||
| const page_size = courses.value.length; | ||
| pagination.total_pages = | ||
| page_size > 0 ? Math.ceil(totalCount / page_size) : 1; | ||
| } | ||
|
Comment on lines
+70
to
+74
|
||
| } catch (e) { | ||
| console.error("useCourses: Error fetching courses:", e); | ||
| error.value = e.message; | ||
| courses.value = []; | ||
| } finally { | ||
| loading.value = false; | ||
| } | ||
| }; | ||
|
|
||
| const fetchCourse = async (courseId) => { | ||
| if (!courseId) return null; | ||
| try { | ||
| const response = await apiFetch(`/api/courses/${courseId}/`); | ||
| if (!response.ok) { | ||
| throw new Error(`HTTP error! status: ${response.status}`); | ||
| } | ||
| return await response.json(); | ||
| } catch (e) { | ||
| error.value = e.message; | ||
| throw e; | ||
| } | ||
| }; | ||
|
|
||
| const getQueryObject = (isAuth = false) => { | ||
| const query = {}; | ||
| if (filters.department) query.department = filters.department; | ||
|
|
@@ -125,6 +146,7 @@ export function useCourses() { | |
| pagination, | ||
| filters, | ||
| sorting, | ||
| fetchCourse, | ||
| fetchDepartments, | ||
| fetchCourses, | ||
| getQueryObject, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.