-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathHome.js
More file actions
375 lines (331 loc) · 12.9 KB
/
Home.js
File metadata and controls
375 lines (331 loc) · 12.9 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* global __DEV__ */
import _ from 'lodash'
import React from 'react'
import Expo from 'expo'
import { compose, graphql } from 'react-apollo'
import { connect } from 'react-redux'
import {
AsyncStorage,
StyleSheet,
Text,
View,
InteractionManager,
Dimensions,
Platform,
Alert,
Image,
BackHandler
} from 'react-native'
import * as Animatable from 'react-native-animatable'
import Header from '../../components/Header'
import CircleButton from './components/CircleButton'
import CourseHeader from '../../components/CourseHeader'
import CourseProgressBar from './components/CourseProgressBar'
import Course from './scenes/Course/Course'
import MainMenu from '../../components/MainMenu/'
import * as courseActions from '../../actions/CourseActions'
import styles from '../../styles/styles'
import appStyle from '../../styles/appStyle'
import courseLogos from './helpers/courseLogos'
import coursesQuery from 'thebrain-shared/graphql/courses/courses'
import { getGraphqlForLogInWithFacebookAccessToken } from 'thebrain-shared/graphql/account/logInWithFacebookAccessToken'
import { getGraphqlForCloseCourseMutation } from 'thebrain-shared/graphql/courses/closeCourse'
import { getGraphqlForLogInWithTokenMutation } from 'thebrain-shared/graphql/account/logInWithToken'
import { getGraphqlForSelectCourseSaveTokenMutation } from 'thebrain-shared/graphql/courses/selectCourseSaveToken'
import currentUserQuery from 'thebrain-shared/graphql/account/currentUser'
import userDetailsQuery from 'thebrain-shared/graphql/userDetails/userDetails'
import WithData from '../../components/WithData'
import { mutationConnectionHandler } from '../../components/NoInternet'
import * as mainMenuActions from '../../actions/MainMenuActions'
class Home extends React.Component {
constructor (props) {
super(props)
const { height, width } = Dimensions.get('window')
this.height = height
this.width = width
this.state = {
isExitAnimationFinished: props.course.selectedCourse,
courseSelectorIsDisabled: false
}
AsyncStorage.getItem('isIntroDisabled').then((isIntroDisabled) => {
if (!isIntroDisabled) {
props.history.push('/intro')
}
})
}
componentDidMount = () => {
BackHandler.addEventListener('hardwareBackPress', this.handleBack)
}
componentWillUnmount = () => {
BackHandler.removeEventListener('hardwareBackPress', this.handleBack)
}
handleBack = () => {
if (this.props.mainMenu.visible) {
this.props.dispatch(mainMenuActions.updateMainMenuVisibility({
visible: false
}))
return true
}
BackHandler.exitApp()
return true
}
logInWithSavedData = async () => {
const deviceId = Expo.Constants.deviceId
const userId = await AsyncStorage.getItem('userId')
const accessToken = await AsyncStorage.getItem('accessToken')
const accessTokenFb = await AsyncStorage.getItem('accessTokenFb')
if (userId && accessToken) {
this.props.logInWithToken({ accessToken, userId, deviceId }).then(async () => {
const newAccessToken = this.props.currentUser.CurrentUser.currentAccessToken
await AsyncStorage.setItem('accessToken', newAccessToken)
}).catch(async () => {
await AsyncStorage.removeItem('accessToken')
await AsyncStorage.removeItem('userId')
Alert.alert('You were logged out', 'Please log in again')
})
}
if (accessTokenFb) {
await this.props.logInWithFacebookAccessToken({ accessTokenFb }).catch(async () => {
await AsyncStorage.removeItem('accessTokenFb')
Alert.alert('Facebook login expired', 'Please log in again')
})
}
}
isStillLoading (nextProps) {
return !nextProps.userDetails || nextProps.userDetails.loading || nextProps.userDetails.error ||
!nextProps.courses || nextProps.courses.loading ||
!nextProps.currentUser || nextProps.currentUser.loading
}
shouldComponentUpdate (nextProps, nextState) {
return !this.isStillLoading(nextProps)
}
componentWillReceiveProps (nextProps) {
if (this.isStillLoading(nextProps)) {
return
}
if (!nextProps.currentUser.CurrentUser) {
this.logInWithSavedData()
}
const courseId = nextProps.userDetails.UserDetails.selectedCourse
if (!courseId) {
return
}
const course = nextProps.courses.Courses.find((course) => course._id === courseId)
this.selectCourse(course)
}
getCoursesIds = () => {
return this.props.courses.Courses.map(course => course._id)
}
getOtherCoursesIds = (selectedCourseId) => {
return this.getCoursesIds().filter(courseId => courseId !== selectedCourseId)
}
animateCourseSelectorsFadeOut = (selectedCourseId) => {
this.refs.courseSelectorTitle.fadeOut(500)
this.getOtherCoursesIds(selectedCourseId).forEach((courseId) => {
this.refs[`${courseId}courseSelector`].fadeOut(500)
})
this.getCoursesIds().forEach((courseId) => {
this.refs[`${courseId}courseSelectorText`].fadeOut(500)
})
InteractionManager.runAfterInteractions(() => {
this.setState({ isExitAnimationFinished: true })
})
}
animateCourseSelector = (selectedCourseId) => {
if (!this.refs[`${selectedCourseId}courseSelectorContainer`]) {
return
}
if (!__DEV__) {
this.refs[`${selectedCourseId}courseSelectorContainer`].measure((fx, fy, width, height, pageXOffset, pageYOffset) => {
const scale = 0.75
const desiredBottomYOffset = 10
const newSizeY = height * scale
const desiredElementTopYOffset = desiredBottomYOffset + newSizeY
let iPhoneOffset = 0
if (Platform.OS === 'ios' && this.height === 568) iPhoneOffset = 20
if (Platform.OS === 'ios' && this.height === 667) iPhoneOffset = 31
if (Platform.OS === 'ios' && this.height === 736) iPhoneOffset = 38.5
if (Platform.OS === 'ios' && this.height === 812) iPhoneOffset = 98.5
const elementHeightChangeAfterScaling = (height - newSizeY) / 2
const translateYValue = this.height - pageYOffset - desiredElementTopYOffset - elementHeightChangeAfterScaling - iPhoneOffset
const newSizeX = width * scale
const centeredLeftXOffset = (this.width - newSizeX) / 2
const elementWidthChangeAfterScaling = (width - newSizeX) / 2
const translateXValue = centeredLeftXOffset - pageXOffset - elementWidthChangeAfterScaling
const courseLogoIsRendered = width && height
if (Platform.OS === 'ios' && courseLogoIsRendered) {
this.refs[`${selectedCourseId}courseSelector`].transitionTo({transform: [{translateX: translateXValue}, {translateY: translateYValue}, {scale}]}, 2000)
} else {
this.refs[`${selectedCourseId}courseSelector`].bounceOut(1000)
}
this.animateCourseSelectorsFadeOut(selectedCourseId)
})
}
}
selectCourse = async (course) => {
if (!this.props.course.selectedCourse) {
const deviceId = Expo.Constants.deviceId
this.props.dispatch(courseActions.select(course))
await mutationConnectionHandler(this.props.history, () => {
this.props.selectCourseSaveToken({ courseId: course._id, deviceId }).then(async () => {
if (this.props.currentUser.CurrentUser) {
if (__DEV__) {
this.setState({isExitAnimationFinished: true})
}
return
} else {
await this.props.currentUser.refetch()
}
const accessToken = this.props.currentUser.CurrentUser.currentAccessToken
const userId = this.props.currentUser.CurrentUser._id
if (userId && accessToken) {
await AsyncStorage.setItem('accessToken', accessToken)
await AsyncStorage.setItem('userId', userId)
if (__DEV__) {
this.setState({isExitAnimationFinished: true})
}
}
})
})
this.animateCourseSelector(course._id)
}
}
disableCourseSelector = () => {
this.setState({ courseSelectorIsDisabled: true })
}
enableCourseSelector = () => {
this.setState({ courseSelectorIsDisabled: false })
}
logoutAction = () => {
this.closeMenu()
this.setState({ isExitAnimationFinished: false })
this.enableCourseSelector()
}
closeCourse = async () => {
await mutationConnectionHandler(this.props.history, async () => {
await this.props.closeCourse()
this.props.dispatch(courseActions.close())
this.setState({ isExitAnimationFinished: false })
this.closeMenu()
this.enableCourseSelector()
})
}
closeMenu = () => {
this.props.dispatch(mainMenuActions.updateMainMenuVisibility({
visible: false
}))
}
render () {
const { isExitAnimationFinished } = this.state
const { course } = this.props
const courseColor = _.get(course, 'selectedCourse.color')
return (
<View style={{
position: 'relative',
width: '100%',
height: '100%',
backgroundColor: courseColor
}}>
{!isExitAnimationFinished &&
<Header withShadow dynamic hide={this.props.course.selectedCourse} />}
{this.props.course.selectedCourse
? <CourseHeader isExitAnimationFinished={isExitAnimationFinished} style={{ position: 'absolute' }}
closeCourse={this.closeCourse}>
<CourseProgressBar />
</CourseHeader> : <View style={style.courseHeader} />}
{!isExitAnimationFinished && <View style={{
flexGrow: 1,
justifyContent: 'center',
alignItems: 'center'
}}>
<Animatable.View ref='courseSelectorTitle'>
<Text
style={[styles.textDefault, {
marginBottom: 5,
fontSize: 24,
fontFamily: 'Kalam-Regular',
marginTop: '10%'
}]}>
Choose a course:
</Text>
</Animatable.View>
{!this.props.courses.loading &&
<View style={{ flexDirection: 'row', flex: 1, flexWrap: 'wrap', justifyContent: 'center' }}>
{this.props.courses.Courses.map(course => {
const courseLogo = courseLogos[course.name]
const onPressAction = course.isDisabled ? () => {} : () => { this.selectCourse(course) }
const courseSelectorDisabler = course.isDisabled ? () => {} : this.disableCourseSelector
const courseColor = course.isDisabled ? 'transparent' : course.color
const textOpacity = course.isDisabled ? 0.5 : 1
return (
<Animatable.View key={course._id} style={{ elevation: 100, width: '45%' }}
ref={`${course._id}courseSelector`}>
<View ref={`${course._id}courseSelectorContainer`}
onLayout={() => {}} >
<CircleButton
courseName={course.name}
color={courseColor}
onPress={onPressAction}
disableCourseSelector={courseSelectorDisabler}
courseSelectorIsDisabled={this.state.courseSelectorIsDisabled}
isDisabled={course.isDisabled}
>
<Image
source={courseLogo.file}
style={{ width: courseLogo.width, height: courseLogo.height, alignSelf: 'center' }}
/>
</CircleButton>
</View>
<View style={{
marginBottom: 20
}}>
<Animatable.Text style={[style.courseTitle, { opacity: textOpacity }]}
ref={`${course._id}courseSelectorText`}
>
{course.name}
</Animatable.Text>
</View>
</Animatable.View>
)
})}
</View>
}
</View>}
{isExitAnimationFinished && <Course closeCourse={this.closeCourse} />}
{this.props.mainMenu.visible && <MainMenu closeCourse={this.closeCourse} logoutAction={this.logoutAction} />}
</View>
)
}
}
export default compose(
connect(state => state),
getGraphqlForLogInWithTokenMutation(graphql),
getGraphqlForLogInWithFacebookAccessToken(graphql),
getGraphqlForSelectCourseSaveTokenMutation(graphql),
getGraphqlForCloseCourseMutation(graphql),
graphql(currentUserQuery, { name: 'currentUser' }),
graphql(coursesQuery, { name: 'courses' }),
graphql(userDetailsQuery, { name: 'userDetails' })
)(WithData(Home, ['currentUser', 'courses', 'userDetails']))
const style = StyleSheet.create({
courseTitle: {
color: 'white',
textAlign: 'center',
fontSize: 16
},
smallCircle: {
position: 'absolute',
top: 0,
left: 58,
transform: [{ translateX: -10 }, { translateY: -10 }],
backgroundColor: 'white',
width: 20,
height: 20,
borderRadius: 10
},
courseHeader: {
margin: 0,
height: appStyle.header.height,
width: '100%'
}
})