-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
89 lines (82 loc) · 2.4 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
import React from "react";
import { AsyncStorage } from 'react-native'
import base64 from 'react-native-base64'
import { AppLoading } from 'expo'
import { API_URL } from './config'
import { NavigationContainer } from '@react-navigation/native'
import { createStackNavigator } from '@react-navigation/stack'
import Home from './screens/home'
import Login from './screens/login'
const Stack = createStackNavigator()
export default class App extends React.Component {
constructor(props) {
super(props)
this.state = {
isReady: false,
isAuthorized: false,
startData: {}
}
}
render() {
if (!this.state.isReady) {
return (
<AppLoading
startAsync = {this._getStartFetch}
onFinish = {() => this.setState({isReady: true})}
onError ={console.warn}
/>
)
}
return (
<NavigationContainer>
<Stack.Navigator>
{this.state.isAuthorized ? (
<Stack.Screen
name="Home"
component={Home}
options={{title: 'Spend Control'}}
initialParams={{startData: this.state.startData}}
/>
) : (
<>
<Stack.Screen name="Login" component={Login} />
<Stack.Screen
name="Home"
component={Home}
options={{title: 'Spend Control'}}
/>
</>
)}
</Stack.Navigator>
</NavigationContainer>
)
}
async _getStartFetch() {
try {
let login = await AsyncStorage.getItem('login')
let password = await AsyncStorage.getItem('password')
let auth = 'Basic ' + base64.encode(login + ':' + password)
let tokenUrl = API_URL + 'tokens'
let response = await fetch(tokenUrl, {
method: 'POST',
headers: {
Authorization: auth
}
})
let responseJson = await response.json()
let tokenAuth = 'Bearer ' + responseJson.token
let startDataUrl = API_URL + 'spends/move/car/start'
let startDataResponse = await fetch(startDataUrl, {
method: 'GET',
headers: {
Authorization: tokenAuth
}
})
let startDataResponseJson = await startDataResponse.json()
startDataResponseJson.tokenAuth = tokenAuth
this.setState({isAuthorized: true})
this.setState({startData: startDataResponseJson})
}
catch {}
}
}