Skip to content

Commit c651a24

Browse files
committed
Looking up the contest on route change.
1 parent dba55d7 commit c651a24

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

api/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ import express from 'express';
22
import data from '../src/testData';
33

44
const router = express.Router();
5+
const contests = data.contests.reduce((obj, contest) => {
6+
obj[contest.id] = contest;
7+
return obj;
8+
}, {});
59

610
router.get('/contests', (req, res) => {
7-
res.send({contests: data.contests});
11+
res.send({
12+
contests: contests
13+
});
814
});
915

1016
export default router;

src/components/App.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import Header from './Header';
33
import ContestList from './ContestList';
4+
import Contest from './Contest';
45

56
const pushState = (obj, url) =>
67
window.history.pushState(obj, '', url);
@@ -20,14 +21,26 @@ class App extends React.Component {
2021
{ currentContestId: contestId},
2122
`/contest/${contestId}`
2223
);
24+
// Look up contest
25+
// this.state.contests[contestId]
26+
this.setState({
27+
pageHeader: this.state.contests[contestId].contestName,
28+
currentContestId: contestId
29+
});
2330
};
31+
currentContent() {
32+
if (this.state.currentContestId) {
33+
return <Contest {...this.state.contests[this.state.currentContestId]}/>;
34+
}
35+
return <ContestList
36+
onContestClick = {this.fetchContest}
37+
contests={this.state.contests} />;
38+
}
2439
render () {
2540
return (
2641
<div className="App">
2742
<Header message={this.state.pageHeader} />
28-
<ContestList
29-
onContestClick = {this.fetchContest}
30-
contests={this.state.contests} />
43+
{this.currentContent()}
3144
</div>
3245
);
3346
}

src/components/Contest.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React, { Component, PropTypes } from 'react';
2+
3+
class Contest extends Component {
4+
render() {
5+
return (
6+
<div className="Contest">
7+
{this.props.id}
8+
</div>
9+
);
10+
}
11+
}
12+
13+
Contest.PropTypes = {
14+
id: PropTypes.number.isRequired
15+
}
16+
17+
export default Contest;

src/components/ContestList.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ import ContestPreview from './ContestPreview';
33

44
const ContestList = ({contests, onContestClick}) => (
55
<div className="ContestList">
6-
{contests.map(contest =>
6+
{Object.keys(contests).map(contestId =>
77
<ContestPreview
8-
key={contest.id}
9-
onClick={onContestClick} {...contest}/>
8+
key={contestId}
9+
onClick={onContestClick}
10+
{...contests[contestId]}/>
1011
)}
1112
</div>
1213
);
1314

1415
ContestList.propTypes = {
15-
contests: React.PropTypes.array,
16+
contests: React.PropTypes.object,
1617
onContestClick: React.PropTypes.func.isRequired,
1718
};
1819

0 commit comments

Comments
 (0)