-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
227 lines (187 loc) · 6.42 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import React from 'react';
import {
Linking
} from 'expo';
import {
SafeAreaView,
Dimensions
} from 'react-native';
import Spinner from 'react-native-loading-spinner-overlay';
import {
PersistGate
} from 'redux-persist/integration/react';
import {
connect as connectRedux,
Provider
} from 'react-redux';
import {
auth,
createUserProfileDocument,
firestore
} from './firebase/firebase.utils';
import Navigator from './components/navigator/navigator.component';
import {
userActionSetCurrentUser
} from './redux/user/user.actions';
import {
userSelectorGetCurrentUser
} from "./redux/user/user.selectors";
import {
selectIsLoading
} from "./redux/blog/blog.selectors";
import {
blogActionSetIsLoading,
blogActionViewBlog
} from './redux/blog/blog.actions';
import {
stylerActionSetDimensions,
} from './redux/styler/styler.actions';
import {
getGlobalStackNavigationContext
} from './components/navigator/navigator.exports';
import {
persistor,
store
} from './redux/store';
class App extends React.Component {
unsubscribeFromAuth = null;
handleDeepLink = async ( args ) => {
const { url } = args;
if ( this.deepLinkHandlingInProgress === true ) {
return;
}
let { path, queryParams } = Linking.parse( url );
if ( path === null || path === undefined ) {
return false;
}
if ( ( queryParams === null ) ||
( queryParams === undefined ) ||
!( queryParams.hasOwnProperty( 'id' ) ) ) {
return false;
}
const {
notifyIsLoading,
viewBlogItem
} = this.props;
notifyIsLoading( true );
try {
const snapshot = firestore.collection( "blogs" )
.doc( queryParams.id );
await snapshot.get()
.then( async ( doc ) => {
if ( doc.exists ) {
const dres = doc.data();
let author = { displayName: "Unknown" };
if ( dres.author ) {
let res = await dres.author.get();
author = res.data();
} else {
author = { displayName: "Anonymous Author" };
}
const blog = { ...dres, author: author };
viewBlogItem( blog );
getGlobalStackNavigationContext().navigate( 'BlogViewer' );
setTimeout( () => notifyIsLoading( false ), 1000 );
} else {
notifyIsLoading( false );
alert( "The post you are trying to access is invalid or might have been deleted!" );
}
this.deepLinkHandlingInProgress = false;
} );
} catch ( error ) {
notifyIsLoading( false );
alert( error.message );
this.deepLinkHandlingInProgress = false;
}
};
constructor( props ) {
super( props );
this.deepLinkHandlingInProgress = false;
}
handleDimensionChange = () => {
const { setDimensions } = this.props;
setDimensions( {
width: Dimensions.get( "window" ).width,
height: Dimensions.get( "window" ).height,
} );
};
componentDidMount () {
const {
setCurrentUser,
notifyIsLoading
} = this.props;
Linking.getInitialURL()
.then( url => {
this.handleDeepLink( { url: url } );
Linking.addEventListener( 'url',
( event ) => this.handleDeepLink( event ) );
} );
Dimensions.addEventListener( "change", this.handleDimensionChange );
notifyIsLoading( true );
this.applicationIsLoading = true;
this.unsubscribeFromAuth = auth.onAuthStateChanged(
async ( userAuth ) => {
if ( userAuth ) {
try {
const userRef = await createUserProfileDocument( userAuth );
userRef.onSnapshot( snapShot => {
setCurrentUser( {
id: snapShot.id,
...snapShot.data()
}
);
} );
} catch ( error ) {
setCurrentUser( null );
alert( error.message );
}
setTimeout( () => notifyIsLoading( false ), 2000 );
} else {
setCurrentUser( null );
}
if ( this.applicationIsLoading ) {
setTimeout( () => notifyIsLoading( false ), 2000 );
this.applicationIsLoading = false;
}
}
);
}
componentWillUnmount () {
Linking.removeEventListener( 'url', this.handleDeepLink );
this.unsubscribeFromAuth();
this.deepLinkHandlingInProgress = false;
Dimensions.removeEventListener( "change", this.handleDimensionChange );
}
render () {
const { isLoading } = this.props;
return (
<SafeAreaView style={ { flex: 1 } }>
<Spinner
visible={ isLoading > 0 ? true : false }
textContent={ 'Hold on! Loading...' }
textStyle={ { color: '#F9F9F9', fontWeight: 'normal', marginTop: -30 } }
/>
<Navigator />
</SafeAreaView>
);
}
};
const mapStateToProps = ( state ) => ( {
currentUser: userSelectorGetCurrentUser( state ),
isLoading: selectIsLoading( state )
} );
const mapDispatchToProps = dispatch => ( {
setCurrentUser: ( item ) => dispatch( userActionSetCurrentUser( item ) ),
viewBlogItem: ( item ) => dispatch( blogActionViewBlog( item ) ),
notifyIsLoading: ( item ) => dispatch( blogActionSetIsLoading( item ) ),
setDimensions: ( item ) => dispatch( stylerActionSetDimensions( item ) )
} );
const ReduxApp = connectRedux( mapStateToProps, mapDispatchToProps )( App );
const ReturnableApp = () => (
<Provider store={ store } >
<PersistGate persistor={ persistor }>
<ReduxApp />
</PersistGate>
</Provider>
);
export default ReturnableApp;