goodCard.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { connect } from "react-redux";
  2. import {gql, urlUpload, actionPromise, actionGoodCard, getGoods, actionCartAdd} from "../reducers";
  3. import {createStore, combineReducers, applyMiddleware, bindActionCreators} from 'redux';
  4. import { useEffect, useState } from 'react';
  5. import {BrowserRouter as Router, Route, Link, Switch, Redirect, useHistory} from 'react-router-dom';
  6. const mapStateToProps = state => ({
  7. state: state,
  8. basket: state.basket,
  9. goodCard: getGoods(state, "goodCard", "GoodFindOne")
  10. });
  11. const mapDispatchToProps = dispatch => bindActionCreators({
  12. getData: actionGoodCard,
  13. onAdd: actionCartAdd
  14. }, dispatch);
  15. const GoodCard = ({id, goodCard = null, state, onAdd, getData, className = "goodCard"}) => {
  16. const history = useHistory();
  17. useEffect(() => getData(id), [id]);
  18. //console.log(state)
  19. const [width, setWidth] = useState(window.innerWidth);
  20. const [startWidth, setStartwidth] = useState(true);
  21. const [finallyWidth, setFinallyWidth] = useState(true);
  22. useEffect(() => startWidth !== finallyWidth && window.location.reload(), [startWidth, finallyWidth])
  23. window.onresize = () => {
  24. width > 900 ? setStartwidth(true) : setStartwidth(false);
  25. setWidth(window.innerWidth);
  26. width > 900 ? setFinallyWidth(true) : setFinallyWidth(false);
  27. }
  28. console.log(goodCard)
  29. return(
  30. <>
  31. { goodCard &&
  32. <div className = {className}>
  33. <h2>{goodCard.name}</h2>
  34. <img src = {goodCard.images ? `${urlUpload}/${goodCard.images[0].url}`: `https://images.ua.prom.st/2259265311_korobka-syurpriz-dlya.jpg`}/>
  35. <span>{`${goodCard.price}грн`}</span>
  36. <p>{goodCard.description}</p>
  37. <button onClick = {() => onAdd(goodCard.name, goodCard.price, goodCard._id, goodCard.description, goodCard.images)}>В кошик</button>
  38. </div>
  39. }
  40. </>
  41. )
  42. }
  43. const CGoodCard = connect(mapStateToProps, mapDispatchToProps)(GoodCard)
  44. export default CGoodCard;