Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/config/default.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"api": "https://hacker-news.firebaseio.com/v0",
"apiSearch": "http://hn.algolia.com/api/v1/",

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename apiSearch to search to fit in with api and base

"base": "https://news.ycombinator.com",
"colors": {
"orange": "#EE6F2D",
Expand Down
31 changes: 31 additions & 0 deletions app/helpers/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import cheerio from 'cheerio-without-node-native';
import config from '../config/default';

/**
* Gets post from given query
* @param {String} query The query string to search for
* @param {Object} options The options for the search, see the 'searchOptions' variable in helpers/api.js
* @return {Promise} Returns a promise
*/

const searchPost = (options = defaultSearchOptions, query='') => {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does defaultSearchOptions come from?


const searchOptions = {
sortByDate: false || options.sortByDate,
pageNumber: 1 || options.pageNumber,
tags: 'story' || options.tag,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these variables see my comments below:

const searchOptions = {
  sortByDate: false || options.sortByDate, // will always be options.sortByDate or undefined as false || <anything> will always be <anything>
  pageNumber: 1 || options.pageNumber, // will always be 1
  tags: 'story' || options.tag, // will always be 'story`
}

Better to do change the condition for all three, e.g: sortByDate: options.sortByDate || false

};

const hitsPerPage = '&hitsPerPage=25';

// There has to be better way to do this
// TODO: Refactor this
const dateParameter = searchOptions.sortByDate ? 'search_by_date' : 'search';
const queryParameter = `query=${query}`;
const tagParameter = `&tags=${searchOptions.tags}`

const searchUrl = `${config.apiSearch}${dateParameter}?${queryParameter}${hitsPerPage}${tagParameter}`;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change config. apiSearch to config.search once updated in config/default.json


return fetch(searchUrl)
.then(response => response.json())
.catch(error => error);
}

/**
* Get item from given ID
* @param {String} itemId The ID of the item to fetch
Expand Down Expand Up @@ -364,4 +394,5 @@ export {
logout,
getComments,
toggleComments,
searchPost,
};
20 changes: 19 additions & 1 deletion app/screens/Search.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import React from 'react';
import commonStyles from '../styles/common';

import {
Text,
View,
} from 'react-native';

import { searchPost } from '../helpers/api';

export default class Search extends React.Component {
constructor(props) {
super(props);

this.state = {
searchResults: [],
};
}

componentDidMount() {
searchPost({}, 'dog').then(responseJson => {
this.setState({
searchResults: responseJson.hits,
});
});
}

render() {
const { searchResults } = this.state;
return (
<View>
<Text>Search Screen</Text>
{searchResults.map(result => (
<Text>{result.title}</Text>
))}
</View>
);
}
Expand Down