-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ios.js
108 lines (92 loc) · 2.27 KB
/
index.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
import React, {
AppRegistry,
Component,
StyleSheet,
Text,
View,
ListView,
AlertIOS
} from 'react-native';
import Firebase from'firebase';
import StatusBar from'./components/StatusBar';
import ActionButton from'./components/ActionButton';
import ListItem from'./components/ListItem';
import styles from './styles.js';
class GroceryApp extends React.Component {
constructor(props) {
super(props);
this.itemsRef = new Firebase("<https://kc-grocery-list.firebaseio.com/items")
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
})
};
}
componentDidMount() {
this.listenForItems(this.itemsRef);
}
_renderItem(item) {
const onPress = () => {
AlertIOS.prompt(
'Complete',
'Cross off '+item.title+'?',
[
{text: 'Complete', onPress: (text) => this.itemsRef.child(item._key).remove()},
{text: 'Cancel', onPress: (text) => console.log('Cancel')},
{text: '???', onPress: (text) => console.log('mystery Button')}
],
'default'
);
}
return (
<ListItem item={item} onPress={onPress}/>
);
}
_addItem() {
AlertIOS.prompt(
'Add New Item',
null,
[
{
text: 'Add',
onPress: (text) => {
this.itemsRef.push({ title: text})
}
},
],
'plain-text'
);
}
render() {
return (
<View style={styles.container}>
<StatusBar title="Grocery List" />
<ListView
dataSource={this.state.dataSource}
renderRow={this._renderItem.bind(this)}
style={styles.linview}/>
<ActionButton title="Add" onPress={this._addItem.bind(this)} />
</View>
);
}
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)
});
});
}
}
AppRegistry.registerComponent('GroceryApp', () => GroceryApp);