redux/actions /course.js
Navnit Rai
Posted on September 22, 2024
import { server } from '../store'; // Importing server endpoint for API requests
import axios from 'axios'; // Importing axios for making HTTP requests
// Action to get all courses with optional filtering by category and keyword
export const getAllCourses =
(category = '', keyword = '') =>
async dispatch => {
try {
dispatch({ type: 'allCoursesRequest' }); // Dispatching request action
// Fetching courses from the server with optional query parameters
const { data } = await axios.get(
`${server}/courses?keyword=${keyword}&category=${category}`
);
// Dispatching success action with the retrieved courses
dispatch({ type: 'allCoursesSuccess', payload: data.courses });
} catch (error) {
// Dispatching failure action with the error message
dispatch({
type: 'allCoursesFail',
payload: error.response.data.message,
});
}
};
// Action to get lectures for a specific course by ID
export const getCourseLectures = id => async dispatch => {
try {
dispatch({ type: 'getCourseRequest' }); // Dispatching request action
// Fetching course lectures from the server using the course ID
const { data } = await axios.get(`${server}/course/${id}`, {
withCredentials: true, // Include credentials for CORS requests
});
// Dispatching success action with the retrieved lectures
dispatch({ type: 'getCourseSuccess', payload: data.lectures });
} catch (error) {
// Dispatching failure action with the error message
dispatch({
type: 'getCourseFail',
payload: error.response.data.message,
});
}
};
💖 💪 🙅 🚩
Navnit Rai
Posted on September 22, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.