homeDeliveryStatsActions.js 1.04 KB
import axios from "axios";
import { FETCH_HOME_DELIVERY_STATS_FAIL, FETCH_HOME_DELIVERY_STATS_SUCCESS, CLEAR_ERRORS } from "../constants/homeDeliveryStatsConstants";
import qs from "qs";

export const getHomeDeliveryStats = () => async dispatch => {
  try {
    const config = {
      headers: {
        "Content-Type": "application/json"
      }
    };
    const query = qs.stringify(
      {
        populate: ["backgroundImage"]
      },
      {
        encodeValuesOnly: true // prettify URL
      }
    );

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

    dispatch({
      type: FETCH_HOME_DELIVERY_STATS_SUCCESS,
      payload: response.data
    });
  } catch (error) {
    console.log("Error while fetching delivery stats");
    console.log(error.response.data);

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

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