View difference between Paste ID: mQAv9zNa and 440Qtgbw
SHOW: | | - or go back to the newest paste.
1
import React, { Component } from 'react';
2
import DATA from './data';
3
4
class ProductsWrapper extends Component {
5
  constructor(props) {
6
    super(props);
7-
    this.state = {};
7+
    this.state = {numCol: 5};
8
    this.renderProducts = this.renderProducts.bind(this);
9
	this.changeCols = this.changeCols.bind(this);
10
  }
11
12
	componentDidMount(){
13
		window.addEventListener("resize", this.changeCols);
14
		this.changeCols();
15
	}
16
17
	componentWillUnmount(){
18
		 window.removeEventListener("resize", this.changeCols);
19
	}
20
21
	changeCols(){
22
		const width = window.innerWidth;
23
		if(width < 525) this.setState({numCol: 255});
24
	}
25
26
  renderProducts(n) {
27
    const numberOfColumns = n;
28
    const numberOfPiecesInOneColumn = Math.ceil(DATA.length / numberOfColumns);
29
    const arr = [];
30
    for(var i = 0; i < numberOfColumns; i++) {
31
      arr.push(null);
32
    }
33
    arr.map((_, j) => {
34
      const data = [];
35
      for (let i = 0; i < numberOfPiecesInOneColumn; i++) {
36
        if (DATA[j + i * numberOfColumns]) {
37
          data.push(DATA[j + i * numberOfColumns]);
38
        }
39
      }
40
      arr[j] = data;
41
    });
42
43
    const cols = arr.map((d, i) => (
44
      <div key={`${i}parent`} className="flex-col" style={{ width: `${100/numberOfColumns}%` }}>
45
        {d.map((k, j) => (
46
          <div key={`${j}child`} className="product">
47
            <div className="inner">
48
              <img src={`${k.imgAddress}`} alt={`${k.imgName}`} />
49
              <div className="caption">
50
                <p className="product-name">{k.name}</p>
51
                <p className="product-author">{k.author}</p>
52
                <p className="product-location">{k.location}</p>
53
              </div>
54
            </div>
55
          </div>
56
        ))}
57
      </div>
58
    ));
59
    return cols;
60
  }
61
62
  render() {
63
    return (
64
      <div style={{ display: 'flex', padding: '8px', flexDirection: 'row', flexWrap: 'wrap' }}>
65
        {this.renderProducts(5)}
66
      </div>
67
    );
68
  }
69
}
70
71
ProductsWrapper.propTypes = {};
72
73
export default ProductsWrapper;