catalog.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { Provider, connect } from 'react-redux';
  2. import { useEffect, useState } from 'react'
  3. import { Header, Footer, MainImg, Links } from "./index"
  4. import thunk from 'redux-thunk';
  5. import { bindActionCreators } from 'redux';
  6. import actionCatalogCard from './../reducers/reducerCat';
  7. const CatalogSubLink = ({name, arr}) => {
  8. const [show, changeValue] = useState(false);
  9. return(
  10. <li><span onClick = {() =>changeValue(!show)}>{name} </span>{<ul className="subCatalog">{ show && arr.map(key =>
  11. <Links className = {"subLink"} key={`${key._id}${Math.random()}`} url={`/catalog/` + key._id} text={key.name}> </Links>)}</ul>}</li>
  12. )
  13. }
  14. const Catalog = ({ state, categories = [], getData = () => console.log("no") }) => {
  15. useEffect(() => categories.length == 0 && getData(), []);
  16. return (
  17. <ul className="catalog">
  18. {categories.map(category =>
  19. category.subCategories == null ?
  20. <Links key={`${category._id}${Math.random()}`} url={`/catalog/` + category._id} text={category.name}> </Links> :
  21. <CatalogSubLink name = {category.name} key = {`${category.subCategories._id}${Math.random()}`}arr = {category.subCategories}/>
  22. )}
  23. </ul>
  24. )
  25. }
  26. const getCategories = state => {
  27. // console.log("state", state)
  28. if (state.promiseRed.categories && state.promiseRed.categories.payload) {
  29. return state.promiseRed.categories.payload.data.CategoryFind
  30. }
  31. return [];
  32. };
  33. const mapStateToProps = state => ({
  34. state: state,
  35. categories: getCategories(state)
  36. });
  37. const mapDispatchToProps = dispatch => bindActionCreators({
  38. getData: actionCatalogCard
  39. }, dispatch);
  40. export default connect(mapStateToProps, mapDispatchToProps)(Catalog);