-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
138 lines (134 loc) · 3.62 KB
/
App.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from "react";
import {
AppRegistry,
TextInput,
Text,
View,
ListView,
TouchableHighlight,
Modal
} from "react-native";
import Toolbar from "./app/components/Toolbar/Toolbar";
import AddButton from "./app/components/AddButton/AddButton";
import * as firebase from "firebase";
const style = require("./app/style");
const config = {
apiKey: "AIzaSyCx8hweXVjJREwx9IW9w1CeU6ZDQbRvDOk",
authDomain: "itemlister-8db7d.firebaseapp.com",
databaseURL: "https://itemlister-8db7d.firebaseio.com",
projectId: "itemlister-8db7d",
storageBucket: "itemlister-8db7d.appspot.com",
messagingSenderId: "719183883473"
};
firebase.initializeApp(config);
export default class App extends Component {
constructor() {
super();
let ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
this.state = {
text: "",
itemDataSource: ds,
modalVisible: false
};
this.itemsRef = this.getRef().child("items");
this.renderRow = this.renderRow.bind(this);
this.pressRow = this.pressRow.bind(this);
}
setModalVisible(visible) {
this.setState({ modalVisible: visible });
}
getRef() {
return firebase.database().ref();
}
componentWillMount() {
this.getItems(this.itemsRef);
}
componentDidMount() {
this.getItems(this.itemsRef);
}
getItems(itemsRef) {
itemsRef.on("value", snap => {
let items = [];
snap.forEach(child => {
items.push({
title: child.val().title,
_key: child.key
});
});
this.setState({
itemDataSource: this.state.itemDataSource.cloneWithRows(items)
});
});
}
pressRow(item) {
this.itemsRef.child(item._key).remove();
}
renderRow(item) {
return (
<TouchableHighlight
onPress={() => {
this.pressRow(item);
}}
>
<View style={style.li}>
<Text style={style.liText}>{item.title}</Text>
</View>
</TouchableHighlight>
);
}
addItem() {
this.setModalVisible(true);
}
render() {
return (
<View style={style.container}>
<Modal
animationType="slide"
transparent={false}
visible={this.state.modalVisible}
onRequestClose={() => {}}
>
<View style={{ marginTop: 22 }}>
<View>
<Toolbar title="Add Item" />
<TextInput
value={this.state.text}
placeholder="Add Item"
onChangeText={value => this.setState({ text: value })}
/>
<TouchableHighlight
onPress={() => {
this.itemsRef.push({ title: this.state.text });
this.setState({ text: "" });
this.setModalVisible(!this.state.modalVisible);
}}
>
<Text>Save Item</Text>
</TouchableHighlight>
<TouchableHighlight
onPress={() => {
this.setModalVisible(!this.state.modalVisible);
}}
>
<Text>Cancel</Text>
</TouchableHighlight>
</View>
</View>
</Modal>
<Toolbar title="itemLister" />
<ListView
dataSource={this.state.itemDataSource}
renderRow={this.renderRow}
/>
<AddButton onPress={this.addItem.bind(this)} title="Add Item" />
</View>
);
}
}
AppRegistry.registerComponent("Toolbar", () => Toolbar);
AppRegistry.registerComponent("AddButton", () => AddButton);