This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
/
index.js
349 lines (316 loc) · 9.28 KB
/
index.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
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
import React from 'react'
import PropTypes from 'prop-types'
import {
Animated,
LayoutAnimation,
Modal,
Platform,
SectionList,
StyleSheet,
TouchableHighlight,
View,
ViewPropTypes
} from 'react-native'
import {Heading3, Heading5} from '../../common/PyText'
import * as Colors from '../../common/PyColors'
import Map from '../../common/PyMap'
import {titleForRoute} from '../../common/PyConstants'
import Cell from './Cell'
import Header from './Header'
import Filter from './Filter'
import TagPopover from './TagPopover'
import I18n from '../../i18n'
const SCALE_MIN_FACTOR = 0.95
const MODAL_MAP = 'MODAL_MAP'
const MODAL_FILTER = 'MODAL_FILTER'
const MODAL_TAGPOPOVER = 'MODAL_TAGPOPOVER'
export default class extends React.Component {
static propTypes = {
dates: PropTypes.arrayOf(PropTypes.string),
favoriteEvents: PropTypes.objectOf(PropTypes.bool),
filter: PropTypes.objectOf(PropTypes.bool),
isFetching: PropTypes.bool,
schedule: PropTypes.array,
selectedDate: PropTypes.string,
tagMapping: PropTypes.objectOf(PropTypes.arrayOf(PropTypes.string)),
syncCompleted: PropTypes.bool,
fetchSchedule: PropTypes.func,
goToSchedule: PropTypes.func, // for MyScheduleList only
onCellPress: PropTypes.func,
saveFavorites: PropTypes.func,
selectDate: PropTypes.func,
updateFilter: PropTypes.func,
navigation: PropTypes.object,
style: ViewPropTypes.style
}
state = {
modalVisible: false,
scaleAnim: new Animated.Value(1)
}
_modalName
_modalTagParams
_firstLaunch = true
componentWillReceiveProps (nextProps) {
const {
dates,
fetchSchedule,
selectedDate,
selectDate,
syncCompleted,
isFetching
} = nextProps
// If current date matches one of the dates, go to that page (only do once).
if (this._firstLaunch && dates) {
this._firstLaunch = false
const current = new Date()
const found = dates.find(date => {
const d = new Date(date)
return d.getDate() === current.getDate() &&
d.getMonth() === current.getMonth() &&
d.getFullYear() === current.getFullYear()
})
if (found) {
selectDate(found)
}
}
// Below are data-fetching section...
// If is fetching data now, just do noting and return
if (isFetching) {
return
}
// If the latest sync is just completed, then we shall update data.
if (syncCompleted && !this.props.syncCompleted) {
fetchSchedule(selectedDate)
return
}
// If users select different date tab, update it.
if (selectedDate !== this.props.selectedDate) {
fetchSchedule(selectedDate)
}
}
componentWillUpdate () {
if (Platform.OS === 'android') { // android shall not have animations
return
}
const {Types, Properties, create} = LayoutAnimation
const config = create(250, Types.easeInEaseOut, Properties.opacity)
LayoutAnimation.configureNext(config)
}
_setModalVisible = visible => {
this.setState({modalVisible: visible})
// android shall not have animations
if (Platform.OS === 'android' || this._modalName === MODAL_TAGPOPOVER) {
return
}
const toValue = visible ? SCALE_MIN_FACTOR : 1.0
const duration = 400
Animated.timing(this.state.scaleAnim, {toValue, duration}).start()
}
_onPressMap = () => {
this._modalName = MODAL_MAP
this._setModalVisible(true)
}
_onPressFilter = () => {
this._modalName = MODAL_FILTER
this._setModalVisible(true)
}
_onPressTag = (tag, nativeEvent) => {
this._modalName = MODAL_TAGPOPOVER
this._modalTagParams = {tag, nativeEvent}
this._setModalVisible(true)
}
_onCellPress ({eventId, location, beginTime, endTime}) {
this.props.onCellPress(eventId, location, beginTime, endTime)
}
_toggleCheck = eventId => {
const {saveFavorites, favoriteEvents} = this.props
const checked = favoriteEvents[eventId]
if (checked) {
const newFavors = {...favoriteEvents}
delete newFavors[eventId]
saveFavorites(newFavors)
return
}
saveFavorites({...favoriteEvents, [eventId]: true})
}
_renderModalContent = () => {
const backgroundColor = Colors.secondary.DARK_BLUE
switch (this._modalName) {
case MODAL_MAP:
return (
<Map
onDone={() => { this._setModalVisible(false) }}
headerProps={{isModal: true, style: {backgroundColor}}}
/>
)
case MODAL_FILTER:
return (
<Filter
filter={this.props.filter}
tags={this._getUniqueTags()}
headerProps={{isModal: true, style: {backgroundColor}}}
onDone={tags => {
this.props.updateFilter(tags)
this._setModalVisible(false)
}}
/>
)
case MODAL_TAGPOPOVER:
const {pageX: x, pageY: y} = this._modalTagParams.nativeEvent
return (
<TagPopover
tag={this._modalTagParams.tag}
point={{x, y}}
onDone={() => { this._setModalVisible(false) }}
/>
)
default:
break
}
}
_renderItem = ({item}) => {
const {eventId, type} = item
const checked = !!this.props.favoriteEvents[eventId]
// HACK to check type is `CUSTOM`
const tags = this.props.tagMapping[eventId] || [type.toUpperCase()]
const isCustom = type.toUpperCase() === 'CUSTOM'
return (
<TouchableHighlight
onPress={isCustom ? null : () => this._onCellPress(item)}
>
<View>
<Cell
{...item}
tags={tags}
checked={checked}
toggleCheck={this._toggleCheck}
onPressTag={this._onPressTag}
/>
</View>
</TouchableHighlight>
)
}
_renderSectionHeader = ({section}) => {
if (!section.data || section.data.length === 0) {
return null
}
return (
<View style={styles.sectionHeader}>
<Heading5 style={styles.sectionHeaderText}>
{section.key}
</Heading5>
</View>
)
}
_getUniqueTags = () => {
const uniqueTags = {}
Object.values(this.props.tagMapping)
.forEach(tags => {
tags.forEach(tag => { uniqueTags[tag] = true })
})
// HACK: harcode added two categories
uniqueTags['KEYNOTE'] = true
uniqueTags['CUSTOM'] = true
return Object.keys(uniqueTags)
}
render () {
const {
dates,
filter,
navigation: {state: {routeName}},
schedule,
selectedDate,
selectDate,
goToSchedule,
style
} = this.props
// Animated states
const transform = [{scale: this.state.scaleAnim}]
const opacity = this.state.scaleAnim.interpolate({
inputRange: [SCALE_MIN_FACTOR, 1],
outputRange: [0.5, 1]
})
const isEmpty = !schedule || schedule.length === 0
return (
<View style={[styles.container, style]}>
<Animated.View style={{flex: 1, transform, opacity}}>
<Header
dates={dates}
color={Colors.colorForRoute(routeName)}
filter={filter}
selectedDate={selectedDate}
selectDate={selectDate}
centerItem={titleForRoute(routeName)}
backgroundColor={Colors.secondary.DARK_BLUE}
onPressMap={this._onPressMap}
onPressFilter={this._onPressFilter}
onResetFilter={() => this.props.updateFilter({})}
/>
{!isEmpty && (
<SectionList
style={{overflow: 'hidden'}}
renderItem={this._renderItem}
renderSectionHeader={this._renderSectionHeader}
keyExtractor={item => item.eventId}
sections={schedule}
removeClippedSubviews={false} // HACK: React Native issue #13316
/>
)}
{routeName === 'MyScheduleList' && isEmpty && (
<View style={styles.addEventWrapper}>
<TouchableHighlight
style={[styles.addEventButton,
{backgroundColor: Colors.colorForRoute(routeName)}
]}
onPress={goToSchedule}
underlayColor='rgba(0, 0, 0, 0.10)'
>
<View>
<Heading3 style={styles.addEventHeading}>
{I18n.t('Add Event!')}
</Heading3>
</View>
</TouchableHighlight>
</View>
)}
</Animated.View>
<Modal
animationType={this._modalName === MODAL_TAGPOPOVER
? 'fade' : 'slide'}
transparent={this._modalName === MODAL_TAGPOPOVER}
visible={this.state.modalVisible}
onRequestClose={() => this._setModalVisible(false)}
>
{this._renderModalContent()}
</Modal>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'hsl(0, 0%, 100%)'
},
sectionHeader: {
paddingHorizontal: 20,
paddingVertical: 8,
backgroundColor: Colors.LIGHT_BACKGROUND
},
sectionHeaderText: {
color: Colors.secondary.DARK_BLUE
},
addEventWrapper: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
addEventButton: {
borderRadius: 10,
padding: 8
},
addEventHeading: {
textAlign: 'center',
overflow: 'hidden'
}
})