-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
103 lines (101 loc) · 3.45 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
import React, { useState, useEffect } from "react";
import { Ionicons } from "@expo/vector-icons";
import { AppLoading } from "expo";
import { Asset } from "expo-asset";
import * as Font from "expo-font";
import AsyncStorage from "@react-native-community/async-storage";
import { InMemoryCache } from "apollo-cache-inmemory";
import { persistCache } from "apollo-cache-persist";
import { ApolloProvider } from "react-apollo-hooks";
import { ApolloClient } from "apollo-boost";
import { HttpLink } from "apollo-link-http";
import { setContext } from "apollo-link-context";
import { onError } from "apollo-link-error";
import { ApolloLink, split } from "apollo-link";
import { WebSocketLink } from "apollo-link-ws";
import { getMainDefinition } from "apollo-utilities";
import { ThemeProvider } from "styled-components";
import styles from "./styles";
import NavController from "./componetns/NavController";
import { AuthProvider } from "./AuthContext";
export default function App() {
const [loaded, setLoaded] = useState(false);
const [client, setClient] = useState(null);
const [isLoggedIn, setIsLoggedIn] = useState(null);
const preLoad = async () => {
try {
await Font.loadAsync({
...Ionicons.font
});
await Asset.loadAsync([require("./assets/logo.png")]);
const cache = new InMemoryCache();
await persistCache({
cache,
storage: AsyncStorage
});
const token = await AsyncStorage.getItem("jwt");
const client = new ApolloClient({
cache,
request: async operation => {
const requestToken = await AsyncStorage.getItem("jwt");
return operation.setContext({
headers: {
Authorization: `Bearer ${requestToken}`
}
});
},
link: ApolloLink.from([
onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
)
);
if (networkError) console.log(`[Network error]: ${networkError}`);
}),
split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === "OperationDefinition" && definition.operation === "subscription"
);
},
new WebSocketLink({
uri: `ws://prismagram-backend.kingsky32.co.kr:4000/`,
options: {
reconnect: true
}
}),
new HttpLink({
uri: "http://prismagram-backend.kingsky32.co.kr",
headers: {
Authorization: `Bearer ${token}`
}
})
)
])
});
const isLoggedIn = await AsyncStorage.getItem("isLoggedIn");
if (!isLoggedIn || isLoggedIn === "false") {
setIsLoggedIn(false);
} else {
setIsLoggedIn(true);
}
setLoaded(true);
setClient(client);
} catch (error) {}
};
useEffect(() => {
preLoad();
}, []);
return loaded && client && isLoggedIn !== null
? <ApolloProvider client={client}>
<ThemeProvider theme={styles}>
<AuthProvider isLoggedIn={isLoggedIn}>
<NavController />
</AuthProvider>
</ThemeProvider>
</ApolloProvider>
: <AppLoading />;
}