-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
RepoPage.js
99 lines (86 loc) · 2.43 KB
/
RepoPage.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
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { loadRepo, loadStargazers } from '../actions';
import Repo from '../components/Repo';
import User from '../components/User';
import List from '../components/List';
function loadData(props) {
const { fullName } = props;
props.loadRepo(fullName, ['description']);
props.loadStargazers(fullName);
}
class RepoPage extends Component {
constructor(props) {
super(props);
this.renderUser = this.renderUser.bind(this);
this.handleLoadMoreClick = this.handleLoadMoreClick.bind(this);
}
componentWillMount() {
loadData(this.props);
}
componentWillReceiveProps(nextProps) {
if (nextProps.fullName !== this.props.fullName) {
loadData(nextProps);
}
}
handleLoadMoreClick() {
this.props.loadStargazers(this.props.fullName, true);
}
renderUser(user) {
return (
<User user={user}
key={user.login} />
);
}
render() {
const { repo, owner, name } = this.props;
if (!repo || !owner) {
return <h1><i>Loading {name} details...</i></h1>;
}
const { stargazers, stargazersPagination } = this.props;
return (
<div>
<Repo repo={repo}
owner={owner} />
<hr />
<List renderItem={this.renderUser}
items={stargazers}
onLoadMoreClick={this.handleLoadMoreClick}
loadingLabel={`Loading stargazers of ${name}...`}
{...stargazersPagination} />
</div>
);
}
}
RepoPage.propTypes = {
repo: PropTypes.object,
fullName: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
owner: PropTypes.object.isRequired,
stargazers: PropTypes.array.isRequired,
stargazersPagination: PropTypes.object,
loadRepo: PropTypes.func.isRequired,
loadStargazers: PropTypes.func.isRequired
};
function mapStateToProps(state, ownProps) {
const { login, name } = ownProps.params;
const {
pagination: { stargazersByRepo },
entities: { users, repos }
} = state;
const fullName = `${login}/${name}`;
const stargazersPagination = stargazersByRepo[fullName] || { ids: [] };
const stargazers = stargazersPagination.ids.map(id => users[id]);
return {
fullName,
name,
stargazers,
stargazersPagination,
repo: repos[fullName],
owner: users[login]
};
}
export default connect(
mapStateToProps,
{ loadRepo, loadStargazers }
)(RepoPage);