File tree 2 files changed +15
-24
lines changed
2 files changed +15
-24
lines changed Original file line number Diff line number Diff line change 1
1
import React , { Component } from 'react' ;
2
2
import { observable , action } from 'mobx' ;
3
- import { observer } from 'mobx-react' ;
3
+ import { observer , inject } from 'mobx-react' ;
4
4
import Button from './Button' ;
5
5
6
- @observer
6
+ const HN_BASE_URL = 'http://hn.algolia.com/api/v1/search?query=' ;
7
+
8
+ const fetchStories = ( query ) =>
9
+ fetch ( HN_BASE_URL + query )
10
+ . then ( response => response . json ( ) ) ;
11
+
12
+ @inject ( 'storyStore' ) @observer
7
13
class SearchStories extends Component {
8
14
@observable query = '' ;
9
15
@@ -23,8 +29,8 @@ class SearchStories extends Component {
23
29
@action
24
30
onSubmit ( event ) {
25
31
if ( this . query ) {
26
- // TODO do API fetch stories
27
- console . log ( this . query ) ;
32
+ fetchStories ( this . query )
33
+ . then ( result => this . props . storyStore . setStories ( result . hits ) )
28
34
29
35
this . query = '' ;
30
36
}
Original file line number Diff line number Diff line change 1
- import { observable , computed } from 'mobx' ;
2
-
3
- const INITIAL_STATE = [
4
- {
5
- title : 'React' ,
6
- url : 'https://facebook.github.io/react/' ,
7
- author : 'Jordan Walke' ,
8
- num_comments : 3 ,
9
- points : 4 ,
10
- objectID : 0 ,
11
- } , {
12
- title : 'Redux' ,
13
- url : 'https://github.com/reactjs/redux' ,
14
- author : 'Dan Abramov, Andrew Clark' ,
15
- num_comments : 2 ,
16
- points : 5 ,
17
- objectID : 1 ,
18
- } ,
19
- ] ;
1
+ import { observable , computed , action } from 'mobx' ;
20
2
21
3
const isNotArchived = ( archivedStoryIds ) => ( story ) =>
22
4
archivedStoryIds . indexOf ( story . objectID ) === - 1 ;
23
5
24
6
class StoryStore {
25
- @observable stories = INITIAL_STATE ;
7
+ @observable stories = [ ] ;
26
8
27
9
constructor ( rootStore ) {
28
10
this . rootStore = rootStore ;
29
11
}
30
12
13
+ @action setStories = stories =>
14
+ this . stories = stories ;
15
+
31
16
@computed get readableStories ( ) {
32
17
const { archivedStoryIds } = this . rootStore . archiveStore ;
33
18
return this . stories . filter ( isNotArchived ( archivedStoryIds ) ) ;
You can’t perform that action at this time.
0 commit comments