detailsAction.js 1.02 KB
import { FETCH_DETAILS_FAIL, FETCH_DETAILS_SUCCESS, CLEAR_ERRORS } from "../constants/detailsConstants";
import axios from "axios";
import qs from "qs";

export const getDetails = detailsId => 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/projects/${detailsId}?${query}`, config);

    dispatch({
      type: FETCH_DETAILS_SUCCESS,
      payload: response.data
    });
  } catch (error) {
    console.log("Error while fetching Project details: ");
    console.log(error.response.data);

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

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