fix(top): fix week and month top load
This commit is contained in:
		| @ -2,11 +2,14 @@ import React from 'react'; | |||||||
| import Page from './page'; | import Page from './page'; | ||||||
|  |  | ||||||
| import TorrentLine from './torrent' | import TorrentLine from './torrent' | ||||||
| import {List} from 'material-ui/List'; | import {List, ListItem} from 'material-ui/List'; | ||||||
|  | import Divider from 'material-ui/Divider'; | ||||||
| import Subheader from 'material-ui/Subheader'; | import Subheader from 'material-ui/Subheader'; | ||||||
| import RaisedButton from 'material-ui/RaisedButton'; | import RaisedButton from 'material-ui/RaisedButton'; | ||||||
| import LinearProgress from 'material-ui/LinearProgress'; | import LinearProgress from 'material-ui/LinearProgress'; | ||||||
|  |  | ||||||
|  | import {Tabs, Tab} from 'material-ui/Tabs'; | ||||||
|  |  | ||||||
| export default class TopPage extends Page { | export default class TopPage extends Page { | ||||||
|   constructor(props) { |   constructor(props) { | ||||||
|     super(props) |     super(props) | ||||||
| @ -25,16 +28,31 @@ export default class TopPage extends Page { | |||||||
|       hours: 'Last 24 hours', |       hours: 'Last 24 hours', | ||||||
|       month: 'Last month' |       month: 'Last month' | ||||||
|     } |     } | ||||||
|  |     this.state = {value: 'All'} | ||||||
|  |   } | ||||||
|  |   loadMoreTorrents(type) | ||||||
|  |   { | ||||||
|  |     window.torrentSocket.emit('topTorrents',  | ||||||
|  |         type == 'main' ? null : type,  | ||||||
|  |         {index: (this.topTorrents[type] && this.topTorrents[type].length) || 0}, | ||||||
|  |         window.customLoader((data) => { | ||||||
|  |           if(!this.topTorrents[type]) | ||||||
|  |             this.topTorrents[type] = [] | ||||||
|  |           if(data && data.length > 0) | ||||||
|  |           { | ||||||
|  |             this.topTorrents[type] = this.topTorrents[type].concat(data); | ||||||
|  |             this.forceUpdate() | ||||||
|  |           } | ||||||
|  |           console.log(type, this.topTorrents[type]) | ||||||
|  |         }) | ||||||
|  |     ) | ||||||
|   } |   } | ||||||
|   componentDidMount() |   componentDidMount() | ||||||
|   { |   { | ||||||
|     super.componentDidMount(); |     super.componentDidMount(); | ||||||
|     for(const type of this.types) |     for(const type of this.types) | ||||||
|     { |     { | ||||||
|       window.torrentSocket.emit('topTorrents', type == 'main' ? null : type, window.customLoader((data) => { |       this.loadMoreTorrents(type) | ||||||
|         this.topTorrents[type] = data; |  | ||||||
|         this.forceUpdate() |  | ||||||
|       })) |  | ||||||
|     } |     } | ||||||
|     this.remoteTopTorrents = ({torrents, type}) => { |     this.remoteTopTorrents = ({torrents, type}) => { | ||||||
|       if(!torrents) |       if(!torrents) | ||||||
| @ -49,13 +67,16 @@ export default class TopPage extends Page { | |||||||
|     if(this.remoteTopTorrents) |     if(this.remoteTopTorrents) | ||||||
|       window.torrentSocket.off('remoteTopTorrents', this.remoteTopTorrents); |       window.torrentSocket.off('remoteTopTorrents', this.remoteTopTorrents); | ||||||
|   } |   } | ||||||
|  |   handleChange = (value) => | ||||||
|  |   { | ||||||
|  |     this.setState({ | ||||||
|  |       value, | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|   render() { |   render() { | ||||||
|     return ( |     return ( | ||||||
|       <div> |       <div> | ||||||
|       	<div className='column center w100p pad0-75'> |       	<div className='column center w100p'> | ||||||
|           <RaisedButton label="Back to main page" primary={true} onClick={() => { |  | ||||||
|             window.router('/') |  | ||||||
|           }} /> |  | ||||||
|           { |           { | ||||||
|             Object.keys(this.topTorrents).length == 0 |             Object.keys(this.topTorrents).length == 0 | ||||||
|             && |             && | ||||||
| @ -63,6 +84,11 @@ export default class TopPage extends Page { | |||||||
|               <LinearProgress mode="indeterminate" /> |               <LinearProgress mode="indeterminate" /> | ||||||
|             </div> |             </div> | ||||||
|           } |           } | ||||||
|  |           <Tabs | ||||||
|  |             className='w100p' | ||||||
|  | 		        value={this.state.value} | ||||||
|  | 		        onChange={this.handleChange} | ||||||
|  | 		      > | ||||||
|           { |           { | ||||||
|             this.types.map((type, index) => { |             this.types.map((type, index) => { | ||||||
|               const torrents = this.topTorrents[type]; |               const torrents = this.topTorrents[type]; | ||||||
| @ -71,22 +97,27 @@ export default class TopPage extends Page { | |||||||
|                 return null; |                 return null; | ||||||
|  |  | ||||||
|               return ( |               return ( | ||||||
|                 <List key={index} style={{paddingBottom: '70px'}} className='animated recent-torrents'> |  | ||||||
|                   <Subheader inset={true}> |                 <Tab key={index} label={this.descriptions[type]} value={this.descriptions[type]}> | ||||||
|                   { |                   <List style={{minWidth: '20em'}}> | ||||||
|                     this.descriptions[type] |  | ||||||
|                   } |  | ||||||
|                   </Subheader> |  | ||||||
|                     { |                     { | ||||||
|                       torrents.map((torrent, index) => { |                       torrents.map((torrent, index) => { | ||||||
|                         return <TorrentLine key={index} torrent={torrent} /> |                         return <TorrentLine key={index} torrent={torrent} /> | ||||||
|                       }) |                       }) | ||||||
|                     } |                     } | ||||||
|  |                     <div> | ||||||
|  |                       <ListItem innerDivStyle={{textAlign: 'center', padding: '1em'}} primaryText={<span>More Torrents</span>} onClick={() => { | ||||||
|  |                         this.loadMoreTorrents(type) | ||||||
|  |                       }} /> | ||||||
|  |                       <Divider /> | ||||||
|  |                     </div> | ||||||
|                   </List> |                   </List> | ||||||
|  |                 </Tab> | ||||||
|               ) |               ) | ||||||
|                |                | ||||||
|             }) |             }) | ||||||
|           } |           } | ||||||
|  |           </Tabs> | ||||||
|         </div> |         </div> | ||||||
|       </div> |       </div> | ||||||
|     ); |     ); | ||||||
|  | |||||||
| @ -389,29 +389,34 @@ module.exports = ({ | |||||||
| 		updateTorrentTrackers(hash); | 		updateTorrentTrackers(hash); | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	const topTorrentsCall = (type, callback) => { | 	const topTorrentsCall = (type, navigation = {}, callback) => { | ||||||
| 		let where = ''; | 		let where = ''; | ||||||
| 		let max = 20; |  | ||||||
|  | 		const index = parseInt(navigation.index) || 0; | ||||||
|  | 		const limit = parseInt(navigation.limit) || 20; | ||||||
|  |  | ||||||
| 		if(type && type.length > 0) | 		if(type && type.length > 0) | ||||||
| 		{ | 		{ | ||||||
| 			where += ' and contentType = ' + sphinx.escape(type) + ' '; |  | ||||||
| 			max = 15; |  | ||||||
|  |  | ||||||
| 			if(type == 'hours') | 			if(type == 'hours') | ||||||
| 			{ | 			{ | ||||||
| 				where = ' and `added` > ' + Math.floor(Date.now() / 1000) - (60 * 60 * 24) | 				where = ' and `added` > ' + (Math.floor(Date.now() / 1000) - (60 * 60 * 24)) | ||||||
| 			} | 			} | ||||||
| 			if(type == 'week') | 			else if(type == 'week') | ||||||
| 			{ | 			{ | ||||||
| 				where = ' and `added` > ' + Math.floor(Date.now() / 1000) - (60 * 60 * 24 * 7) | 				where = ' and `added` > ' + (Math.floor(Date.now() / 1000) - (60 * 60 * 24 * 7)) | ||||||
| 			} | 			} | ||||||
| 			if(type == 'month') | 			else if(type == 'month') | ||||||
| 			{ | 			{ | ||||||
| 				where = ' and `added` > ' + Math.floor(Date.now() / 1000) - (60 * 60 * 24 * 30) | 				where = ' and `added` > ' + (Math.floor(Date.now() / 1000) - (60 * 60 * 24 * 30)) | ||||||
|  | 			} | ||||||
|  | 			else | ||||||
|  | 			{ | ||||||
|  | 				where += ' and contentType = ' + sphinx.escape(type) + ' '; | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		const query = `SELECT * FROM torrents WHERE seeders > 0 and contentCategory != 'xxx' ${where} ORDER BY seeders DESC LIMIT ${max}`; | 		const query = `SELECT * FROM torrents WHERE seeders > 0 and contentCategory != 'xxx' ${where} ORDER BY seeders DESC LIMIT ${index},${limit}`; | ||||||
|  | 		console.log(query) | ||||||
| 		if(topCache[query]) | 		if(topCache[query]) | ||||||
| 		{ | 		{ | ||||||
| 			callback(topCache[query]); | 			callback(topCache[query]); | ||||||
| @ -423,18 +428,16 @@ module.exports = ({ | |||||||
| 				return; | 				return; | ||||||
| 			} | 			} | ||||||
| 			 | 			 | ||||||
| 			rows = rows.map((row) => { | 			rows = rows.map((row) => baseRowData(row)); | ||||||
| 				return baseRowData(row); |  | ||||||
| 			}); |  | ||||||
| 			topCache[query] = rows; | 			topCache[query] = rows; | ||||||
| 		  	callback(rows); | 		  	callback(rows); | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	recive('topTorrents', (type, callback) => | 	recive('topTorrents', (type, navigation, callback) => | ||||||
| 	{ | 	{ | ||||||
| 		topTorrentsCall(type, callback) | 		topTorrentsCall(type, navigation, callback) | ||||||
| 		p2p.emit('topTorrents', {type}, (remote, socketObject) => { | 		p2p.emit('topTorrents', {type, navigation}, (remote, socketObject) => { | ||||||
| 			console.log('remote top results', remote && remote.length) | 			console.log('remote top results', remote && remote.length) | ||||||
| 			if(remote && remote.length > 0) | 			if(remote && remote.length > 0) | ||||||
| 			{ | 			{ | ||||||
| @ -446,8 +449,8 @@ module.exports = ({ | |||||||
| 		}) | 		}) | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	p2p.on('topTorrents', ({type} = {}, callback) => { | 	p2p.on('topTorrents', ({type, navigation} = {}, callback) => { | ||||||
| 		topTorrentsCall(type, (data) => callback(data)) | 		topTorrentsCall(type, navigation, (data) => callback(data)) | ||||||
| 	}) | 	}) | ||||||
|  |  | ||||||
| 	recive('peers', (callback) => | 	recive('peers', (callback) => | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user