Skip to content

Commit ccb9661

Browse files
committed
part 14
1 parent 7081bb8 commit ccb9661

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

src/components/SearchStories.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import React, { Component } from 'react';
22
import { observable, action } from 'mobx';
3-
import { observer } from 'mobx-react';
3+
import { observer, inject } from 'mobx-react';
44
import 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
713
class 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
}

src/stores/storyStore.js

+5-20
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
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

213
const isNotArchived = (archivedStoryIds) => (story) =>
224
archivedStoryIds.indexOf(story.objectID) === -1;
235

246
class 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));

0 commit comments

Comments
 (0)