-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathindex.ios.js
112 lines (109 loc) · 3.09 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
109
110
111
112
import React, { Component } from 'react';
import {
AppRegistry,
ActivityIndicator,
View,
Navigator,
AsyncStorage
} from 'react-native';
import Landing from './application/components/Landing';
import Register from './application/components/accounts/Register';
import RegisterConfirmation from './application/components/accounts/RegisterConfirmation';
import Login from './application/components/accounts/Login';
import Dashboard from './application/components/Dashboard';
import Loading from './application/components/shared/Loading';
import { Headers } from './application/fixtures';
import { extend } from 'underscore';
import { API, DEV } from './application/config';
import { globals } from './application/styles';
class assembliesTutorial extends Component {
constructor(){
super();
this.logout = this.logout.bind(this);
this.updateUser = this.updateUser.bind(this);
this.state = {
user : null,
ready : false,
initialRoute : 'Landing',
}
}
componentDidMount(){
this._loadLoginCredentials()
}
async _loadLoginCredentials(){
try {
let sid = await AsyncStorage.getItem('sid');
console.log('SID', sid);
if (sid){
this.fetchUser(sid);
} else {
this.ready();
}
} catch (err) {
this.ready(err);
}
}
ready(err){
this.setState({ ready: true });
}
fetchUser(sid){
fetch(`${API}/users/me`, { headers: extend(Headers, { 'Set-Cookie': `sid=${sid}`})})
.then(response => response.json())
.then(user => this.setState({ user, ready: true, initialRoute: 'Dashboard' }))
.catch(err => this.ready(err))
.done();
}
logout(){
this.nav.push({ name: 'Landing' })
}
updateUser(user){
this.setState({ user });
}
render() {
if ( ! this.state.ready ) { return <Loading /> }
return (
<Navigator
style={globals.flex}
ref={(el) => this.nav = el }
initialRoute={{ name: this.state.initialRoute }}
renderScene={(route, navigator) => {
switch(route.name){
case 'Landing':
return (
<Landing navigator={navigator}/>
);
case 'Dashboard':
return (
<Dashboard
updateUser={this.updateUser}
navigator={navigator}
logout={this.logout}
user={this.state.user}
/>
);
case 'Register':
return (
<Register navigator={navigator}/>
);
case 'RegisterConfirmation':
return (
<RegisterConfirmation
{...route}
updateUser={this.updateUser}
navigator={navigator}
/>
);
case 'Login':
return (
<Login
navigator={navigator}
updateUser={this.updateUser}
/>
);
}
}}
/>
);
}
}
AppRegistry.registerComponent('assembliesTutorial', () => assembliesTutorial);