fix(top): fix week and month top load

This commit is contained in:
Alexey Kasyanchuk 2018-03-26 20:28:25 +03:00
parent 960731fb80
commit 99b48ef285
2 changed files with 74 additions and 40 deletions

View File

@ -2,11 +2,14 @@ import React from 'react';
import Page from './page';
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 RaisedButton from 'material-ui/RaisedButton';
import LinearProgress from 'material-ui/LinearProgress';
import {Tabs, Tab} from 'material-ui/Tabs';
export default class TopPage extends Page {
constructor(props) {
super(props)
@ -25,16 +28,31 @@ export default class TopPage extends Page {
hours: 'Last 24 hours',
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()
{
super.componentDidMount();
for(const type of this.types)
{
window.torrentSocket.emit('topTorrents', type == 'main' ? null : type, window.customLoader((data) => {
this.topTorrents[type] = data;
this.forceUpdate()
}))
this.loadMoreTorrents(type)
}
this.remoteTopTorrents = ({torrents, type}) => {
if(!torrents)
@ -49,13 +67,16 @@ export default class TopPage extends Page {
if(this.remoteTopTorrents)
window.torrentSocket.off('remoteTopTorrents', this.remoteTopTorrents);
}
handleChange = (value) =>
{
this.setState({
value,
});
}
render() {
return (
<div>
<div className='column center w100p pad0-75'>
<RaisedButton label="Back to main page" primary={true} onClick={() => {
window.router('/')
}} />
<div className='column center w100p'>
{
Object.keys(this.topTorrents).length == 0
&&
@ -63,6 +84,11 @@ export default class TopPage extends Page {
<LinearProgress mode="indeterminate" />
</div>
}
<Tabs
className='w100p'
value={this.state.value}
onChange={this.handleChange}
>
{
this.types.map((type, index) => {
const torrents = this.topTorrents[type];
@ -71,22 +97,27 @@ export default class TopPage extends Page {
return null;
return (
<List key={index} style={{paddingBottom: '70px'}} className='animated recent-torrents'>
<Subheader inset={true}>
{
this.descriptions[type]
}
</Subheader>
<Tab key={index} label={this.descriptions[type]} value={this.descriptions[type]}>
<List style={{minWidth: '20em'}}>
{
torrents.map((torrent, index) => {
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>
</Tab>
)
})
}
</Tabs>
</div>
</div>
);

View File

@ -389,29 +389,34 @@ module.exports = ({
updateTorrentTrackers(hash);
});
const topTorrentsCall = (type, callback) => {
const topTorrentsCall = (type, navigation = {}, callback) => {
let where = '';
let max = 20;
const index = parseInt(navigation.index) || 0;
const limit = parseInt(navigation.limit) || 20;
if(type && type.length > 0)
{
where += ' and contentType = ' + sphinx.escape(type) + ' ';
max = 15;
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])
{
callback(topCache[query]);
@ -423,18 +428,16 @@ module.exports = ({
return;
}
rows = rows.map((row) => {
return baseRowData(row);
});
rows = rows.map((row) => baseRowData(row));
topCache[query] = rows;
callback(rows);
});
}
recive('topTorrents', (type, callback) =>
recive('topTorrents', (type, navigation, callback) =>
{
topTorrentsCall(type, callback)
p2p.emit('topTorrents', {type}, (remote, socketObject) => {
topTorrentsCall(type, navigation, callback)
p2p.emit('topTorrents', {type, navigation}, (remote, socketObject) => {
console.log('remote top results', remote && remote.length)
if(remote && remote.length > 0)
{
@ -446,8 +449,8 @@ module.exports = ({
})
});
p2p.on('topTorrents', ({type} = {}, callback) => {
topTorrentsCall(type, (data) => callback(data))
p2p.on('topTorrents', ({type, navigation} = {}, callback) => {
topTorrentsCall(type, navigation, (data) => callback(data))
})
recive('peers', (callback) =>