1
1
import React , { Component } from 'react' ;
2
- import logo from './logo.svg' ;
3
2
import Scoreboard from './scoreboard' ;
4
3
import DayChooser from './daychooser' ;
5
4
import GameService from './GameService' ;
6
-
5
+ import Loader from './loader.js' ;
6
+ import GameDetail from './gameDetail' ;
7
+ import NoGames from './noGames' ;
8
+ import rp from 'request-promise' ;
7
9
import './App.css' ;
8
- // import TeamsJson from './teams.json';
9
10
10
- var request = require ( 'request' ) ;
11
11
var moment = require ( 'moment' ) ;
12
12
13
13
class App extends Component {
14
14
constructor ( props ) {
15
15
super ( props ) ;
16
- this . state = { myGames : [ ] , myTeams : [ ] , currentDate : moment ( ) } ;
16
+ this . state = {
17
+ myGames : [ ] ,
18
+ myTeams : [ ] ,
19
+ currentDate : moment ( ) ,
20
+ currentGame : null ,
21
+ fullGame : null ,
22
+ isLoading : true
23
+ } ;
17
24
18
25
// This binding is necessary to make `this` work in the callback
19
26
this . changeDate = this . changeDate . bind ( this ) ;
27
+ this . selectGame = this . selectGame . bind ( this ) ;
28
+ this . closeGame = this . closeGame . bind ( this ) ;
20
29
}
21
30
22
31
componentDidMount = ( ) => {
23
- const gameDate = moment ( ) . format ( 'YYYY-M-D' ) ;
32
+ // const gameDate = moment().format('YYYY-M-D');
24
33
25
- setTimeout ( ( ) => this . getData ( gameDate ) , 1200 ) ;
34
+ // setTimeout(() => this.getData(gameDate), 1200);
35
+
36
+
37
+ var reload = ( ) => {
38
+ this . getData ( moment ( this . state . currentDate ) . format ( 'YYYY-M-D' ) ) ;
39
+ } ;
40
+
41
+ reload ( ) ;
42
+
43
+ // setInterval(reload , 30000);
26
44
}
27
45
28
46
changeDate ( increment ) {
@@ -32,55 +50,110 @@ class App extends Component {
32
50
this . getData ( moment ( gameDate ) . format ( 'YYYY-M-D' ) ) ;
33
51
}
34
52
35
- getData ( gameDate ) {
36
- const teamsUrl = 'https://statsapi.web.nhl.com/api/v1/teams/' ;
37
-
38
- request ( teamsUrl , ( error , response , body ) => {
39
-
40
- if ( error != undefined ) {
41
- console . log ( error . message ) ;
42
- } else {
43
- const teams = JSON . parse ( body ) . teams ;
53
+ selectGame ( game ) {
54
+ this . getGame ( game . link ) ;
55
+ this . setState ( { currentGame : game } ) ;
56
+ }
57
+
58
+ closeGame ( ) {
59
+
60
+ this . setState ( { currentGame : null } ) ;
61
+ }
62
+
63
+ getGame ( gameLink ) {
64
+
65
+ const url = 'https://statsapi.web.nhl.com' + gameLink ;
66
+
67
+ rp ( url ) . then ( ( gameData ) => {
44
68
45
- this . setState ( { myTeams : teams . teams } ) ;
69
+ const data = JSON . parse ( gameData ) ;
70
+
71
+ this . setState ( { fullGame : data } ) ;
72
+
73
+ } ) ;
74
+ }
75
+
76
+ getData ( gameDate ) {
77
+
78
+ // this.setState({isLoading: true});
79
+ const gameService = new GameService ( ) ;
80
+ var teamDataPromise = gameService . getTeams ( ) ;
81
+
82
+ teamDataPromise . then ( ( d ) => {
83
+ const teams = JSON . parse ( d ) . teams ;
46
84
47
- const scoresUrl = 'https://statsapi.web.nhl.com/api/v1/schedule?startDate=' + gameDate + '&endDate=' + gameDate + '&expand=schedule.linescore' ;
85
+ this . setState ( { myTeams : teams . teams } ) ;
48
86
49
- request ( scoresUrl , ( error , response , body ) => {
50
-
51
- const games = JSON . parse ( body ) . dates [ 0 ] . games ;
87
+ gameService . getGamesForDate ( gameDate )
88
+ . then ( ( gameData ) => {
89
+
90
+ const data = JSON . parse ( gameData ) ;
91
+
92
+ if ( data . dates . length > 0 ) {
93
+ const games = data . dates [ 0 ] . games ;
94
+
95
+ const supplementedGames = games . map ( function ( g ) {
96
+ const away = teams . find ( x => x . id === g . teams [ 'away' ] . team . id ) ;
97
+ const home = teams . find ( x => x . id === g . teams [ 'home' ] . team . id ) ;
98
+ g . awayDetails = away ;
99
+ g . homeDetails = home ;
100
+ return g ;
101
+ } ) ;
102
+
103
+ this . setState ( { myGames : supplementedGames } ) ;
104
+ } else {
105
+ this . setState ( { myGames : [ ] } ) ;
106
+ }
107
+
108
+ this . setState ( { isLoading : false } ) ;
52
109
53
- const supplementedGames = games . map ( function ( g ) {
54
- const away = teams . find ( x => x . id === g . teams [ 'away' ] . team . id ) ;
55
- const home = teams . find ( x => x . id === g . teams [ 'home' ] . team . id ) ;
56
- g . awayDetails = away ;
57
- g . homeDetails = home ;
58
- return g ;
59
- } ) ;
60
- console . log ( supplementedGames ) ;
61
- this . setState ( { myGames : supplementedGames } ) ;
62
- } ) ;
63
- }
64
-
65
-
66
- } ) ;
110
+ } ) ;
67
111
68
-
112
+ } ) ;
69
113
}
70
114
71
115
72
116
render ( ) {
73
117
74
118
const listItems = this . state . myGames . map ( ( thisGame ) =>
75
- < Scoreboard game = { thisGame } key = { thisGame . gamePk } />
119
+ < Scoreboard game = { thisGame } key = { thisGame . gamePk } selectGame = { this . selectGame } />
76
120
) ;
77
121
122
+ var mainItem = listItems ;
123
+ var gameDetail = ( < div > </ div > ) ;
124
+
125
+ if ( this . state . isLoading ) {
126
+ mainItem = ( < Loader /> )
127
+ }
128
+ if ( this . state . myGames . length === 0 ) {
129
+ mainItem = ( < NoGames /> ) ;
130
+ }
131
+
132
+ if ( this . state . currentGame != null ) {
133
+ gameDetail = ( < GameDetail game = { this . state . currentGame } fullGame = { this . state . fullGame } closeGame = { this . closeGame } /> ) ;
134
+ }
135
+
78
136
return (
79
137
< div className = "App" >
80
- < div className = "App-header" >
81
- < DayChooser currentDate = { this . state . currentDate } changeCurrentDate = { this . changeDate } />
138
+
139
+ < div className = "row" >
140
+ < div className = "col-md-4" >
141
+ < div className = "row" >
142
+ < div className = "col-md-12" >
143
+
144
+ < div className = "App-header" >
145
+ < DayChooser currentDate = { this . state . currentDate } changeCurrentDate = { this . changeDate } />
146
+ </ div >
147
+ < div id = "game-list" >
148
+ { mainItem }
149
+ </ div >
150
+ </ div >
151
+ </ div >
152
+ </ div >
153
+ < div className = "col-md-8" >
154
+ { gameDetail }
155
+ </ div >
82
156
</ div >
83
- { listItems }
84
157
85
158
</ div >
86
159
) ;
0 commit comments