Skip to content

Commit dba55d7

Browse files
committed
Navigating to a contest url
1 parent b6a2c39 commit dba55d7

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

src/components/App.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import React from 'react';
22
import Header from './Header';
33
import ContestList from './ContestList';
44

5+
const pushState = (obj, url) =>
6+
window.history.pushState(obj, '', url);
7+
58
class App extends React.Component {
69
state = {
710
pageHeader: 'Naming Contests',
@@ -12,12 +15,19 @@ class App extends React.Component {
1215
}
1316
componentWillUnmount () {
1417
}
15-
18+
fetchContest = (contestId) => {
19+
pushState(
20+
{ currentContestId: contestId},
21+
`/contest/${contestId}`
22+
);
23+
};
1624
render () {
1725
return (
1826
<div className="App">
1927
<Header message={this.state.pageHeader} />
20-
<ContestList contests={this.state.contests} />
28+
<ContestList
29+
onContestClick = {this.fetchContest}
30+
contests={this.state.contests} />
2131
</div>
2232
);
2333
}

src/components/ContestList.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import React from 'react';
22
import ContestPreview from './ContestPreview';
33

4-
const ContestList = ({contests}) => (
4+
const ContestList = ({contests, onContestClick}) => (
55
<div className="ContestList">
66
{contests.map(contest =>
7-
<ContestPreview key={contest.id} {...contest}/>
7+
<ContestPreview
8+
key={contest.id}
9+
onClick={onContestClick} {...contest}/>
810
)}
911
</div>
1012
);
1113

1214
ContestList.propTypes = {
13-
contests: React.PropTypes.array
15+
contests: React.PropTypes.array,
16+
onContestClick: React.PropTypes.func.isRequired,
1417
};
1518

1619
export default ContestList;

src/components/ContestPreview.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, { Component } from 'react';
33
class ContestPreview extends Component {
44

55
handleClick = () => {
6-
console.log(this.props.contestName);
6+
this.props.onClick(this.props.id)
77
}
88

99
render() {
@@ -21,8 +21,10 @@ class ContestPreview extends Component {
2121
}
2222

2323
ContestPreview.propTypes = {
24+
id: React.PropTypes.number.isRequired,
2425
categoryName: React.PropTypes.string.isRequired,
25-
contestName: React.PropTypes.string.isRequired
26+
contestName: React.PropTypes.string.isRequired,
27+
onClick: React.PropTypes.func.isRequired,
2628
};
2729

2830
export default ContestPreview;

0 commit comments

Comments
 (0)