redux/actions /course.js

navnit73

Navnit Rai

Posted on September 22, 2024

redux/actions /course.js
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,
    });
  }
};

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
navnit73
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.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024