-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPinListPage.js
81 lines (77 loc) · 2.26 KB
/
PinListPage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import React from "react";
import { Link } from "react-router-dom";
import randomColor from "randomcolor";
import { Route } from "react-router-dom";
import "./PinListPage.css";
function stringToColor(str) {
return randomColor({
seed: str
});
}
class PinListPage extends React.Component {
componentDidUpdate({ pins }) {
if (
this.props.pins.length !== pins &&
this.lastElement &&
pins.length !== 0 // Only scroll on updates. Don't scroll in the first request
) {
this.lastElement.scrollIntoView({ behavior: "smooth" });
}
}
render() {
if (!this.props.match) {
return null;
}
return (
<div>
{this.props.pins.length === 0 && (
<div
style={{
display: "flex",
alignItems: "center",
justifyContent: "center",
flexDirection: "column",
padding: 30
}}
>
<div>There are no pins yet.</div>
<Link to="/upload-pin">Create the first one</Link>
</div>
)}
<ul className="pins">
{this.props.pins.map((pin, index) => (
<li
className="pin"
key={index}
ref={element => {
if (element && index === this.props.pins.length - 1) {
this.lastElement = element;
}
}}
>
<a href={pin.link} target="_blank">
<img
src={pin.image}
alt={pin.title}
onError={event => {
event.target.src = `https://via.placeholder.com/200x200/${stringToColor(
pin.link
).replace("#", "")}?text= +`;
}}
/>
<h4 className="title">{pin.title}</h4>
</a>
</li>
))}
</ul>
</div>
);
}
}
export default ({ pins = [] }) => (
// Render PinListPage as children instead of using react router's render or component props
// The reason is that those other options mount/unmount the component, which would not trigger componentDidUpdate
<Route exact path="/">
{({ match }) => <PinListPage pins={pins} match={match} />}
</Route>
);