catalog.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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} 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. <>
  18. <ul className="catalog">
  19. {categories.map(category =>
  20. category.subCategories == null ?
  21. <Links key={category._id} url={`/catalog/` + category._id} text={category.name}> </Links> :
  22. <CatalogSubLink name = {category.name} arr = {category.subCategories}/>
  23. // <li onClick = {() =>changeValue(!show)}>{category.name} { show && <ul className="catalog">{category.subCategories.map(subCategory =>
  24. // <Links key={subCategory._id} url={`/catalog/` + subCategory._id} text={subCategory.name}> </Links>)}</ul>}</li>
  25. )}
  26. </ul>
  27. </>
  28. )
  29. }
  30. const getCategories = state => {
  31. // console.log("state", state)
  32. if (state.promiseRed.categories && state.promiseRed.categories.payload) {
  33. return state.promiseRed.categories.payload.data.CategoryFind
  34. }
  35. return [];
  36. };
  37. const mapStateToProps = state => ({
  38. state: state,
  39. categories: getCategories(state)
  40. });
  41. const mapDispatchToProps = dispatch => bindActionCreators({
  42. getData: actionCatalogCard
  43. }, dispatch);
  44. export default connect(mapStateToProps, mapDispatchToProps)(Catalog);