redux/actions /admin.js

Navnit Rai - Sep 22 - - Dev Community
import { server } from '../store'; // Server endpoint for API requests
import axios from 'axios'; // Axios for making HTTP requests

// Action to create a new course
export const createCourse = formData => async dispatch => {
  try {
    const config = {
      headers: {
        'Content-type': 'multipart/form-data', // Specify content type for form data
      },
      withCredentials: true, // Include credentials for CORS requests
    };
    dispatch({ type: 'createCourseRequest' }); // Dispatch request action

    const { data } = await axios.post(
      `${server}/createcourse`, // API endpoint
      formData, // Form data to be sent
      config
    );

    dispatch({ type: 'createCourseSuccess', payload: data.message }); // Dispatch success action
  } catch (error) {
    dispatch({
      type: 'createCourseFail',
      payload: error.response.data.message, // Error message from the server
    });
  }
};

// Action to delete a course
export const deleteCourse = id => async dispatch => {
  try {
    const config = {
      withCredentials: true, // Include credentials for CORS requests
    };
    dispatch({ type: 'deleteCourseRequest' }); // Dispatch request action

    const { data } = await axios.delete(`${server}/course/${id}`, config); // API call to delete course

    dispatch({ type: 'deleteCourseSuccess', payload: data.message }); // Dispatch success action
  } catch (error) {
    dispatch({
      type: 'deleteCourseFail',
      payload: error.response.data.message, // Error message from the server
    });
  }
};

// Action to add a lecture to a course
export const addLecture = (id, formdata) => async dispatch => {
  try {
    const config = {
      headers: {
        'Content-type': 'multipart/form-data', // Specify content type for form data
      },
      withCredentials: true,
    };
    dispatch({ type: 'addLectureRequest' }); // Dispatch request action

    const { data } = await axios.post(
      `${server}/course/${id}`, // API endpoint
      formdata, // Form data to be sent
      config
    );

    dispatch({ type: 'addLectureSuccess', payload: data.message }); // Dispatch success action
  } catch (error) {
    dispatch({
      type: 'addLectureFail',
      payload: error.response.data.message, // Error message from the server
    });
  }
};

// Action to delete a lecture from a course
export const deleteLecture = (courseId, lectureId) => async dispatch => {
  try {
    const config = {
      withCredentials: true, // Include credentials for CORS requests
    };
    dispatch({ type: 'deleteLectureRequest' }); // Dispatch request action

    const { data } = await axios.delete(
      `${server}/lecture?courseId=${courseId}&lectureId=${lectureId}`, // API call to delete lecture
      config
    );

    dispatch({ type: 'deleteLectureSuccess', payload: data.message }); // Dispatch success action
  } catch (error) {
    dispatch({
      type: 'deleteLectureFail',
      payload: error.response.data.message, // Error message from the server
    });
  }
};

// Action to get all users for admin
export const getAllUsers = () => async dispatch => {
  try {
    const config = {
      withCredentials: true, // Include credentials for CORS requests
    };
    dispatch({ type: 'getAllUsersRequest' }); // Dispatch request action

    const { data } = await axios.get(`${server}/admin/users`, config); // API call to get users

    dispatch({ type: 'getAllUsersSuccess', payload: data.users }); // Dispatch success action
  } catch (error) {
    dispatch({
      type: 'getAllUsersFail',
      payload: error.response.data.message, // Error message from the server
    });
  }
};

// Action to update a user's role
export const updateUserRole = id => async dispatch => {
  try {
    const config = {
      withCredentials: true, // Include credentials for CORS requests
    };
    dispatch({ type: 'updateUserRoleRequest' }); // Dispatch request action

    const { data } = await axios.put(`${server}/admin/user/${id}`, {}, config); // API call to update user role

    dispatch({ type: 'updateUserRoleSuccess', payload: data.message }); // Dispatch success action
  } catch (error) {
    dispatch({
      type: 'updateUserRoleFail',
      payload: error.response.data.message, // Error message from the server
    });
  }
};

// Action to delete a user
export const deleteUser = id => async dispatch => {
  try {
    const config = {
      withCredentials: true, // Include credentials for CORS requests
    };
    dispatch({ type: 'deleteUserRequest' }); // Dispatch request action

    const { data } = await axios.delete(`${server}/admin/user/${id}`, config); // API call to delete user

    dispatch({ type: 'deleteUserSuccess', payload: data.message }); // Dispatch success action
  } catch (error) {
    dispatch({
      type: 'deleteUserFail',
      payload: error.response.data.message, // Error message from the server
    });
  }
};

// Action to get dashboard statistics
export const getDashboardStats = () => async dispatch => {
  try {
    const config = {
      withCredentials: true, // Include credentials for CORS requests
    };
    dispatch({ type: 'getAdminStatsRequest' }); // Dispatch request action

    const { data } = await axios.get(`${server}/admin/stats`, config); // API call to get admin stats

    dispatch({ type: 'getAdminStatsSuccess', payload: data }); // Dispatch success action
  } catch (error) {
    dispatch({
      type: 'getAdminStatsFail',
      payload: error.response.data.message, // Error message from the server
    });
  }
};

Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player