-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.android.js
147 lines (144 loc) · 3.5 KB
/
index.android.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
139
140
141
142
143
144
145
146
147
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
var React = require('react-native');
var {
AppRegistry,
AsyncStorage,
Navigator,
StyleSheet,
Text,
View,
} = React;
var AuthForm = require('./auth').Form;
var Toolbar = require('./toolbar');
var Games = require('./games');
var Game = require('./game');
class BrdgmeMobile extends React.Component {
constructor(props) {
super(props)
this.state = {
email: null,
token: null,
loading: true,
};
AsyncStorage.multiGet(['email', 'token'])
.then((values) => {
var newState = {
loading: false,
};
values.forEach((pair) => {
newState[pair[0]] = pair[1];
});
this.setState(newState);
});
}
onAuthenticate(email, token) {
this.setState({
email: email,
token: token,
});
AsyncStorage.multiSet([
['email', email],
['token', token],
]);
}
logout() {
this.setState({
token: null,
});
AsyncStorage.removeItem('token');
}
fetch(input, init) {
if (!init) {
init = {};
}
if (!init.headers) {
init.headers = {};
}
if (!init.headers.Authorization) {
init.headers.Authorization = 'token ' + this.state.token;
}
return fetch(input, init).then((response) => {
if (response.status === 401) {
this.logout();
throw('logged out');
}
return response;
});
}
render() {
return (
<View
style={{
flex: 1,
}}
>
{ this.state.loading || !this.state.token ?
<View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center',
}}
>
<Text
style={{
fontSize: 50,
textAlign: 'center',
marginBottom: 5,
}}
onPress={() => this.setState({showConfirmation: true})}
>
brdg.me
</Text>
<Text
style={{
textAlign: 'center',
marginBottom: 50,
}}
>
board games with friends by email
</Text>
{ !this.state.loading ?
<View
style={{
marginLeft: 40,
marginRight: 40,
}}
>
<AuthForm
initialEmail={this.state.email}
onAuthenticate={(email, token) => this.onAuthenticate(email, token)}
/>
</View> : null
}
</View> :
<Navigator
initialRoute={{name: 'Games'}}
renderScene={(route, navigator) =>
<View style={{flex: 1}}>
{ route.name === 'Games' ?
<Games
fetch={(input, init) => this.fetch(input, init)}
logout={() => this.logout()}
navigator={navigator}
/> : null }
{ route.name === 'Game' ?
<Game
id={route.id}
fetch={(input, init) => this.fetch(input, init)}
logout={() => this.logout()}
navigator={navigator}
/> : null}
</View>
}
/>
}
</View>
);
}
}
AppRegistry.registerComponent('BrdgmeMobile', () => BrdgmeMobile);