Created
April 4, 2018 19:48
-
-
Save Ayyagaries/f95969882ce7086d1a4e9e6714d12dde to your computer and use it in GitHub Desktop.
homescreen with spinner
This file contains hidden or 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 PropTypes from 'prop-types'; | |
import { connect } from 'react-redux'; | |
import { StyleSheet, View, FlatList } from 'react-native'; | |
import Icon from 'react-native-vector-icons/FontAwesome'; | |
import { List, ListItem, SearchBar } from 'react-native-elements'; | |
import { Container, Content, Text, Button, Label } from 'native-base'; | |
import Spinner from 'react-native-loading-spinner-overlay'; | |
import { requestAttachedFacilities } from '../actions/pickFacility'; | |
class PickFacility extends Component { | |
static navigationOptions = { | |
drawerLabel: 'Pick a Facility', | |
title: 'Pick a Facility', | |
drawerIcon: () => <Icon name="ios-home" />, | |
}; | |
static propTypes = { | |
navigation: PropTypes.object, | |
dispatch: PropTypes.func, | |
PickFacility: PropTypes.object, | |
}; | |
constructor(props) { | |
super(props); | |
this.state = { | |
data: [], | |
noData: false, | |
}; | |
} | |
componentDidMount() { | |
console.log('Component did mount'); | |
this.props.dispatch(requestAttachedFacilities()); | |
} | |
haandleLoadMore = () => { | |
console.log('end of page'); | |
}; | |
fliterList = (e) => { | |
const text = e.toLowerCase(); | |
const trucks = this.state.data; | |
const filteredName = trucks.filter(item => item.name.toLowerCase().match(text)); | |
if (!text || text === '') { | |
this.setState({ | |
data: this.props.PickFacility.attachedFacilities, | |
}); | |
} else if (!Array.isArray(filteredName) && !filteredName.length) { | |
// set no data flag to true so as to render flatlist conditionally | |
this.setState({ | |
noData: true, | |
}); | |
} else if (Array.isArray(filteredName)) { | |
this.setState({ | |
noData: true, | |
data: filteredName, | |
}); | |
} | |
}; | |
handleRetry = () => { | |
this.props.dispatch(requestAttachedFacilities()); | |
}; | |
listItemClick = (e) => { | |
console.log(e); | |
}; | |
spinnerView = () => ( | |
<Spinner visible textContent="Loading" textStyle={{ color: '#FFF' }} /> | |
); | |
listView = () => ( | |
<List containerStyle={{ borderTopWidth: 0, borderBottomWidth: 0 }}> | |
<FlatList | |
bounces={false} | |
data={this.state.noData ? this.state.data : this.props.PickFacility.attachedFacilities} | |
ItemSeparatorComponent={this.renderSeparator} | |
ListHeaderComponent={this.renderHeader} | |
refreshing={false} | |
renderItem={({ item }) => ( | |
<ListItem | |
onPress={this.listItemClick} | |
title={`${item.name}`} | |
containerStyle={{ borderBottomWidth: 0 }} | |
titleStyle={{ fontSize: 12, fontWeight: 'bold' }} | |
/> | |
)} | |
keyExtractor={item => item.id} | |
onEndReached={this.haandleLoadMore} | |
onEndReachedThreshold={0} | |
onPress | |
/> | |
</List> | |
); | |
networkErrorView = () => ( | |
<View | |
style={{ | |
flex: 1, | |
alignSelf: 'center', | |
marginTop: '50%', | |
}} | |
> | |
<Icon name="exclamation-triangle" color="#C0C0C0" size={30} style={styles.iconStyle} /> | |
<Label style={styles.securetext}>Network Connectivity Error</Label> | |
<Button style={styles.button} onPress={this.handleRetry}> | |
<Label style={styles.buttonLabel}>RETRY</Label> | |
</Button> | |
</View> | |
); | |
renderHeader = () => ( | |
<SearchBar | |
placeholder="Search Facility" | |
lightTheme | |
onChangeText={this.fliterList} | |
inputStyle={{ fontSize: 15 }} | |
/> | |
); | |
renderSeparator = () => ( | |
<View | |
style={{ | |
height: 1, | |
width: '100%', | |
backgroundColor: '#CED0CE', | |
}} | |
/> | |
); | |
render() { | |
console.log(this.props.PickFacility.isLoading); | |
if (this.props.PickFacility.attachedFacilities.length > 0) { | |
return ( | |
<Container style={styles.container}> | |
<Content> | |
{this.props.PickFacility.isLoading ? this.spinnerView() : this.listView()} | |
</Content> | |
</Container> | |
); | |
} else if (this.props.PickFacility.msg) { | |
return ( | |
<Container style={styles.container}> | |
<Content> | |
{this.spinnerView()} | |
{this.networkErrorView()} | |
</Content> | |
</Container> | |
); | |
} | |
return null; | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
// backgroundColor: 'yellow', | |
}, | |
boxContainer: { | |
flex: 1, // 1:3 | |
alignItems: 'center', | |
justifyContent: 'center', | |
alignSelf: 'stretch', | |
}, | |
boxZero: { | |
flex: 1, // 3:6 | |
// backgroundColor: '#EF4F43', | |
marginTop: '20%', | |
}, | |
boxOne: { | |
flex: 2, // 3:6, | |
// backgroundColor: '#888888', | |
}, | |
boxTwo: { | |
flex: 1, // 1:6 | |
// backgroundColor: '#F17F42', | |
}, | |
boxThree: { | |
flex: 1, // 2:6 | |
alignItems: 'center', | |
// backgroundColor: '#CE6D39', | |
marginBottom: '10%', | |
}, | |
header: { | |
backgroundColor: '#769654', | |
}, | |
button: { | |
alignSelf: 'center', | |
margin: '5%', | |
padding: '10%', | |
backgroundColor: '#769654', | |
}, | |
text: { | |
alignSelf: 'center', | |
}, | |
securetext: { | |
alignSelf: 'center', | |
fontSize: 20, | |
color: '#C0C0C0', | |
}, | |
copyrightText: { | |
alignSelf: 'center', | |
fontSize: 10, | |
}, | |
buttonLabel: { | |
color: '#FFF', | |
}, | |
versionrelease: { | |
fontSize: 10, | |
alignSelf: 'center', | |
}, | |
iconStyle: { | |
alignSelf: 'center', | |
}, | |
ListItemStyle: { | |
fontSize: 10, | |
}, | |
}); | |
const mapStateToProps = state => ({ PickFacility: state.pickFacility }); | |
export default connect(mapStateToProps)(PickFacility); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment