{"size": 1949, "ext": "js", "max_stars_repo_path": "client/src/core/Card.js", "max_stars_repo_name": "yogeshdecodes/ecom-monorepo", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "client/src/core/Card.js", "max_issues_repo_name": "yogeshdecodes/ecom-monorepo", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "client/src/core/Card.js", "max_forks_repo_name": "yogeshdecodes/ecom-monorepo", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.3378378378, "max_line_length": 105, "alphanum_fraction": 0.5854284248}
import React, { useState, useEffect } from 'react';
import ImageHelper from './helper/ImageHelper';
import { Redirect } from 'react-router-dom';
import { addItemToCart, removeItemFromCart } from './helper/cartHelper';

const Card = ({
  product,
  addtoCart = true,
  removeFromCart = false,
  setReload = (f) => f,
  //   function(f){return f}
  reload = undefined,
}) => {
  const [redirect, setRedirect] = useState(false);
  const [count, setCount] = useState(product.count);

  const cartTitle = product ? product.name : 'A photo from pexels';
  const cartDescrption = product ? product.description : 'Default description';
  const cartPrice = product ? product.price : 'DEFAULT';

  const addToCart = () => {
    addItemToCart(product, () => setRedirect(true));
  };

  const getARedirect = (redirect) => {
    if (redirect) {
      return <Redirect to="/cart" />;
    }
  };

  const showAddToCart = (addtoCart) => {
    return (
      addtoCart && (
        <button
          onClick={addToCart}
          className="btn btn-warning"
        >
          Add to Cart
        </button>
      )
    );
  };

  const showRemoveFromCart = (removeFromCart) => {
    return (
      removeFromCart && (
        <button
          onClick={() => {
            removeItemFromCart(product._id);
            setReload(!reload);
          }}
          className="btn btn-block btn-outline-danger mt-2 mb-2"
        >
          Remove from cart
        </button>
      )
    );
  };
  return (
    <div className="card rounded ">
      {getARedirect(redirect)}
      <div class="card-image">
          <span class="card-notify-badge">New</span>
          <ImageHelper product={product} />
      </div>
      <div class=" m-auto"> <h5>{cartTitle}</h5> </div>
      <p class="font-weight-bold">₹ {cartPrice}</p>
      <div class="text-center mb-4"> {showAddToCart(addtoCart)}{showRemoveFromCart(removeFromCart)}</div>
    </div>
  );
};

export default Card;
