Skip to content

Commit 2cb3911

Browse files
committed
Add elapsed refresh to building hours
1 parent a74449f commit 2cb3911

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

source/views/building-hours/stateful-list.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as defaultData from '../../../docs/building-hours'
1616
import {reportNetworkProblem} from '../../lib/report-network-problem'
1717
import toPairs from 'lodash/toPairs'
1818
import groupBy from 'lodash/groupBy'
19+
import delay from 'delay'
1920

2021
import {CENTRAL_TZ} from './lib'
2122
const githubBaseUrl =
@@ -65,8 +66,22 @@ export class BuildingHoursView extends React.Component {
6566
this.setState(() => ({now: moment.tz(CENTRAL_TZ)}))
6667
}
6768

68-
fetchData = async () => {
69+
refresh = async () => {
70+
let start = Date.now()
6971
this.setState(() => ({loading: true}))
72+
73+
await this.fetchData()
74+
75+
// wait 0.5 seconds – if we let it go at normal speed, it feels broken.
76+
let elapsed = Date.now() - start
77+
if (elapsed < 500) {
78+
await delay(500 - elapsed)
79+
}
80+
81+
this.setState(() => ({loading: false}))
82+
}
83+
84+
fetchData = async () => {
7085
let {data: buildings} = await fetchJson(githubBaseUrl).catch(err => {
7186
reportNetworkProblem(err)
7287
return defaultData
@@ -75,7 +90,6 @@ export class BuildingHoursView extends React.Component {
7590
buildings = defaultData.data
7691
}
7792
this.setState(() => ({
78-
loading: false,
7993
buildings: groupBuildings(buildings),
8094
now: moment.tz(CENTRAL_TZ),
8195
}))
@@ -91,7 +105,7 @@ export class BuildingHoursView extends React.Component {
91105
navigation={this.props.navigation}
92106
buildings={this.state.buildings}
93107
now={this.state.now}
94-
onRefresh={this.fetchData}
108+
onRefresh={this.refresh}
95109
loading={this.state.loading}
96110
/>
97111
)

0 commit comments

Comments
 (0)