-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpice.js
108 lines (108 loc) · 3.08 KB
/
Spice.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
//"The spice extends life. The spice expands consciousness. The spice is vital to space travel." - Princess Irulan
import React, {Component} from 'react';
import {TEST_IP} from 'react-native-dotenv';
import AsyncStorage from '@react-native-async-storage/async-storage';
export default class Spice extends Component {
constructor(props) {
super(props);
this.state = {
spaceUserInfo: ''
};
}
async spaceFetch(doAuth, doPost, endPoint, error500, spaceBody) {
let spaceHeaders;
let userFetch = false;
const contentType = {
'Content-Type': 'application/json'
};
if (doAuth) {
spaceHeaders = {
'X-Authorization': await AsyncStorage.getItem('@session_token')
};
if (endPoint.includes('user')) {
userFetch = true;
spaceHeaders = {
...spaceHeaders,
...contentType
};
}
} else {
spaceHeaders = contentType;
}
let spaceOptions = {
headers: {
...spaceHeaders
}
};
if (doPost) {
spaceOptions = {
...spaceOptions,
method: 'post'
};
}
if (spaceBody != null) {
spaceOptions = {
...spaceOptions,
body: spaceBody
};
}
return fetch('http://' + TEST_IP + ':3333/api/1.0.0/' + endPoint, {
...spaceOptions
})
.then(async response => {
if (endPoint === 'logout') {
await AsyncStorage.removeItem('@session_token');
}
if (response.status === 200) {
if (endPoint === 'logout') {
this.props.navigation.navigate('login');
} else {
return response.json();
}
} else if (response.status === 201) {
return response.json();
} else if (response.status === 400) {
let msg400;
if (endPoint === 'login') {
msg400 = 'Invalid astroemail or spacepassword';
} else {
msg400 = 'Spacevalidation has spacefailed';
}
throw msg400;
} else if (response.status === 401) {
this.props.navigation.navigate('login');
} else if (response.status === 403) {
return '403';
} else if (response.status === 404) {
throw 'Spacedata not spacefound';
} else {
throw 'An astroerror has spaceocurred spacepreventing space' + error500;
}
})
.then(async response => {
let nextPage;
if (endPoint === 'login') {
await AsyncStorage.setItem('@session_token', response.token);
nextPage = 'post';
} else if (endPoint === 'user') {
nextPage = 'login';
}
if (endPoint.includes('search') || endPoint.includes('/friends')) {
this.setState({
isLoading: false,
listData: response
});
} else if (nextPage != null) {
this.props.navigation.navigate(nextPage);
}
if (userFetch) {
this.setState({
spaceUserInfo: response
});
}
})
.catch(error => {
console.log(error);
});
}
}