Advertisement
ahmadandika

Untitled

Mar 19th, 2020
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // containers/home/HomeItem.jsx
  2.  
  3. /* eslint-disable jsx-a11y/no-static-element-interactions */
  4. /* eslint-disable jsx-a11y/click-events-have-key-events */
  5. import React, { Fragment, Component } from 'react';
  6. import Link from 'next/link';
  7. import { Collapse } from 'reactstrap';
  8. import Router from 'next/router';
  9.  
  10. class HomeItem extends Component {
  11.   constructor(props) {
  12.     super(props);
  13.     const { data } = this.props;
  14.     const collection = data.map(i => {
  15.       if (i.group_child !== null) {
  16.         return i.group_child.map(x => x.id);
  17.       }
  18.     });
  19.  
  20.     const stripped = collection.filter(Boolean).flat();
  21.  
  22.     this.state = {
  23.       indexCollapse: stripped
  24.     };
  25.   }
  26.  
  27.   toggle = groupId => {
  28.     const { indexCollapse } = this.state;
  29.  
  30.     const indArr = indexCollapse;
  31.  
  32.     if (!indexCollapse.includes(groupId)) {
  33.       indArr.push(groupId);
  34.       this.setState({
  35.         indexCollapse: indArr
  36.       });
  37.     } else {
  38.       const index = indArr.indexOf(groupId);
  39.       if (index !== -1) indArr.splice(index, 1);
  40.       this.setState({
  41.         indexCollapse: indArr
  42.       });
  43.     }
  44.   };
  45.  
  46.   handleLink = category => {
  47.     if (process.browser) {
  48.       const path = [
  49.         {
  50.           id: category.id,
  51.           name: category.name
  52.         }
  53.       ];
  54.       localStorage.removeItem('path');
  55.       localStorage.setItem('path', JSON.stringify(path));
  56.  
  57.       Router.push(`/subcategory/id/${category.id}`);
  58.     }
  59.     console.log('cat', category);
  60.   };
  61.  
  62.   render() {
  63.     const { dataGroup } = this.props;
  64.     const { indexCollapse } = this.state;
  65.     // console.log('indexCollapse', indexCollapse);
  66.     // // console.log('size', size);
  67.  
  68.     return (
  69.       <div id="page-content-wrapper" className="pl-3 pr-3 w-100">
  70.         <nav className="navbar navbar-light border-bottom">
  71.           <nav aria-label="breadcrumb" className="d-flex">
  72.             <h5 className="align-self-center">{dataGroup.name}</h5>
  73.           </nav>
  74.         </nav>
  75.  
  76.         <div className="container-fluid">
  77.           <div className="row">
  78.             <div className="col-md-12">
  79.               <span>{dataGroup.name}</span>
  80.             </div>
  81.           </div>
  82.           <div className="d-flex flex-wrap">
  83.             {!dataGroup.group_child ? (
  84.               <span className="ml-2 w-100 text-dark font-weight-light">
  85.                 No sub group found
  86.               </span>
  87.             ) : (
  88.               dataGroup.group_child.map(group => (
  89.                 <Fragment key={group.id}>
  90.                   <span className="w-100 text-dark">
  91.                     <i
  92.                       className="fa fa-caret-right pr-2 text-warning"
  93.                       style={{ cursor: 'pointer' }}
  94.                       onClick={this.toggle.bind(this, group.id)}
  95.                     />
  96.                     {group.name}
  97.                   </span>
  98.                   <Collapse isOpen={indexCollapse.includes(group.id)}>
  99.                     <div
  100.                       className="d-flex flex-wrap"
  101.                       style={{ cursor: 'pointer' }}
  102.                     >
  103.                       {!group.category_child ? (
  104.                         <span className="ml-2 w-100 text-dark font-weight-light">
  105.                           No category found...
  106.                         </span>
  107.                       ) : (
  108.                         group.category_child.map(category => (
  109.                           <div
  110.                             // href="/subcategory/id/[id]"
  111.                             onClick={() => {
  112.                               this.handleLink(category);
  113.                             }}
  114.                             // as={`/subcategory/id/${category.id}&path=[{"id":"${category.id}","name":"${category.name}"}]`}
  115.                             key={category.id}
  116.                           >
  117.                             <div className="product-listing mt-5 mr-2">
  118.                               <div className="thumb-wrapper w-100">
  119.                                 <img src={category.picture.path} alt="" />
  120.                               </div>
  121.                               <div className="desc">{category.name}</div>
  122.                             </div>
  123.                           </div>
  124.                         ))
  125.                       )}
  126.                     </div>
  127.                   </Collapse>
  128.                 </Fragment>
  129.               ))
  130.             )}
  131.           </div>
  132.         </div>
  133.       </div>
  134.     );
  135.   }
  136. }
  137.  
  138. export default HomeItem;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement