|
1 | 1 | // @flow |
2 | | -/** |
3 | | - * All About Olaf |
4 | | - * Building Hours view. This component loads data from either GitHub or |
5 | | - * the local copy as a fallback, and renders the list of buildings. |
6 | | - */ |
7 | | - |
8 | | -import React from 'react' |
9 | | -import {NoticeView} from '../components/notice' |
10 | | -import {tracker} from '../../analytics' |
11 | | -import bugsnag from '../../bugsnag' |
12 | | -import {BuildingHoursList} from './list' |
13 | | - |
14 | | -import type momentT from 'moment' |
15 | | -import type {TopLevelViewPropsType} from '../types' |
16 | | -import type {BuildingType} from './types' |
17 | | -import {data as fallbackBuildingHours} from '../../../docs/building-hours' |
18 | | -import groupBy from 'lodash/groupBy' |
19 | | - |
20 | | -import moment from 'moment-timezone' |
21 | | -const CENTRAL_TZ = 'America/Winnipeg' |
22 | 2 |
|
| 3 | +export {BuildingHoursView} from './stateful-list' |
23 | 4 | export {BuildingHoursDetailView} from './detail' |
24 | | - |
25 | | -const githubBaseUrl = 'https://stodevx.github.io/AAO-React-Native' |
26 | | - |
27 | | -const groupBuildings = (buildings: BuildingType[]) => |
28 | | - groupBy(buildings, b => b.category || 'Other') |
29 | | - |
30 | | -export class BuildingHoursView extends React.Component { |
31 | | - static navigationOptions = { |
32 | | - title: 'Building Hours', |
33 | | - headerBackTitle: 'Hours', |
34 | | - } |
35 | | - |
36 | | - state: { |
37 | | - error: ?Error, |
38 | | - loading: boolean, |
39 | | - now: momentT, |
40 | | - buildings: {[key: string]: BuildingType[]}, |
41 | | - intervalId: number, |
42 | | - } = { |
43 | | - error: null, |
44 | | - loading: true, |
45 | | - // now: moment.tz('Wed 7:25pm', 'ddd h:mma', null, CENTRAL_TZ), |
46 | | - now: moment.tz(CENTRAL_TZ), |
47 | | - buildings: groupBuildings(fallbackBuildingHours), |
48 | | - intervalId: 0, |
49 | | - } |
50 | | - |
51 | | - componentWillMount() { |
52 | | - this.fetchData() |
53 | | - |
54 | | - // This updates the screen every ten seconds, so that the building |
55 | | - // info statuses are updated without needing to leave and come back. |
56 | | - this.setState({intervalId: setInterval(this.updateTime, 10000)}) |
57 | | - } |
58 | | - |
59 | | - componentWillUnmount() { |
60 | | - clearTimeout(this.state.intervalId) |
61 | | - } |
62 | | - |
63 | | - props: TopLevelViewPropsType |
64 | | - |
65 | | - updateTime = () => { |
66 | | - this.setState({now: moment.tz(CENTRAL_TZ)}) |
67 | | - } |
68 | | - |
69 | | - fetchData = async () => { |
70 | | - this.setState({loading: true}) |
71 | | - |
72 | | - let buildings: BuildingType[] = [] |
73 | | - try { |
74 | | - let container = await fetchJson(`${githubBaseUrl}/building-hours.json`) |
75 | | - let data = container.data |
76 | | - buildings = data |
77 | | - } catch (err) { |
78 | | - tracker.trackException(err.message) |
79 | | - bugsnag.notify(err) |
80 | | - console.warn(err) |
81 | | - buildings = fallbackBuildingHours |
82 | | - } |
83 | | - |
84 | | - if (process.env.NODE_ENV === 'development') { |
85 | | - buildings = fallbackBuildingHours |
86 | | - } |
87 | | - |
88 | | - this.setState({ |
89 | | - loading: false, |
90 | | - buildings: groupBuildings(buildings), |
91 | | - now: moment.tz(CENTRAL_TZ), |
92 | | - }) |
93 | | - } |
94 | | - |
95 | | - render() { |
96 | | - if (this.state.error) { |
97 | | - return <NoticeView text={'Error: ' + this.state.error.message} /> |
98 | | - } |
99 | | - |
100 | | - return ( |
101 | | - <BuildingHoursList |
102 | | - navigation={this.props.navigation} |
103 | | - buildings={this.state.buildings} |
104 | | - now={this.state.now} |
105 | | - onRefresh={this.fetchData} |
106 | | - loading={this.state.loading} |
107 | | - /> |
108 | | - ) |
109 | | - } |
110 | | -} |
0 commit comments