homeBannersAction.js 1.05 KB
import axios from "axios";
import { FETCH_HOME_BANNERS_SUCCESS, FETCH_HOME_BANNERS_FAIL, CLEAR_ERRORS } from "../constants/homeBannerConstants";
import qs from "qs";

// Get room details
export const getHomeBanners = () => async dispatch => {
  try {
    const config = {
      headers: {
        "Content-Type": "application/json"
      }
    };
    const query = qs.stringify(
      {
        populate: ["image"],
        sort: ["serialNumber:desc"]
      },
      {
        encodeValuesOnly: true // prettify URL
      }
    );

    const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/home-banners?${query}`, config);

    dispatch({
      type: FETCH_HOME_BANNERS_SUCCESS,
      payload: response.data
    });
  } catch (error) {
    console.log("Error while fetching home banners: ");
    console.log(error.response.data);

    dispatch({
      type: FETCH_HOME_BANNERS_FAIL,
      payload: error.response.data
    });
  }
};

// Clear errors
export const clearErrors = () => async dispatch => {
  dispatch({
    type: CLEAR_ERRORS
  });
};