-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
165 lines (144 loc) · 5.22 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
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
import { StatusBar } from 'expo-status-bar';
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { NativeRouter, Route, Switch, Redirect } from "react-router-native";
import AsyncStorage from '@react-native-async-storage/async-storage';
import LoginPage from './components/LoginPage';
import ChooseOption from './components/ChooseOption';
import MyCalendar from './components/MyCalendar';
import Logout from './components/Logout';
import FbLogin from './components/FbLogin'
//import com.facebook.FacebookSdk;
import { Settings } from 'react-native-fbsdk-next';
// <LoginPage register={this.register} login={this.login}/> <MyCalendar/> <StatusBar style="auto" />
class App extends Component{
constructor(props){
super(props);
this.state = {
list:[],
isLogged:false,
token:""
}
}
callStorage = async () => {
if(await AsyncStorage.getItem('state')){
try {
let state = await AsyncStorage.getItem('state');
this.setState(JSON.parse(state), () => {
if(this.state.isLogged){
}
})
} catch (err) {
console.log(err)
}
}
}
componentDidMount() {
this.callStorage()
}
saveToStorage = async () => {
try{
await AsyncStorage.setItem("state", JSON.stringify(this.state))
} catch (err){
console.log(err)
}
}
clearState = () => {
this.setState({
list:[],
isLogged:false,
token:""
})
}
register = user => {
let request = {
method: "POST",
mode:'cors',
headers: {"Content-type":"application/json"},
body: JSON.stringify(user)
}
fetch("https://backendandroid.herokuapp.com/register", request).then(response => {
if(response.ok){
alert("Register Success!")
}else {
console.log("Server responded with a status: ",response.status)
}
}).catch(error => {
console.log("There was an error. Error: ", error)
})
}
login = user => {
let request = {
method:"POST",
mode:'cors',
headers: {"Content-type":"application/json"},
body:JSON.stringify(user)
}
fetch("https://backendandroid.herokuapp.com/login", request).then(response => {
if(response.ok){
response.json().then(data => {
this.setState({
isLogged:true,
token:data.token
}, () => {
this.saveToStorage();
})
}).catch(error => {
console.log("Failed to parse JSON, Reason:", error)
})
}else {
console.log("Server responded with a status:", response.status)
}
}).catch(error => {
console.log("There was an error. Error:", error)
})
}
logout = () => {
let request = {
method: "POST",
mode: "cors",
headers: {"Content-type":"application/json",
"token": this.state.token}
}
fetch("https://backendandroid.herokuapp.com/logout", request).then(response => {
if(response.ok){
this.clearState();
} else {
this.clearState();
console.log("Server responded with a status:", response.status)
}
}).catch(error => {
console.log("There was an error:", error);
this.clearState();
})
}
render(){
return (
<NativeRouter>
<View style={styles.container}>
{/* <FbLogin/> */}
<Logout isLogged={this.state.isLogged} logout={this.logout}/>
<Switch>
<Route exact path="/" render={()=> this.state.isLogged ?
(<Redirect to="/calendar"/>) :
( <LoginPage register={this.register} login={this.login}/>)} />
<Route path="/calendar" render={()=> this.state.isLogged ?
( <MyCalendar/>) :
(<Redirect to="/"/>)
} />
<StatusBar style="auto" />
</Switch>
</View>
</NativeRouter>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
alignItems: 'center',
justifyContent: 'center',
},
});
export default App;