categoriesAction.js 3.07 KB
import axios from "axios";
import qs from "qs";
import {
  GET_CATEGORIES_FAIL,
  GET_CATEGORIES_REQUEST,
  GET_CATEGORIES_SUCCESS,
  GET_SUB_CATEGORIES_FAIL,
  GET_SUB_CATEGORIES_REQUEST,
  GET_SUB_CATEGORIES_SUCCESS
} from "../constants/categoryConstants";

export const getAllCategories = () => async dispatch => {
  // console.log("here >");
  try {
  // console.log("here >>");

    dispatch({
      type: GET_CATEGORIES_REQUEST
    });

    const config = {
      headers: {
        "Content-Type": "application/json"
      }
    };
    const query = {
      populate: ["image", "subCategories"]
    };

    const queryString = qs.stringify(query, {
      encodeValuesOnly: true
    });

    const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/categories?${queryString}`, config);
    // console.log("response > ", response);
    dispatch({
      type: GET_CATEGORIES_SUCCESS,
      payload: response.data
    });

    return response.data;
  } catch (error) {
    dispatch({
      type: GET_CATEGORIES_FAIL,
      payload: error.response.data
    });
  }
};

export const getAllSubCategories = categoryName => async dispatch => {
  try {
    dispatch({
      type: GET_SUB_CATEGORIES_REQUEST
    });
    const config = {
      headers: {
        "Content-Type": "application/json"
      }
    };

    let query = {
      filters: {
        category: {
          name: {}
        }
      },
      populate: ["category"]
    };

    if (categoryName) {
      // console.log("here 1", query.filters.category);
      query.filters.category.name = { $eq: categoryName };
    }
    // console.log(">>>", query);
    const queryString = qs.stringify(query, {
      encodeValuesOnly: true
    });

    const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/sub-categories/?${queryString}`, config);

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

    return response.data;
  } catch (error) {
    dispatch({
      type: GET_SUB_CATEGORIES_FAIL,
      payload: error.response.data
    });
  }
};



export const getSubCategoriesByCategoryId = categoryId => async dispatch => {
  try {
    dispatch({
      type: GET_SUB_CATEGORIES_REQUEST,
      loading: true
    });
    const config = {
      headers: {
        "Content-Type": "application/json"
      }
    };

    let query = {
      filters: {
        category: {
          id: {}
        }
      },
      populate: ["category"]
    };

    if (categoryId) {
      // console.log("here 1", query.filters.category);
      query.filters.category.id = { $eq: categoryId };
    }
    // console.log(">>>", query);
    const queryString = qs.stringify(query, {
      encodeValuesOnly: true
    });

    const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/sub-categories/?${queryString}`, config);

    dispatch({
      type: GET_SUB_CATEGORIES_SUCCESS,
      payload: response.data,
      loading: false
    });

    return response.data;
  } catch (error) {
    dispatch({
      type: GET_SUB_CATEGORIES_FAIL,
      payload: error.response.data
    });
  }
};