-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
117 lines (102 loc) · 3.09 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
/**
* React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {AppNavigator} from './src/navigation/Navigator';
import {
ApolloClient,
InMemoryCache,
ApolloProvider,
ApolloLink,
concat,
createHttpLink,
} from '@apollo/client';
import {onError} from '@apollo/client/link/error';
import {Provider} from 'react-redux';
import {persistor, store} from './src/redux/store';
import {PersistGate} from 'redux-persist/integration/react';
import messaging from '@react-native-firebase/messaging';
const API_URL = 'https://velena-graphql-api.herokuapp.com/graphql';
const httpLink = new createHttpLink({uri: API_URL});
const errorLink = 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}`);
}
});
const loggerLink = new ApolloLink((operation, forward) => {
console.log(`GraphQL Request: ${operation.operationName}`);
operation.setContext({start: new Date()});
return forward(operation).map(response => {
const responseTime = new Date() - operation.getContext().start;
console.log(`GraphQL Response took: ${responseTime}`);
if (responseTime >= 10000) {
//show poor network notification
}
return response;
});
});
const authMiddleware = new ApolloLink(async (operation, forward) => {
const token = store.getState().account.sessionToken;
operation.setContext(({headers = {}}) => ({
headers: {
...headers,
authorization: token,
},
}));
return forward(operation);
});
export const client = new ApolloClient({
link: concat(authMiddleware, httpLink, loggerLink, errorLink),
cache: new InMemoryCache(),
});
export const navigationRef = React.createRef();
class App extends React.Component {
componentDidMount() {
this.requestUserPermission();
}
requestUserPermission = async () => {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
this.getFcmToken(); //<---- Add this
console.log('Authorization status:', authStatus);
}
};
getFcmToken = async () => {
const fcmToken = await messaging().getToken();
if (fcmToken) {
console.log(fcmToken);
console.log('Your Firebase Token is:', fcmToken);
} else {
console.log('Failed', 'No token received');
}
};
render() {
return (
<ApolloProvider client={client}>
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<NavigationContainer ref={navigationRef}>
<AppNavigator />
</NavigationContainer>
</PersistGate>
</Provider>
</ApolloProvider>
);
}
}
export default App;