The code then makes a GET request to the /api/course
API endpoint using the fetchWithCache
function. This function appears to cache requests to reduce unnecessary API calls. It sends a query with the course's prefix
and number
as URL parameters (after encoding them for safety), for example, /api/course?prefix=CS&number=101
.
The fetchWithCache
function takes in three main arguments:
The URL with query parameters.
cacheIndexNebula
: Likely an identifier for the cache.
expireTime
: Defines the time the cache expires.
A configuration object specifying the request method (GET
) and the accepted response type (application/json
).
The first .then()
block checks the response to ensure the API call was successful. If the message
in the response is not 'success'
, an error is thrown.
If the response is successful, the next .then()
block processes the actual data (CourseData[]
), which is an array of course data.
The array of course data is sorted by the catalog_year
property in descending order. This ensures that the most recent course information is placed at the beginning of the array (index 0
).
It then sets the courseData
state to { state: 'done', data: response[0] }
, where response[0]
is the most recent course data.
The course API is triggered everytime the the cource is changed, (for example when you search a new cource)