-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglePostContainer.js
141 lines (126 loc) · 4.55 KB
/
SinglePostContainer.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import _throttle from 'lodash.throttle';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import LazyPhotos from '../decorators/LazyPhotos';
import { shouldLoadPosts, getFbShareUrl, windowOpen } from '../services/helpers';
import { fetchRecommendedPosts, shareClick, fetchSharesNumber } from '../store/posts/actions';
import { getVisitedPosts, addVisitedPost, clickTracking } from '../store/user/actions';
import { getCurrentPost, getRecommendedPosts } from '../store/posts/reducer';
import { saveVisitedPosts } from '../store/_middleware/localStorage';
import Main from '../components/Main';
import Post from '../components/Post';
import RecommendedPosts from '../components/RecommendedPosts';
import NotifyBox from '../components/NotifyBox';
import Footer from '../components/Footer';
@LazyPhotos
class SinglePostContainer extends Component {
static propTypes = {
post: PropTypes.shape({
title: PropTypes.string.isRequired,
slug: PropTypes.string.isRequired,
description: PropTypes.string.isRequired,
photos: PropTypes.arrayOf(PropTypes.object).isRequired,
cover: PropTypes.shape({
url: PropTypes.string.isRequired,
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
}),
sharesLoaded: PropTypes.bool.isRequired,
}).isRequired,
visitedPosts: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
fetchRecommendedPosts: PropTypes.func.isRequired,
getVisitedPosts: PropTypes.func.isRequired,
addVisitedPost: PropTypes.func.isRequired,
recommendedPosts: PropTypes.arrayOf(PropTypes.object).isRequired,
isFetchingRecommendedPosts: PropTypes.bool.isRequired,
isDownloadedRecommendedPosts: PropTypes.bool.isRequired,
fetchSharesNumber: PropTypes.func.isRequired,
shareClick: PropTypes.func.isRequired,
notifyBtn: PropTypes.bool.isRequired,
clickTracking: PropTypes.func.isRequired,
}
componentDidMount() {
if (this.props.post) {
setTimeout(() => {
this.props.getVisitedPosts(this.props.post.slug);
window.addEventListener('scroll', this.handleScroll);
this.slug = this.props.post.slug;
}, 5);
}
}
componentWillReceiveProps(nextProps) {
if (this.props.visitedPosts.length !== nextProps.visitedPosts.length) {
saveVisitedPosts(nextProps.visitedPosts);
}
}
componentDidUpdate() {
if (this.props.post) {
const { post: { slug } } = this.props;
if (this.slug !== slug && !this.props.visitedPosts.includes(slug)) {
this.props.addVisitedPost(this.props.post.slug);
}
if (!this.props.post.sharesLoaded) {
setTimeout(() => {
this.props.fetchSharesNumber(this.props.post.slug);
}, 5);
}
this.slug = slug;
}
}
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll);
}
shoudlLoadRecommendedPosts() {
return !this.props.isFetchingRecommendedPosts &&
!this.props.isDownloadedRecommendedPosts &&
!this.props.recommendedPosts.length;
}
handleScroll = _throttle(() => {
if (shouldLoadPosts() && this.shoudlLoadRecommendedPosts()) {
this.props.fetchRecommendedPosts(this.props.visitedPosts);
}
}, 300)
handleClick = (slug, shares, liked, e) => {
e.preventDefault();
if (!liked) windowOpen(getFbShareUrl(slug));
this.props.shareClick(slug, shares, liked);
}
render() {
return (
<Main>
{this.props.post && <Post {...this.props.post} handleClick={this.handleClick} />}
{this.props.recommendedPosts &&
<RecommendedPosts
isDownloaded={this.props.isDownloadedRecommendedPosts}
isFetching={this.props.isFetchingRecommendedPosts}
posts={this.props.recommendedPosts}
/>
}
<NotifyBox notifyBtn={this.props.notifyBtn} clickTracking={this.props.clickTracking} />
<Footer clickTracking={this.props.clickTracking} />
</Main>
);
}
}
function mapStateToProps(state) {
return {
post: getCurrentPost(state),
visitedPosts: state.user.visitedPosts,
isDownloadedRecommendedPosts: state.user.isDownloadedRecommendedPosts,
isFetchingRecommendedPosts: state.user.isFetchingRecommendedPosts,
recommendedPosts: getRecommendedPosts(state),
notifyBtn: state.header.notifyBtn,
};
}
export default connect(
mapStateToProps,
{
getVisitedPosts,
addVisitedPost,
fetchRecommendedPosts,
fetchSharesNumber,
shareClick,
clickTracking,
},
)(SinglePostContainer);