Skip to content

Commit b1ca0fb

Browse files
committed
finish for now
1 parent cff06e5 commit b1ca0fb

File tree

11 files changed

+139
-20
lines changed

11 files changed

+139
-20
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"react-dom": "^15.5.4",
1111
"react-redux": "^5.0.5",
1212
"redux": "^3.6.0",
13-
"redux-logger": "^3.0.6"
13+
"redux-logger": "^3.0.6",
14+
"redux-saga": "^0.15.3"
1415
},
1516
"scripts": {
1617
"start": "react-scripts start",

src/actions/story.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {
2+
STORIES_ADD,
3+
STORIES_FETCH,
4+
} from '../constants/actionTypes';
5+
6+
const doAddStories = stories => ({
7+
type: STORIES_ADD,
8+
stories,
9+
});
10+
11+
const doFetchStories = query => ({
12+
type: STORIES_FETCH,
13+
query,
14+
});
15+
16+
export {
17+
doAddStories,
18+
doFetchStories,
19+
};

src/api/story.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const HN_BASE_URL = 'http://hn.algolia.com/api/v1/search?query=';
2+
3+
const fetchStories = query =>
4+
fetch(HN_BASE_URL + query)
5+
.then(response => response.json());
6+
7+
export {
8+
fetchStories,
9+
};

src/components/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import React, { Component } from 'react';
22
import './App.css';
33

44
import Stories from './Stories';
5+
import SearchStories from './SearchStories';
56

67
class App extends Component {
78
render() {
89
return (
910
<div className="app">
11+
<div className="interactions">
12+
<SearchStories />
13+
</div>
1014
<Stories />
1115
</div>
1216
);

src/components/Buttons.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ const Button = ({
3030
export default Button;
3131

3232
export {
33+
Button,
3334
ButtonInline,
34-
};
35+
};

src/components/SearchStories.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React, { Component } from 'react';
2+
import { connect } from 'react-redux';
3+
import { doFetchStories } from '../actions/story';
4+
import { Button } from './Buttons';
5+
6+
const applyQueryState = query => () => ({
7+
query
8+
});
9+
10+
class SearchStories extends Component {
11+
constructor(props) {
12+
super(props);
13+
14+
this.state = {
15+
query: '',
16+
};
17+
18+
this.onChange = this.onChange.bind(this);
19+
this.onSubmit = this.onSubmit.bind(this);
20+
}
21+
22+
onSubmit(event) {
23+
const { query } = this.state;
24+
if (query) {
25+
// TODO: not defined yet
26+
this.props.onFetchStories(query)
27+
28+
this.setState(applyQueryState(''));
29+
}
30+
31+
event.preventDefault();
32+
}
33+
34+
onChange(event) {
35+
const { value } = event.target;
36+
this.setState(applyQueryState(value));
37+
}
38+
39+
render() {
40+
return (
41+
<form onSubmit={this.onSubmit}>
42+
<input
43+
type="text"
44+
value={this.state.query}
45+
onChange={this.onChange}
46+
/>
47+
<Button type="submit">
48+
Search
49+
</Button>
50+
</form>
51+
);
52+
}
53+
}
54+
55+
const mapDispatchToProps = (dispatch) => ({
56+
onFetchStories: query => dispatch(doFetchStories(query)),
57+
});
58+
59+
export default connect(
60+
null,
61+
mapDispatchToProps
62+
)(SearchStories);

src/constants/actionTypes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export const STORY_ARCHIVE = 'STORY_ARCHIVE';
2+
export const STORIES_FETCH = 'STORIES_FETCH';
3+
export const STORIES_ADD = 'STORIES_ADD';

src/reducers/story.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
const INITIAL_STATE = [
2-
{
3-
title: 'React',
4-
url: 'https://facebook.github.io/react/',
5-
author: 'Jordan Walke',
6-
num_comments: 3,
7-
points: 4,
8-
objectID: 0,
9-
}, {
10-
title: 'Redux',
11-
url: 'https://github.com/reactjs/redux',
12-
author: 'Dan Abramov, Andrew Clark',
13-
num_comments: 2,
14-
points: 5,
15-
objectID: 1,
16-
},
17-
];
1+
import { STORIES_ADD } from '../constants/actionTypes';
2+
3+
const INITIAL_STATE = [];
4+
5+
const applyAddStories = (state, action) =>
6+
[ ...action.stories ];
187

198
function storyReducer(state = INITIAL_STATE, action) {
209
switch(action.type) {
10+
case STORIES_ADD : {
11+
return applyAddStories(state, action);
12+
}
2113
default : return state;
2214
}
2315
}

src/sagas/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { takeEvery, all } from 'redux-saga/effects';
2+
import { STORIES_FETCH } from '../constants/actionTypes';
3+
import { handleFetchStories } from './story';
4+
5+
function *watchAll() {
6+
yield all([
7+
takeEvery(STORIES_FETCH, handleFetchStories),
8+
]);
9+
}
10+
11+
export default watchAll;

src/sagas/story.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { call, put } from 'redux-saga/effects';
2+
import { doAddStories } from '../actions/story';
3+
import { fetchStories } from '../api/story';
4+
5+
function* handleFetchStories(action) {
6+
const { query } = action;
7+
const result = yield call(fetchStories, query);
8+
yield put(doAddStories(result.hits));
9+
}
10+
11+
export {
12+
handleFetchStories,
13+
};

0 commit comments

Comments
 (0)