diff --git a/components/ListItem.js b/components/ListItem.js index 4f1cb0f..3329ff5 100644 --- a/components/ListItem.js +++ b/components/ListItem.js @@ -17,17 +17,16 @@ import colors from '../utils/colors'; const placeholder = require('../assets/default.png'); -const ListItem = ({ text, imageUrl }) => { - const image = ( - imageUrl ? {uri: imageUrl} : placeholder +const ListItem = ({ text, image }) => { + const imageUrl = ( + image ? {uri: image} : placeholder ); - return ( - + { text } diff --git a/components/Main.js b/components/Main.js index e842e73..f6a752f 100644 --- a/components/Main.js +++ b/components/Main.js @@ -13,10 +13,10 @@ StatusBar, View } from 'react-native'; - +import { debounce } from 'lodash'; import ListItem from './ListItem'; import colors from '../utils/colors'; - +import searchFor from '../utils/fetcher'; export default class Main extends Component { constructor(props){ super(props); @@ -25,34 +25,47 @@ export default class Main extends Component { rowHasChanged: (r1, r2) => r1 !== r2 }); - const data = ['Spectacles', 'Giraffe', 'Turtle', 'Shark', 'Lamp', - 'Beef', 'Drawer', 'Brocoli', 'Raspberries', 'Plate', 'Zebra']; - - this.state = { artists: dataSource.cloneWithRows(data) } + this.state = { articles: dataSource } } - renderRow = (text, sId, rId) => { + renderRow = (article, sId, rId) => { + const imageUrl = article.fields.thumbnail ? article.fields.thumbnail : null; return ( + text={ article.webTitle } + image={ imageUrl } /> ); }; + render() { - const { artists } = this.state; + const { articles } = this.state; return ( - + - ); } + + makeQuery = debounce(query => { + searchFor(query) + .then(articles => { + this.setState({ + articles: this.state.articles.cloneWithRows(articles), + }); + }) + .catch((error) => { + throw error; + }); + }, 400); } const styles = StyleSheet.create({ @@ -63,6 +76,10 @@ export default class Main extends Component { alignItems: 'center', backgroundColor: colors.white }, + listItem: { + flex:1, + alignSelf: 'stretch' + }, searchBox: { height: 40, borderColor: colors.black, diff --git a/package.json b/package.json index 005fdc7..ef564b0 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "start": "node node_modules/react-native/local-cli/cli.js start" }, "dependencies": { + "lodash": "^4.13.1", "react": "15.0.2", "react-native": "0.26.3" } diff --git a/utils/fetcher.js b/utils/fetcher.js new file mode 100644 index 0000000..dec0c9e --- /dev/null +++ b/utils/fetcher.js @@ -0,0 +1,15 @@ +export default function get(url){ + return fetch(url) + .then((response) => response.json()); +} + +export default function searchFor(query){ + const requestUrl = ( + `http://content.guardianapis.com/search?show-elements=all&show-fields=all&q=${ query }&type=article&api-key=GUARDIAN-API-KEY&order-by=newest` + ); + return get(requestUrl) + .then( (res) => { + const results = res.response.results ? res.response.results : []; + return results; + }); +}