Created
April 18, 2018 07:04
-
-
Save seyna/90fba80eab3a3c931f051d94954f4213 to your computer and use it in GitHub Desktop.
add codes to fetch remote data and render it with spinner loading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import { Alert, ListView, BackHandler, FlatList } from 'react-native' // add Alert and ListView | |
import { Container, Text, Header, Content, Form, Item, Input, Label, Button, Icon, Picker, List, ListItem, Thumbnail, Body, Spinner } from 'native-base'; | |
import { connect } from 'react-redux' | |
// Add Actions - replace 'Your' with whatever your reducer is called :) | |
// import YourActions from '../Redux/YourRedux' | |
// Styles | |
// import styles from './Styles/MyNewContainerStyle' | |
import * as firebase from 'firebase'; | |
const StatusBar = require('../Components/StatusBar'); | |
const ActionButton = require('../Components/ActionButton'); | |
//const ListItem = require('../Components/ListItem'); | |
const styles = require('../Components/Styles/styles.js'); | |
// Initialize Firebase | |
const firebaseConfig = { | |
apiKey: "AIzaSyBGl9zIUTNLtzRB3WLr32TVY17jrZRrRSk", | |
authDomain: "", | |
databaseURL: "https://sooource-33fa3.firebaseio.com/", | |
storageBucket: "", | |
}; | |
const firebaseApp = firebase.initializeApp(firebaseConfig); | |
class MyNewContainer extends Component { | |
// constructor (props) { | |
// super(props) | |
// this.state = {} | |
// } | |
constructor(props) { | |
super(props); | |
this.state = { | |
dataSource: new ListView.DataSource({ | |
rowHasChanged: (row1, row2) => row1 !== row2, | |
}), | |
selected2: undefined, | |
username: undefined, | |
password: undefined, | |
isLoading: true | |
}; | |
this.itemsRef = this.getRef().child('items'); | |
} | |
onValueChange2(value: string) { | |
this.setState({ | |
selected2: value | |
}); | |
} | |
getRef() { | |
return firebaseApp.database().ref(); | |
} | |
listenForItems(itemsRef) { | |
itemsRef.on('value', (snap) => { | |
// get children as an array | |
var items = []; | |
snap.forEach((child) => { | |
items.push({ | |
title: child.val().title, | |
_key: child.key | |
}); | |
}); | |
this.setState({ | |
dataSource: this.state.dataSource.cloneWithRows(items) | |
}); | |
}); | |
} | |
componentDidMount() { | |
//this.listenForItems(this.itemsRef); | |
BackHandler.addEventListener('hardwareBackPress', () => { | |
this.props.navigation.goBack(); | |
return true | |
}) | |
return fetch('https://facebook.github.io/react-native/movies.json') | |
.then((response) => response.json()) | |
.then((responseJson) => { | |
this.setState({ | |
isLoading: false, | |
dataSource: responseJson.movies, | |
}, function () { | |
}); | |
}) | |
.catch((error) => { | |
console.error(error); | |
}); | |
} | |
render() { | |
if (this.state.isLoading) { | |
return ( | |
<Container style={{ flex: 1, padding: 20 }}> | |
<Spinner /> | |
</Container> | |
) | |
} | |
//var items = [ {key: 'Simon Mignolet', desc: 'Hello, world'}, {key: 'Nathaniel Clyne'} ]; | |
return ( | |
<Container> | |
<Header /> | |
<Content> | |
<FlatList data={this.state.dataSource} | |
renderItem={ ( {item} ) => | |
<ListItem> | |
<Thumbnail square size={80} source={{ uri: 'https://ap.rdcpix.com/747161936/3283050dfa803e250276918b79a5425fl-m4xd-r3_w100_h100_q80.jpg' }} /> | |
<Body> | |
<Text>{item.title}</Text> | |
<Text note>{item.releaseYear}</Text> | |
</Body> | |
</ListItem> | |
}> | |
</FlatList> | |
<Form> | |
<Item stackedLabel> | |
<Label>Username</Label> | |
<Input onChangeText={(username) => this.setState({ username })} | |
value={this.state.username} /> | |
</Item> | |
<Item stackedLabel last> | |
<Label>Password</Label> | |
<Input secureTextEntry={true} onChangeText={(password) => this.setState({ password })} | |
value={this.state.password}/> | |
</Item> | |
<Picker | |
mode="dropdown" | |
iosIcon={<Icon name="ios-arrow-down-outline" />} | |
placeholder="Select your SIM" | |
placeholderStyle={{ color: "#bfc6ea" }} | |
placeholderIconColor="#007aff" | |
style={{ width: undefined }} | |
selectedValue={this.state.selected2} | |
onValueChange={this.onValueChange2.bind(this)} | |
> | |
<Picker.Item label="Wallet" value="key0" /> | |
<Picker.Item label="ATM Card" value="key1" /> | |
<Picker.Item label="Debit Card" value="key2" /> | |
<Picker.Item label="Credit Card" value="key3" /> | |
<Picker.Item label="Net Banking" value="key4" /> | |
</Picker> | |
<Button block onPress={this._addItem.bind(this)}> | |
<Text>Primary</Text> | |
</Button> | |
</Form> | |
</Content> | |
</Container> | |
) | |
} | |
_addItem(text) { | |
Alert.alert( | |
'Add New Item', | |
null, | |
[ | |
{ text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel' }, | |
{ | |
text: 'Add', | |
onPress: (text) => { | |
//this.itemsRef.push({ title: "test" }) | |
this.itemsRef.push({ | |
username: this.state.username, | |
password: this.state.password, | |
selected2: this.state.selected2 | |
}).then(function () { _renderMessage() }) | |
} | |
}, | |
] | |
); | |
} | |
_renderItem(item) { | |
const onPress = () => { | |
Alert.alert( | |
'Complete', | |
null, | |
[ | |
{ text: 'Complete', onPress: (text) => this.itemsRef.child(item._key).remove() }, | |
{ text: 'Cancel', onPress: (text) => console.log('Cancelled') } | |
] | |
); | |
}; | |
return ( | |
<ListItem item={item} onPress={onPress} /> | |
); | |
} | |
} | |
const _renderMessage = (text) => { | |
Alert.alert( | |
'Success', | |
null, | |
[ | |
{ text: 'OK', onPress: () => console.log('Cancel Pressed'), style: 'cancel' }, | |
] | |
); | |
} | |
const mapStateToProps = (state) => { | |
return { | |
} | |
} | |
const mapDispatchToProps = (dispatch) => { | |
return { | |
} | |
} | |
export default connect(mapStateToProps, mapDispatchToProps)(MyNewContainer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment