{"size": 904, "ext": "js", "max_stars_repo_path": "frontend/src/redux/actions/productListAction.js", "max_stars_repo_name": "tsbayar/winshop", "max_stars_repo_stars_event_min_datetime": "2021-05-31T14:50:15.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-09T03:33:52.000Z", "max_issues_repo_path": "frontend/src/redux/actions/productListAction.js", "max_issues_repo_name": "tsbayar/winshop", "max_issues_repo_issues_event_min_datetime": "2021-06-16T07:15:43.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-16T07:15:43.000Z", "max_forks_repo_path": "frontend/src/redux/actions/productListAction.js", "max_forks_repo_name": "tsbayar/winshop", "max_forks_repo_forks_event_min_datetime": "2021-05-30T19:12:31.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-07T19:18:27.000Z"}
{"max_stars_count": 15, "max_issues_count": 1, "max_forks_count": 7, "avg_line_length": 19.652173913, "max_line_length": 75, "alphanum_fraction": 0.6227876106}
import axios from "axios";

import {
  PRODUCT_LIST_REQUEST,
  PRODUCT_LIST_SUCCESS,
  PRODUCT_LIST_FAILED,
} from "../actionTypes/productListConstants";

///*   ACTIONS    ///

const reqProducts = () => ({
  type: PRODUCT_LIST_REQUEST,
});

const addProducts = (products) => ({
  type: PRODUCT_LIST_SUCCESS,
  payload: products,
});

const productsFailed = (errMess) => ({
  type: PRODUCT_LIST_FAILED,
  payload: errMess,
});

///*     ACTION CREATOR     ///

export const listProducts = (keyword = "", API) => async (dispatch) => {
  try {
    dispatch(reqProducts());

    const { data } = await axios.get(`${API}/products?keyword=${keyword}`);

    dispatch(addProducts(data));

    console.log(data);
  } catch (error) {
    dispatch(
      productsFailed(
        error.response && error.response.data.message
          ? error.response.data.message
          : error.message
      )
    );
  }
};
