DEV Community

Navnit Rai
Navnit Rai

Posted on

redux/actions /other.js

import { server } from '../store'; // Importing server endpoint for API requests
import axios from 'axios'; // Importing axios for making HTTP requests

// Action to handle contact form submissions
export const contactUs = (name, email, message) => async dispatch => {
  try {
    const config = {
      headers: {
        'Content-type': 'application/json', // Setting content type for JSON
      },
      withCredentials: true, // Include credentials for CORS requests
    };

    dispatch({ type: 'contactRequest' }); // Dispatching request action

    // Sending the contact message to the server
    const { data } = await axios.post(
      `${server}/contact`,
      { name, email, message }, // Payload containing the user's contact info
      config
    );

    dispatch({ type: 'contactSuccess', payload: data.message }); // Dispatching success action
  } catch (error) {
    dispatch({
      type: 'contactFail', // Dispatching failure action
      payload: error.response.data.message, // Sending error message
    });
  }
};

// Action to handle course request submissions
export const courseRequest = (name, email, course) => async dispatch => {
  try {
    const config = {
      headers: {
        'Content-type': 'application/json', // Setting content type for JSON
      },
      withCredentials: true, // Include credentials for CORS requests
    };

    dispatch({ type: 'courseRequestRequest' }); // Dispatching request action

    // Sending the course request to the server
    const { data } = await axios.post(
      `${server}/courserequest`,
      { name, email, course }, // Payload containing the user's request info
      config
    );

    dispatch({ type: 'courseRequestSuccess', payload: data.message }); // Dispatching success action
  } catch (error) {
    dispatch({
      type: 'courseRequestFail', // Dispatching failure action
      payload: error.response.data.message, // Sending error message
    });
  }
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)