/
index.html
122 lines (117 loc) · 4.07 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Most Popular JavaScript Projects in Github</title>
<link
rel="stylesheet"
href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous"
/>
<link rel="stylesheet" href="main.css" />
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const LoadingHandler = () => (
<div className="container">
<h1 className="text-center">Loading...</h1>
</div>
)
const ErrorHandler = state => (
<div className="container">
<h1>呀,出错了 {state.error.message}</h1>
</div>
)
class ReportList extends React.Component {
constructor(props) {
super(props)
this.state = {
loading: true,
data: null,
error: null,
}
this.getKB = this.getKB.bind(this)
}
componentDidMount() {
this.props.promise.then(
value => this.setState({ loading: false, data: value }),
error => this.setState({ loading: false, error: error })
)
}
getKB(target) {
return Math.ceil(target / 1000) + 'k'
}
render() {
if (this.state.loading) {
return <LoadingHandler />
} else if (this.state.error) {
return <ErrorHandler />
} else {
const items = this.state.data.items
const lists = items.map((item, index) => {
const star = this.getKB(item.stargazers_count)
const fork = this.getKB(item.forks)
return (
<li key={index}>
<div className="media">
<div className="ranking mr-3">{index + 1}</div>
<div className="media-body">
<h4 className="title mt-0">
<a href={item.html_url}>{item.name}</a>
</h4>
<p className="desc">{item.description}</p>
</div>
<div className="icon ml-3 text-right">
{star} <img src="./icon-star.svg" />
<br />
{fork} <img src="./icon-fork.svg" />
</div>
</div>
</li>
)
})
return (
<div className="container">
<h1 className="display-6">Most Popular JavaScript Projects in Github</h1>
<div className="date-wrap text-center">
<Clock />
</div>
<ul className="items-wrap">{lists}</ul>
</div>
)
}
}
}
class Clock extends React.Component {
constructor(props) {
super(props)
this.state = { date: new Date() }
}
componentDidMount() {
this.timerID = setInterval(() => this.tick(), 1000)
}
componentWillUnmount() {
clearInterval(this.timerID)
}
tick() {
this.setState({ date: new Date() })
}
render() {
return <span>{this.state.date.toLocaleTimeString()}</span>
}
}
ReactDOM.render(
<ReportList promise={$.getJSON('https://api.github.com/search/repositories?q=javascript&sort=stars')} />,
document.getElementById('root')
)
</script>
</body>
</html>