1
1
import React from 'react' ;
2
+ import PropTypes from 'prop-types' ;
2
3
import { connect } from 'react-redux' ;
3
4
import {
4
5
FlatList ,
@@ -12,6 +13,8 @@ import {
12
13
import styled from 'styled-components' ;
13
14
import CardView from 'react-native-cardview' ;
14
15
import TimeAgo from 'time-ago' ;
16
+ import concat from 'lodash/concat' ;
17
+ import uniqBy from 'lodash/uniqBy' ;
15
18
import { fetchGistComments , deleteComment , addComment } from '../gists.actiontype' ;
16
19
import ListEmptyComponent from './components/EmptyListComponent' ;
17
20
import { colors } from '../../config' ;
@@ -75,6 +78,16 @@ const ActivityIndicatorContainer = styled.View`
75
78
justify-content: center;
76
79
align-items: center;
77
80
` ;
81
+ // padding: 20;
82
+ const EndOfViewStyle = styled . View `
83
+ flex: 1;
84
+ justify-content: center;
85
+ align-items: center;
86
+ margin-bottom: 20;
87
+ margin-top: 20;
88
+ ` ;
89
+
90
+ const getGistComments = item => ( { type : item , id : item } ) ;
78
91
79
92
class GistCommentsScreen extends React . Component {
80
93
constructor ( ) {
@@ -121,7 +134,31 @@ class GistCommentsScreen extends React.Component {
121
134
} ) ;
122
135
}
123
136
137
+ handleOnEndReached = ( ) => {
138
+ this . props . fetchComments ( this . props . navigation . getParam ( 'gistData' ) . id ) ;
139
+ }
140
+
124
141
renderItem = ( { item } ) => {
142
+ console . log ( 'item type=============' , item . type ) ;
143
+ switch ( item . type ) {
144
+ case 'preloader' :
145
+ return (
146
+ < EndOfViewStyle >
147
+ < ActivityIndicator
148
+ size = "large"
149
+ color = "#33B5E5"
150
+ />
151
+ </ EndOfViewStyle >
152
+ ) ;
153
+ case 'noData' :
154
+ return (
155
+ < EndOfViewStyle >
156
+ < ListEmptyComponent
157
+ message = "No more comments found for this gist"
158
+ />
159
+ </ EndOfViewStyle >
160
+ ) ;
161
+ default :
125
162
return (
126
163
< TouchableOpacity
127
164
style = { { flex : 1 } }
@@ -141,28 +178,36 @@ class GistCommentsScreen extends React.Component {
141
178
< Comment > { item . body } </ Comment >
142
179
</ CardContainer >
143
180
</ TouchableOpacity >
144
- ) ;
181
+ ) ;
182
+ }
145
183
}
146
184
147
185
render ( ) {
148
- const { comments , inProgress } = this . props ;
186
+ const { inProgress , hasMoreComments , comments } = this . props ;
149
187
150
- if ( inProgress ) {
151
- return (
152
- < ActivityIndicatorContainer >
153
- < ActivityIndicator size = "large" color = "#33B5E5" />
154
- </ ActivityIndicatorContainer >
155
- ) ;
156
- }
188
+ console . log ( 'hasMoreComments' , hasMoreComments ) ;
189
+ const toAppendData = hasMoreComments ? getGistComments ( 'preloader' ) : getGistComments ( 'noData' ) ;
190
+
191
+ const uniqComments = uniqBy ( concat ( comments , toAppendData ) , ( { id } ) => ( id ) ) ;
192
+
193
+ // if (inProgress) {
194
+ // return (
195
+ // <ActivityIndicatorContainer>
196
+ // <ActivityIndicator size="large" color="#33B5E5" />
197
+ // </ActivityIndicatorContainer>
198
+ // );
199
+ // }
157
200
158
201
return (
159
202
< React . Fragment >
160
203
< FlatList
161
204
style = { { marginBottom : '11%' , flexGrow : 1 } }
162
205
keyExtractor = { item => item . id }
163
- data = { this . props . comments }
206
+ data = { uniqComments }
164
207
renderItem = { this . renderItem }
165
208
ListEmptyComponent = { ( ) => < ListEmptyComponent message = "No comments found" /> }
209
+ onEndReachedThreshold = { 0.01 }
210
+ onEndReached = { this . handleOnEndReached }
166
211
extraData = { this . props }
167
212
/>
168
213
< InputContainer >
@@ -199,6 +244,17 @@ const mapStateToProps = ({ gistComments, loggedInUser }) => ({
199
244
comments : gistComments . comments ,
200
245
currentUserId : loggedInUser . userId ,
201
246
inProgress : gistComments . inProgress ,
247
+ hasMoreComments : gistComments . hasMoreComments ,
202
248
} ) ;
203
249
250
+ GistCommentsScreen . propTypes = {
251
+ // comments: PropTypes.arrayOf(React.PropTypes.object).isRequired,
252
+ inProgress : PropTypes . bool . isRequired ,
253
+ fetchComments : PropTypes . func . isRequired ,
254
+ addThisComment : PropTypes . func . isRequired ,
255
+ deleteThisComment : PropTypes . func . isRequired ,
256
+ currentUserId : PropTypes . number . isRequired ,
257
+ // navigation: PropTypes.object.isRequired,
258
+ } ;
259
+
204
260
export default connect ( mapStateToProps , mapDispatchToProps ) ( GistCommentsScreen ) ;
0 commit comments