forked from VulcanJS/Vulcan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostsPage.jsx
105 lines (82 loc) · 3.14 KB
/
PostsPage.jsx
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
import { Components, registerComponent, withDocument, withCurrentUser, getActions, withMutation } from 'meteor/nova:core';
import Posts from 'meteor/nova:posts';
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
class PostsPage extends Component {
render() {
if (this.props.loading) {
return <div className="posts-page"><Components.Loading/></div>
} else {
const post = this.props.document;
const htmlBody = {__html: post.htmlBody};
return (
<div className="posts-page">
<Components.HeadTags url={Posts.getLink(post)} title={post.title} image={post.thumbnailUrl} description={post.excerpt} />
<Components.PostsItemPostsPage post={post} currentUser={this.props.currentUser} />
{post.htmlBody ? <div className="posts-page-body" dangerouslySetInnerHTML={htmlBody}></div> : null}
<Components.SocialButton type="facebook" post={post} />
<Components.SocialButton type="twitter" post={post} />
<Components.PostsCommentsThread terms={{postId: post._id}} />
</div>
);
}
}
// triggered after the component did mount on the client
async componentDidMount() {
try {
// destructure the relevant props
const {
// from the parent component, used in withDocument, GraphQL HOC
documentId,
// from connect, Redux HOC
setViewed,
postsViewed,
// from withMutation, GraphQL HOC
increasePostViewCount,
} = this.props;
// a post id has been found & it's has not been seen yet on this client session
if (documentId && !postsViewed.includes(documentId)) {
// trigger the asynchronous mutation with postId as an argument
await increasePostViewCount({postId: documentId});
// once the mutation is done, update the redux store
setViewed(documentId);
}
} catch(error) {
console.log(error); // eslint-disable-line
}
}
}
PostsPage.displayName = "PostsPage";
PostsPage.propTypes = {
documentId: PropTypes.string,
document: PropTypes.object,
postsViewed: PropTypes.array,
setViewed: PropTypes.func,
increasePostViewCount: PropTypes.func,
}
const queryOptions = {
collection: Posts,
queryName: 'postsSingleQuery',
fragmentName: 'PostsPage',
};
const mutationOptions = {
name: 'increasePostViewCount',
args: {postId: 'String'},
};
const mapStateToProps = state => ({ postsViewed: state.postsViewed });
const mapDispatchToProps = dispatch => bindActionCreators(getActions().postsViewed, dispatch);
registerComponent(
// component name used by Nova
'PostsPage',
// React component
PostsPage,
// HOC to give access to the current user
withCurrentUser,
// HOC to load the data of the document, based on queryOptions & a documentId props
[withDocument, queryOptions],
// HOC to provide a single mutation, based on mutationOptions
withMutation(mutationOptions),
// HOC to give access to the redux store & related actions
connect(mapStateToProps, mapDispatchToProps)
);