From 1c71bdf720cf0e724d5b66cd27d4402763de584f Mon Sep 17 00:00:00 2001 From: swaroop55 Date: Mon, 6 Mar 2017 18:53:37 +0530 Subject: [PATCH] Tap to retry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Give option to user to retry a specific request. Example Scenario: -User is in 5th page. -Network Request fails Current Behaviour: This is not handled. User has to navigate to top and pull to refresh the page. Modified Behaviour: ‘Tap to retry’ view is shown which on tapping will try to fetch the same request with same page no. --- GiftedListView.js | 48 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/GiftedListView.js b/GiftedListView.js index e80e80d..3a3094b 100644 --- a/GiftedListView.js +++ b/GiftedListView.js @@ -53,6 +53,7 @@ var GiftedListView = React.createClass({ paginationFetchingView: null, paginationAllLoadedView: null, paginationWaitingView: null, + paginationErrorView: null, emptyView: null, renderSeparator: null, rowHasChanged:null, @@ -81,6 +82,7 @@ var GiftedListView = React.createClass({ paginationFetchingView: React.PropTypes.func, paginationAllLoadedView: React.PropTypes.func, paginationWaitingView: React.PropTypes.func, + paginationErrorView: React.PropTypes.func, emptyView: React.PropTypes.func, renderSeparator: React.PropTypes.func, @@ -118,6 +120,23 @@ var GiftedListView = React.createClass({ ); }, + + paginationErrorView(errorCallback) { + if (this.props.paginationErrorView) { + return this.props.paginationErrorView(errorCallback); + } + return ( + + + + + Tap to retry + + + + + ); + }, paginationWaitingView(paginateCallback) { if (this.props.paginationWaitingView) { return this.props.paginationWaitingView(paginateCallback); @@ -235,12 +254,23 @@ var GiftedListView = React.createClass({ this.setState({ paginationStatus: 'fetching', }); - this.props.onFetch(this._getPage() + 1, this._postPaginate, {}); + this._setPage(this._getPage() + 1); + this.props.onFetch(this._getPage(), this._postPaginate, {}); + } + }, + + _onRetry() { + if(this.state.paginationStatus==='allLoaded'){ + return null + }else { + this.setState({ + paginationStatus: 'fetching', + }); + this.props.onFetch(this._getPage(), this._postPaginate, {}); } }, _postPaginate(rows = [], options = {}) { - this._setPage(this._getPage() + 1); var mergedRows = null; if (this.props.withSections === true) { mergedRows = MergeRecursive(this._getRows(), rows); @@ -258,23 +288,29 @@ var GiftedListView = React.createClass({ _updateRows(rows = [], options = {}) { if (rows !== null) { this._setRows(rows); + let paginationStatus = "waiting"; + if(options.allLoaded){ + paginationStatus = "allLoaded"; + }else if(options.isError){ + paginationStatus = "error"; + } if (this.props.withSections === true) { this.setState({ dataSource: this.state.dataSource.cloneWithRowsAndSections(rows), isRefreshing: false, - paginationStatus: (options.allLoaded === true ? 'allLoaded' : 'waiting'), + paginationStatus, }); } else { this.setState({ dataSource: this.state.dataSource.cloneWithRows(rows), isRefreshing: false, - paginationStatus: (options.allLoaded === true ? 'allLoaded' : 'waiting'), + paginationStatus, }); } } else { this.setState({ isRefreshing: false, - paginationStatus: (options.allLoaded === true ? 'allLoaded' : 'waiting'), + paginationStatus, }); } }, @@ -286,6 +322,8 @@ var GiftedListView = React.createClass({ return this.paginationWaitingView(this._onPaginate); } else if (this.state.paginationStatus === 'allLoaded' && this.props.pagination === true) { return this.paginationAllLoadedView(); + } else if (this.state.paginationStatus === 'error' && this.props.pagination === true) { + return this.paginationErrorView(this._onRetry); } else if (this._getRows().length === 0) { return this.emptyView(this._onRefresh); } else {