Skip to content

Commit 85109bc

Browse files
authored
Merge pull request #1612 from StoDevX/remote-data-sources
Add remaining remote data sources
2 parents a418b0b + eb9cf88 commit 85109bc

File tree

7 files changed

+138
-36
lines changed

7 files changed

+138
-36
lines changed

source/storybook/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ export class SnapshotsView extends React.Component {
101101
},
102102
},
103103
contacts: {
104-
list: {view: () => <ContactsView />, delay: 100},
104+
list: {
105+
view: () => <Nav>{props => <ContactsView {...props} />}</Nav>,
106+
delay: 100,
107+
},
105108
},
106109
dictionary: {
107110
list: {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {BuildingHoursList} from './list'
1212
import moment from 'moment-timezone'
1313
import type {TopLevelViewPropsType} from '../types'
1414
import type {BuildingType} from './types'
15-
import * as defaultData from '../../../docs/building-hours'
15+
import * as defaultData from '../../../docs/building-hours.json'
1616
import {reportNetworkProblem} from '../../lib/report-network-problem'
1717
import toPairs from 'lodash/toPairs'
1818
import groupBy from 'lodash/groupBy'

source/views/contacts/contact-list.js

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ import {SectionList, StyleSheet} from 'react-native'
99
import {ListSeparator, ListSectionHeader} from '../components/list'
1010
import {ListEmpty, ListFooter} from '../components/list'
1111
import {ContactRow} from './contact-row'
12-
import {data} from './data'
12+
import delay from 'delay'
13+
import {reportNetworkProblem} from '../../lib/report-network-problem'
14+
import * as defaultData from '../../../docs/contact-info.json'
1315
import groupBy from 'lodash/groupBy'
1416
import toPairs from 'lodash/toPairs'
1517
import * as c from '../components/colors'
1618
import type {ContactType} from './types'
19+
import type {TopLevelViewPropsType} from '../types'
1720

1821
const AAO_URL = 'https://github.com/StoDevX/AAO-React-Native/issues/new'
22+
const GITHUB_URL =
23+
'https://stodevx.github.io/AAO-React-Native/contact-info.json'
1924

2025
const groupContacts = (contacts: ContactType[]) => {
2126
const grouped = groupBy(contacts, c => c.category)
@@ -28,12 +33,60 @@ const styles = StyleSheet.create({
2833
},
2934
})
3035

31-
export default class ContactsListView extends React.PureComponent {
36+
type Props = TopLevelViewPropsType
37+
38+
type State = {
39+
contacts: Array<ContactType>,
40+
loading: boolean,
41+
refreshing: boolean,
42+
}
43+
44+
export class ContactsListView extends React.PureComponent<void, Props, State> {
3245
static navigationOptions = {
3346
title: 'Important Contacts',
3447
headerBackTitle: 'Contacts',
3548
}
3649

50+
state = {
51+
contacts: defaultData.data,
52+
loading: true,
53+
refreshing: false,
54+
}
55+
56+
componentWillMount() {
57+
this.fetchData()
58+
}
59+
60+
refresh = async () => {
61+
const start = Date.now()
62+
this.setState(() => ({refreshing: true}))
63+
64+
await this.fetchData()
65+
66+
// wait 0.5 seconds – if we let it go at normal speed, it feels broken.
67+
const elapsed = Date.now() - start
68+
if (elapsed < 500) {
69+
await delay(500 - elapsed)
70+
}
71+
72+
this.setState(() => ({refreshing: false}))
73+
}
74+
75+
fetchData = async () => {
76+
this.setState(() => ({loading: true}))
77+
78+
let {data: contacts} = await fetchJson(GITHUB_URL).catch(err => {
79+
reportNetworkProblem(err)
80+
return defaultData
81+
})
82+
83+
if (process.env.NODE_ENV === 'development') {
84+
contacts = defaultData.data
85+
}
86+
87+
this.setState(() => ({contacts, loading: false}))
88+
}
89+
3790
onPressContact = (contact: ContactType) => {
3891
this.props.navigation.navigate('ContactsDetailView', {
3992
contact,
@@ -46,12 +99,10 @@ export default class ContactsListView extends React.PureComponent {
4699
renderItem = ({item}: {item: ContactType}) =>
47100
<ContactRow contact={item} onPress={this.onPressContact} />
48101

49-
keyExtractor = (item: ContactType) => {
50-
return item.title
51-
}
102+
keyExtractor = (item: ContactType) => item.title
52103

53104
render() {
54-
const groupedData = groupContacts(data)
105+
const groupedData = groupContacts(this.state.contacts)
55106
return (
56107
<SectionList
57108
ItemSeparatorComponent={ListSeparator}
@@ -68,6 +119,8 @@ export default class ContactsListView extends React.PureComponent {
68119
keyExtractor={this.keyExtractor}
69120
renderSectionHeader={this.renderSectionHeader}
70121
renderItem={this.renderItem}
122+
refreshing={this.state.refreshing}
123+
onRefresh={this.refresh}
71124
/>
72125
)
73126
}

source/views/contacts/data.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

source/views/contacts/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// @flow
22

3-
export {default as ContactsView} from './contact-list'
3+
export {ContactsListView as ContactsView} from './contact-list'
44
export {ContactsDetailView} from './contact-detail'

source/views/dictionary/list.js

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
ListSectionHeader,
1616
ListSeparator,
1717
} from '../components/list'
18+
import delay from 'delay'
19+
import {reportNetworkProblem} from '../../lib/report-network-problem'
1820
import LoadingView from '../components/loading'
1921
import type {WordType} from './types'
2022
import type {TopLevelViewPropsType} from '../types'
@@ -25,9 +27,10 @@ import uniq from 'lodash/uniq'
2527
import words from 'lodash/words'
2628
import deburr from 'lodash/deburr'
2729
import isString from 'lodash/isString'
28-
import {data as terms} from '../../../docs/dictionary.json'
30+
import * as defaultData from '../../../docs/dictionary.json'
2931
import {SearchBar} from '../components/searchbar'
3032

33+
const GITHUB_URL = 'https://stodevx.github.io/AAO-React-Native/dictionary.json'
3134
const rowHeight = Platform.OS === 'ios' ? 76 : 89
3235
const headerHeight = Platform.OS === 'ios' ? 33 : 41
3336

@@ -46,19 +49,59 @@ const styles = StyleSheet.create({
4649
},
4750
})
4851

49-
export class DictionaryView extends React.Component {
52+
type Props = TopLevelViewPropsType
53+
54+
type State = {
55+
results: {[key: string]: Array<WordType>},
56+
}
57+
58+
export class DictionaryView extends React.PureComponent<void, Props, State> {
5059
static navigationOptions = {
5160
title: 'Campus Dictionary',
5261
headerBackTitle: 'Dictionary',
5362
}
5463

55-
props: TopLevelViewPropsType
5664
searchBar: any
5765

58-
state: {
59-
results: {[key: string]: Array<WordType>},
60-
} = {
61-
results: terms,
66+
state = {
67+
results: defaultData.data,
68+
allTerms: defaultData.data,
69+
loading: true,
70+
refreshing: false,
71+
}
72+
73+
componentWillMount() {
74+
this.fetchData()
75+
}
76+
77+
refresh = async () => {
78+
const start = Date.now()
79+
this.setState(() => ({refreshing: true}))
80+
81+
await this.fetchData()
82+
83+
// wait 0.5 seconds – if we let it go at normal speed, it feels broken.
84+
const elapsed = Date.now() - start
85+
if (elapsed < 500) {
86+
await delay(500 - elapsed)
87+
}
88+
89+
this.setState(() => ({refreshing: false}))
90+
}
91+
92+
fetchData = async () => {
93+
this.setState(() => ({loading: true}))
94+
95+
let {data: allTerms} = await fetchJson(GITHUB_URL).catch(err => {
96+
reportNetworkProblem(err)
97+
return defaultData
98+
})
99+
100+
if (process.env.NODE_ENV === 'development') {
101+
allTerms = defaultData.data
102+
}
103+
104+
this.setState(() => ({allTerms, loading: false}))
62105
}
63106

64107
onPressRow = (data: WordType) => {
@@ -105,13 +148,13 @@ export class DictionaryView extends React.Component {
105148
_performSearch = (text: string) => {
106149
// Android clear button returns an object
107150
if (!isString(text)) {
108-
this.setState({results: terms})
151+
this.setState(state => ({results: state.allTerms}))
109152
return
110153
}
111154

112155
const query = text.toLowerCase()
113-
this.setState(() => ({
114-
results: terms.filter(term =>
156+
this.setState(state => ({
157+
results: state.allTerms.filter(term =>
115158
this.termToArray(term).some(word => word.startsWith(query)),
116159
),
117160
}))
@@ -123,7 +166,7 @@ export class DictionaryView extends React.Component {
123166
performSearch = debounce(this._performSearch, 50)
124167

125168
render() {
126-
if (!terms) {
169+
if (this.state.loading) {
127170
return <LoadingView />
128171
}
129172

source/views/transportation/other-modes.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,30 @@ export default class OtherModesView extends React.PureComponent {
6464

6565
state = {
6666
modes: defaultData.data,
67-
loading: false,
67+
loading: true,
68+
refreshing: false,
6869
}
6970

7071
componentWillMount() {
7172
this.fetchData()
7273
}
7374

74-
fetchData = async () => {
75+
refresh = async () => {
7576
const start = Date.now()
77+
this.setState(() => ({refreshing: true}))
78+
79+
await this.fetchData()
80+
81+
// wait 0.5 seconds – if we let it go at normal speed, it feels broken.
82+
const elapsed = Date.now() - start
83+
if (elapsed < 500) {
84+
await delay(500 - elapsed)
85+
}
86+
87+
this.setState(() => ({refreshing: false}))
88+
}
89+
90+
fetchData = async () => {
7691
this.setState(() => ({loading: true}))
7792

7893
let {data: modes} = await fetchJson(GITHUB_URL).catch(err => {
@@ -84,12 +99,6 @@ export default class OtherModesView extends React.PureComponent {
8499
modes = defaultData.data
85100
}
86101

87-
// wait 0.5 seconds – if we let it go at normal speed, it feels broken.
88-
const elapsed = Date.now() - start
89-
if (elapsed < 500) {
90-
await delay(500 - elapsed)
91-
}
92-
93102
this.setState(() => ({modes, loading: false}))
94103
}
95104

@@ -102,8 +111,8 @@ export default class OtherModesView extends React.PureComponent {
102111
<FlatList
103112
keyExtractor={this.keyExtractor}
104113
renderItem={this.renderItem}
105-
refreshing={this.state.loading}
106-
onRefresh={this.fetchData}
114+
refreshing={this.state.refreshing}
115+
onRefresh={this.refresh}
107116
data={this.state.modes}
108117
/>
109118
)

0 commit comments

Comments
 (0)