forked from fullstackreact/react-native-firestack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ios.js
More file actions
304 lines (277 loc) · 7.36 KB
/
Copy pathindex.ios.js
File metadata and controls
304 lines (277 loc) · 7.36 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TextInput,
ActivityIndicatorIOS,
Image,
TouchableHighlight,
Dimensions,
ListView,
} from 'react-native';
const windowSize = Dimensions.get('window');
import Firestack from 'react-native-firestack';
import config from './config';
const server = new Firestack(config.firebase);
server.configure();
class Login extends Component {
constructor(props) {
super(props);
this.state = {
username: '',
password: ''
}
}
signIn(e) {
this.props.onSignIn(this.state)
}
render() {
return (
<View style={styles.container}>
<Image style={styles.bg} source={{uri: 'http://i.imgur.com/xlQ56UK.jpg'}} />
<View style={styles.header}>
<Image style={styles.mark} source={{uri: 'http://i.imgur.com/da4G0Io.png'}} />
</View>
<View style={styles.inputs}>
<View style={styles.inputContainer}>
<Image style={styles.inputUsername} source={{uri: 'http://i.imgur.com/iVVVMRX.png'}}/>
<TextInput
style={[styles.input, styles.whiteFont]}
placeholder="Email"
placeholderTextColor="#FFF"
onChangeText={(t) => this.setState({username: t})}
value={this.state.username}
/>
</View>
<View style={styles.inputContainer}>
<Image style={styles.inputPassword} source={{uri: 'http://i.imgur.com/ON58SIG.png'}}/>
<TextInput
password={true}
style={[styles.input, styles.whiteFont]}
placeholder="Pasword"
placeholderTextColor="#FFF"
onChangeText={(t) => this.setState({password: t})}
value={this.state.password}
/>
</View>
<View style={styles.forgotContainer}>
<Text style={styles.greyFont}>Forgot Password</Text>
</View>
</View>
<View style={styles.signin}>
<TouchableHighlight onPress={this.signIn.bind(this)}>
<Text style={styles.whiteFont}>Sign In</Text>
</TouchableHighlight>
</View>
<View style={styles.signup}>
<Text style={styles.greyFont}>Don't have an account?<Text style={styles.whiteFont}> Sign Up</Text></Text>
</View>
</View>
);
}
}
class AuthorizedPage extends Component {
constructor(props) {
super(props);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
vals: [],
dataSource: ds,
}
}
onLogout(e) {
server.signOut()
.then(() => {
console.log('signed out...');
})
}
componentDidMount() {
const ref = server.database.ref('things');
ref.orderByChild('timestamp')
.on('value', snapshot => {
if (snapshot.val()) {
const val = snapshot.val();
const vals = Object.keys(val).map(k => val[k]);
const dataSource = this.state.dataSource.cloneWithRows(vals);
this.setState({dataSource})
}
});
ref.push({
timestamp: server.ServerValue.TIMESTAMP,
name: 'mounted',
});
this.ref = ref;
}
componentWillUnmount() {
const ref = this.ref || server.database.ref('things');
ref.push({
timestamp: server.ServerValue.TIMESTAMP,
name: 'unmounted'
});
ref.orderByChild('timestamp').off('value');
}
render() {
const {currentUser} = this.props;
return (
<View style={styles.container}>
<Text>Welcome back {currentUser.displayName}</Text>
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{rowData.timestamp}</Text>}
/>
<TouchableHighlight onPress={this.onLogout.bind(this)}>
<Text>Logout</Text>
</TouchableHighlight>
</View>
)
}
}
class Example extends Component {
constructor(props) {
super(props);
this.state = {
loading: true,
currentUser: null
}
}
componentWillMount() {
console.log('listen for auth, yo');
server.listenForAuth((evt) => {
console.log('listen for auth', evt);
if (evt.error) {
this.setState({currentUser: null, loading: false});
} else {
this.setState({currentUser: evt.user, loading: false});
}
});
}
componentWillUnmount() {
server.unlistenForAuth();
}
componentDidMount() {
console.log('new props', this.props);
server.getCurrentUser()
.then(u => {
console.log('got current user', u)
this.setState({
loading: false,
currentUser: u
})
})
.catch(e => {
console.info('no current user', e)
this.setState({
loading: false,
currentUser: null
})
})
server.logEventWithName('launched', {});
}
onSignIn({username, password}) {
console.log('onSignIn called', username, password);
server.signInWithEmail(username, password)
.then(u => this.setState({currentUser: u, loading: false}))
.catch(e => {
console.info('error ->', e);
})
}
render() {
const {loading, currentUser} = this.state;
if (loading) {
return (<View style={styles.container}>
<Text>Loading...</Text>
</View>)
}
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
{currentUser ?
<AuthorizedPage currentUser={currentUser} {...this.props} /> :
<Login onSignIn={this.onSignIn.bind(this)} />}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flexDirection: 'column',
flex: 1,
backgroundColor: 'transparent'
},
bg: {
position: 'absolute',
left: 0,
top: 0,
width: windowSize.width,
height: windowSize.height
},
header: {
justifyContent: 'center',
alignItems: 'center',
flex: .5,
backgroundColor: 'transparent'
},
mark: {
width: 150,
height: 150
},
signin: {
backgroundColor: '#FF3366',
padding: 20,
alignItems: 'center'
},
signup: {
justifyContent: 'center',
alignItems: 'center',
flex: .15
},
inputs: {
marginTop: 10,
marginBottom: 10,
flex: .25
},
inputPassword: {
marginLeft: 15,
width: 20,
height: 21
},
inputUsername: {
marginLeft: 15,
width: 20,
height: 20
},
inputContainer: {
padding: 10,
borderWidth: 1,
borderBottomColor: '#CCC',
borderColor: 'transparent'
},
input: {
position: 'absolute',
left: 61,
top: 12,
right: 0,
height: 20,
fontSize: 14
},
forgotContainer: {
alignItems: 'flex-end',
padding: 15,
},
greyFont: {
color: '#D8D8D8'
},
whiteFont: {
color: '#FFF'
}
})
AppRegistry.registerComponent('Example', () => Example);