File tree Expand file tree Collapse file tree 2 files changed +15
-24
lines changed Expand file tree Collapse file tree 2 files changed +15
-24
lines changed Original file line number Diff line number Diff line change 11import  React ,  {  Component  }  from  'react' ; 
22import  {  observable ,  action  }  from  'mobx' ; 
3- import  {  observer  }  from  'mobx-react' ; 
3+ import  {  observer ,   inject  }  from  'mobx-react' ; 
44import  Button  from  './Button' ; 
55
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 
713class  SearchStories  extends  Component  { 
814  @observable  query  =  '' ; 
915
@@ -23,8 +29,8 @@ class SearchStories extends Component {
2329  @action 
2430  onSubmit ( event )  { 
2531    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 ) ) 
2834
2935      this . query  =  '' ; 
3036    } 
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' ; 
202
213const  isNotArchived  =  ( archivedStoryIds )  =>  ( story )  => 
224  archivedStoryIds . indexOf ( story . objectID )  ===  - 1 ; 
235
246class  StoryStore  { 
25-   @observable  stories  =  INITIAL_STATE ; 
7+   @observable  stories  =  [ ] ; 
268
279  constructor ( rootStore )  { 
2810    this . rootStore  =  rootStore ; 
2911  } 
3012
13+   @action  setStories  =  stories  => 
14+     this . stories  =  stories ; 
15+ 
3116  @computed  get  readableStories ( )  { 
3217    const  {  archivedStoryIds }  =  this . rootStore . archiveStore ; 
3318    return  this . stories . filter ( isNotArchived ( archivedStoryIds ) ) ; 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments