src/components/Groceries.js
import React, { Component } from 'react'
import {
ListView,
NetInfo,
StyleSheet,
Text,
TextInput,
View
} from 'react-native'
import Item from './Item'
export default class Groceries extends Component {
constructor(props) {
super(props)
this.state = {
newItem: ''
}
}
componentWillMount() {
this.dataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})
this.props.loadOfflineItems()
if (NetInfo) {
NetInfo.isConnected.fetch().done(isConnected => {
if (isConnected) {
this.props.checkConnection()
} else {
this.props.goOffline()
}
})
} else {
this.props.checkConnection()
}
}
renderRow(rowData) {
return (
<Item name={rowData.title}
removable={this.props.connected}
onRemove={() => this._remove(rowData.id)} />
)
}
_add() {
this.props.addItem(this.state.newItem);
this.setState({newItem: ''})
setTimeout(() => this.refs.newItem.focus(), 1)
}
_remove(id) {
this.props.removeItem(id)
}
render() {
let items, readonlyMessage
if (this.props.connected) {
items = this.props.onlineItems
} else if (this.props.connectionChecked) {
items = this.props.offlineItems
readonlyMessage = <Text style={styles.offline}>Offline</Text>
} else {
items = []
readonlyMessage = <Text style={styles.offline}>Loading...</Text>
}
return (
<View style={styles.container}>
{readonlyMessage}
<TextInput placeholder="Something delicious"
style={styles.newItem}
ref="newItem"
editable={this.props.connected}
value={this.state.newItem}
onChangeText={(newItem) => this.setState({newItem})}
onSubmitEditing={() => this._add()} />
<ListView
dataSource={this.dataSource.cloneWithRows(items)}
enableEmptySections={true}
renderRow={this.renderRow.bind(this)}
/>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 40,
backgroundColor: '#F6F6F6'
},
newItem: {
backgroundColor: '#FFFFFF',
height: 42,
borderColor: '#CCCCCC',
borderWidth: 1,
marginBottom: 10,
marginLeft: 20,
marginRight: 20,
paddingLeft: 10,
borderRadius: 5,
fontSize: 20
},
offline: {
backgroundColor: '#000000',
color: '#FFFFFF',
textAlign: 'center',
marginBottom: 10,
paddingTop: 5,
paddingBottom: 5
}
})