Skip to content

Commit 7e2590e

Browse files
committed
added spinner
1 parent 8777182 commit 7e2590e

File tree

6 files changed

+77
-39
lines changed

6 files changed

+77
-39
lines changed

Diff for: mobile/ReactNativeTutorial/app/bundles/comments/components/Index/Index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// @flow
22
import React from 'react';
33
import { View } from 'react-native';
4+
import _ from 'lodash/fp';
45
import List from './List/List';
56
import Footer from './Footer/Footer';
7+
import Spinner from './Spinner/Spinner';
68
import withIndexProps from '../../hocs/withIndexProps';
79

810
import styles from './IndexStyle';
@@ -17,16 +19,23 @@ class Index extends React.Component {
1719

1820
constructor(props: PropsType) {
1921
super(props);
22+
_.bindAll(['renderList'], this);
2023
}
2124

2225
componentDidMount() {
2326
this.props.actions.fetch();
2427
}
2528

29+
renderList() {
30+
return this.props.meta.loading ?
31+
<Spinner /> :
32+
<List {...this.props} />
33+
}
34+
2635
render() {
2736
return (
2837
<View style={styles.container}>
29-
<List {...this.props} />
38+
{this.renderList()}
3039
<Footer />
3140
</View>
3241
)

Diff for: mobile/ReactNativeTutorial/app/bundles/comments/components/Index/List/List.js

+18-37
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,25 @@ import _ from 'lodash/fp';
77
import styles from './ListStyle';
88

99
type PropsType = {
10-
1110
}
1211

13-
class List extends React.Component {
14-
15-
props: PropsType;
16-
17-
state: {
18-
dataSource: ListView.DataSource;
19-
};
20-
21-
22-
constructor(props: PropsType) {
23-
super(props);
24-
this.state = {
25-
dataSource: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }),
26-
};
27-
}
28-
29-
componentWillReceiveProps(nextProps) {
30-
const data = _.compose(
31-
_.reverse,
32-
_.sortBy(_.get('id')),
33-
_.values
34-
)(nextProps.comments);
35-
this.setState({ dataSource: this.state.dataSource.cloneWithRows(data) })
36-
}
37-
38-
render() {
39-
return (
40-
<ListView
41-
style={styles.container}
42-
dataSource={this.state.dataSource}
43-
renderRow={(item) => <Item {...item} />}
44-
>
45-
</ListView>
46-
)
47-
}
48-
}
12+
const List = (props: PropsType) => {
13+
const data = _.compose(
14+
_.reverse,
15+
_.sortBy(_.get('id')),
16+
_.values
17+
)(props.comments);
18+
const dataSource = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 })
19+
.cloneWithRows(data);
20+
return (
21+
<ListView
22+
enableEmptySections
23+
style={styles.container}
24+
dataSource={dataSource}
25+
renderRow={(item) => <Item {...item} />}
26+
>
27+
</ListView>
28+
);
29+
};
4930

5031
export default List;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @flow
2+
import React from 'react';
3+
import { View, ActivityIndicator } from 'react-native';
4+
5+
import styles from './SpinnerStyle';
6+
7+
type PropsType = {
8+
9+
}
10+
11+
const Spinner = (props: PropsType) => (
12+
<View style={styles.container}>
13+
<ActivityIndicator
14+
size="large"
15+
color="#4641B5"
16+
/>
17+
</View>
18+
);
19+
20+
export default Spinner;
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { StyleSheet } from 'react-native';
2+
3+
const styles = StyleSheet.create({
4+
container: {
5+
flex: 1,
6+
alignItems: 'center',
7+
justifyContent: 'center',
8+
},
9+
});
10+
11+
export default styles;

Diff for: mobile/ReactNativeTutorial/app/bundles/comments/sagas/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ const UPDATE_FORM = 'SAGA:COMMENTS:UPDATE_FORM';
1515

1616

1717
function* fetchHandler() {
18+
yield put(reduxActions.setLoadingComments(true));
1819
try {
1920
const response = yield call(api.fetchComments);
2021
yield put(reduxActions.createComments(response.entities.comments))
2122
} catch (e) {
2223
console.log(e);
2324
yield call(Alert.alert, 'Error', 'Could not connect to server', [{text: 'OK'}]);
25+
} finally {
26+
yield put(reduxActions.setLoadingComments(false));
2427
}
2528
}
2629

Diff for: mobile/ReactNativeTutorial/app/reducers/commentsStoreReducer.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { fromJS } from 'immutable';
22

33
const CREATE = 'COMMENTS_STORE:CREATE';
44
const REMOVE = 'COMMENTS_STORE:REMOVE';
5+
const SET_LOADING = 'COMMENTS_STORE:SET_LOADING';
56

67
export const initialState = fromJS({
7-
meta: {}
8+
meta: {
9+
loading: false,
10+
},
811
});
912

1013
const create = (state, action) => {
@@ -15,6 +18,9 @@ const remove = (state, action) => {
1518
return state.delete(action.id);
1619
};
1720

21+
const setLoading = (state, action) => {
22+
return state.setIn(['meta', 'loading'], action.loading);
23+
};
1824

1925
export default (state, action) => {
2026
if (!state) {
@@ -26,6 +32,8 @@ export default (state, action) => {
2632
return create(state, action);
2733
case REMOVE:
2834
return remove(state, action);
35+
case SET_LOADING:
36+
return setLoading(state, action);
2937
default:
3038
return state;
3139
}
@@ -39,7 +47,12 @@ const removeComment = (id) => {
3947
return {type: REMOVE, id };
4048
};
4149

50+
const setLoadingComments = (loading) => {
51+
return {type: SET_LOADING, loading};
52+
};
53+
4254
export const actions = {
4355
createComments,
4456
removeComment,
57+
setLoadingComments,
4558
};

0 commit comments

Comments
 (0)