Skip to content

Instantly share code, notes, and snippets.

@ecavalcanti
Last active October 17, 2017 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecavalcanti/1f33a0902095e1b12f654fcdae779dc2 to your computer and use it in GitHub Desktop.
Save ecavalcanti/1f33a0902095e1b12f654fcdae779dc2 to your computer and use it in GitHub Desktop.
import React from 'react'
import { TouchableOpacity, View, FlatList, Text, Image } from 'react-native'
import md5 from 'js-md5'
const PUBLIC_KEY = 'SUA_PUBLIC_KEY'
const PRIVATE_KEY = 'SUA_PRIVATE_KEY'
export default class Home extends React.PureComponent {
static navigationOptions = {
title: 'Heroes'
}
state = {
data: []
}
async componentDidMount() {
const timestamp = Number(new Date())
const hash = md5.create()
hash.update(timestamp + PRIVATE_KEY + PUBLIC_KEY)
const response = await fetch(`https://gateway.marvel.com/v1/public/characters?ts=${timestamp}&orderBy=name&limit=10&apikey=${PUBLIC_KEY}&hash=${hash.hex()}`)
const responseJson = await response.json()
this.setState({data: responseJson.data.results})
}
_renderItem = ({item}) => {
return (
<TouchableOpacity onPress={()=>this._onItemPress(item)} style={{flexDirection:'row', padding: 10, alignItems:'center'}}>
<Image style={{height: 50, width: 50, borderRadius: 25}} source={{uri: `${item.thumbnail.path}.${item.thumbnail.extension}` }} />
<Text style={{marginLeft: 10}}>{item.name}</Text>
</TouchableOpacity>
)
}
_onItemPress = (item) => {
this.props.navigation.navigate('Description', {hero: item})
}
render() {
return (
<FlatList
data={this.state.data}
renderItem={this._renderItem}
keyExtractor={(item) => item.id}
ItemSeparatorComponent={()=>
<View style={{height:1, backgroundColor: '#f7f7f7'}}
/>}
/>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment