forked from vergnet/application-amicale
		
	Block refresh to once every minute
This commit is contained in:
		
							parent
							
								
									aba52782d0
								
							
						
					
					
						commit
						8e03b6b8c2
					
				
					 2 changed files with 33 additions and 10 deletions
				
			
		|  | @ -20,6 +20,8 @@ type State = { | ||||||
|     machinesWatched: Array<Object>, |     machinesWatched: Array<Object>, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | const minTimeBetweenRefresh = 60; | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * Class used to create a basic list view using online json data. |  * Class used to create a basic list view using online json data. | ||||||
|  * Used by inheriting from it and redefining getters. |  * Used by inheriting from it and redefining getters. | ||||||
|  | @ -32,6 +34,7 @@ export default class FetchedDataSectionList extends React.Component<Props, State | ||||||
|     willBlurSubscription: function; |     willBlurSubscription: function; | ||||||
|     refreshInterval: IntervalID; |     refreshInterval: IntervalID; | ||||||
|     refreshTime: number; |     refreshTime: number; | ||||||
|  |     lastRefresh: Date; | ||||||
| 
 | 
 | ||||||
|     constructor(fetchUrl: string, refreshTime: number) { |     constructor(fetchUrl: string, refreshTime: number) { | ||||||
|         super(); |         super(); | ||||||
|  | @ -112,15 +115,33 @@ export default class FetchedDataSectionList extends React.Component<Props, State | ||||||
|      * @private |      * @private | ||||||
|      */ |      */ | ||||||
|     _onRefresh = () => { |     _onRefresh = () => { | ||||||
|         this.setState({refreshing: true}); |         let canRefresh; | ||||||
|         this.webDataManager.readData().then((fetchedData) => { |         if (this.lastRefresh !== undefined) | ||||||
|             this.setState({ |             canRefresh = (new Date().getTime() - this.lastRefresh.getTime())/1000 > minTimeBetweenRefresh; | ||||||
|                 fetchedData: fetchedData, |         else | ||||||
|                 refreshing: false, |             canRefresh = true; | ||||||
|                 firstLoading: false | 
 | ||||||
|             }); |         if (canRefresh) { | ||||||
|             this.webDataManager.showUpdateToast(this.getUpdateToastTranslations()[0], this.getUpdateToastTranslations()[1]); |             this.setState({refreshing: true}); | ||||||
|         }); |             this.webDataManager.readData() | ||||||
|  |                 .then((fetchedData) => { | ||||||
|  |                     this.setState({ | ||||||
|  |                         fetchedData: fetchedData, | ||||||
|  |                         refreshing: false, | ||||||
|  |                         firstLoading: false | ||||||
|  |                     }); | ||||||
|  |                     this.lastRefresh = new Date(); | ||||||
|  |                 }) | ||||||
|  |                 .catch((err) => { | ||||||
|  |                     this.setState({ | ||||||
|  |                         fetchedData: {}, | ||||||
|  |                         refreshing: false, | ||||||
|  |                         firstLoading: false | ||||||
|  |                     }); | ||||||
|  |                     this.webDataManager.showUpdateToast(this.getUpdateToastTranslations()[0], this.getUpdateToastTranslations()[1]); | ||||||
|  |                 }); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|  | @ -316,7 +337,8 @@ export default class FetchedDataSectionList extends React.Component<Props, State | ||||||
|         const nav = this.props.navigation; |         const nav = this.props.navigation; | ||||||
|         const dataset = this.createDataset(this.state.fetchedData); |         const dataset = this.createDataset(this.state.fetchedData); | ||||||
|         return ( |         return ( | ||||||
|             <BaseContainer navigation={nav} headerTitle={this.getHeaderTranslation()} headerRightButton={this.getRightButton()}> |             <BaseContainer navigation={nav} headerTitle={this.getHeaderTranslation()} | ||||||
|  |                            headerRightButton={this.getRightButton()}> | ||||||
|                 {this.hasTabs() ? |                 {this.hasTabs() ? | ||||||
|                     <Tabs> |                     <Tabs> | ||||||
|                         {this.getTabbedView(dataset)} |                         {this.getTabbedView(dataset)} | ||||||
|  |  | ||||||
|  | @ -27,6 +27,7 @@ export default class WebDataManager { | ||||||
|         } catch (error) { |         } catch (error) { | ||||||
|             console.log('Could not read FetchedData from server'); |             console.log('Could not read FetchedData from server'); | ||||||
|             console.log(error); |             console.log(error); | ||||||
|  |             throw new Error('Could not read FetchedData from server'); | ||||||
|         } |         } | ||||||
|         this.lastDataFetched = fetchedData; |         this.lastDataFetched = fetchedData; | ||||||
|         return fetchedData; |         return fetchedData; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue