-
-
Notifications
You must be signed in to change notification settings - Fork 830
/
PostsUserPage.js
147 lines (127 loc) · 3.12 KB
/
PostsUserPage.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
142
143
144
145
146
147
import UserPage from 'flarum/components/UserPage';
import LoadingIndicator from 'flarum/components/LoadingIndicator';
import Button from 'flarum/components/Button';
import CommentPost from 'flarum/components/CommentPost';
/**
* The `PostsUserPage` component shows a user's activity feed inside of their
* profile.
*/
export default class PostsUserPage extends UserPage {
init() {
super.init();
/**
* Whether or not the activity feed is currently loading.
*
* @type {Boolean}
*/
this.loading = true;
/**
* Whether or not there are any more activity items that can be loaded.
*
* @type {Boolean}
*/
this.moreResults = false;
/**
* The Post models in the feed.
*
* @type {Post[]}
*/
this.posts = [];
/**
* The number of activity items to load per request.
*
* @type {Integer}
*/
this.loadLimit = 20;
this.loadUser(m.route.param('username'));
}
content() {
let footer;
if (this.loading) {
footer = LoadingIndicator.component();
} else if (this.moreResults) {
footer = (
<div className="PostsUserPage-loadMore">
{Button.component({
children: app.trans('core.forum.user_posts_load_more_button'),
className: 'Button',
onclick: this.loadMore.bind(this)
})}
</div>
);
}
return (
<div className="PostsUserPage">
<ul className="PostsUserPage-list">
{this.posts.map(post => (
<li>
<div className="PostsUserPage-discussion">
In <a href={app.route.post(post)} config={m.route}>{post.discussion().title()}</a>
</div>
{CommentPost.component({post, showDiscussionTitle: true})}
</li>
))}
</ul>
{footer}
</div>
);
}
/**
* Initialize the component with a user, and trigger the loading of their
* activity feed.
*/
show(user) {
super.show(user);
this.refresh();
}
/**
* Clear and reload the user's activity feed.
*
* @public
*/
refresh() {
this.loading = true;
this.posts = [];
m.lazyRedraw();
this.loadResults().then(this.parseResults.bind(this));
}
/**
* Load a new page of the user's activity feed.
*
* @param {Integer} [offset] The position to start getting results from.
* @return {Promise}
* @protected
*/
loadResults(offset) {
return app.store.find('posts', {
filter: {
user: this.user.id(),
type: 'comment'
},
page: {offset, limit: this.loadLimit},
sort: '-time'
});
}
/**
* Load the next page of results.
*
* @public
*/
loadMore() {
this.loading = true;
this.loadResults(this.posts.length).then(this.parseResults.bind(this));
}
/**
* Parse results and append them to the activity feed.
*
* @param {Post[]} results
* @return {Post[]}
*/
parseResults(results) {
this.loading = false;
[].push.apply(this.posts, results);
this.moreResults = results.length >= this.loadLimit;
m.redraw();
return results;
}
}