Skip to content

Commit

Permalink
Fixes for Top Cards.
Browse files Browse the repository at this point in the history
  • Loading branch information
phulin committed Oct 14, 2019
1 parent 2275877 commit 366f332
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
10 changes: 7 additions & 3 deletions routes/tools_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const CardRating = require('../models/cardrating');
const router = express.Router();

/* Minimum number of picks to show up in Top Cards list. */
const MIN_PICKS = 20;
const MIN_PICKS = 40;
/* Maximum results to return on a vague filter string. */
const MAX_RESULTS = 300;
const MAX_RESULTS = 1000;

/* Gets k sorted minimum elements of arr. */
/* Modifies arr. */
Expand Down Expand Up @@ -93,7 +93,7 @@ function topCards(filter, res) {
];
});
const nonNullData = fullData.filter(x => x[3] !== null);
const data = sortLimit(nonNullData, MAX_RESULTS, x => -(x[3] === null ? -1 : x[3]));
const data = sortLimit(nonNullData, MAX_RESULTS, x => x[3] === null ? -1 : x[3]);
return {
ratings,
versions,
Expand All @@ -117,9 +117,11 @@ router.get('/api/topcards', (req, res) => {

topCards(filter, res).then(({
data,
names,
}) => {
res.status(200).send({
success: 'true',
numResults: names.length,
data,
});
}).catch(err => {
Expand All @@ -142,8 +144,10 @@ router.get('/topcards', (req, res) => {

topCards(filter, res).then(({
data,
names,
}) => {
res.render('tool/topcards', {
numResults: names.length,
data,
});
}).catch(err => {
Expand Down
10 changes: 8 additions & 2 deletions src/components/FilterCollapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,16 @@ class FilterCollapse extends Component {
}

render() {
const { filter, setFilter, numCards, useQuery, ...props } = this.props;
const { filter, setFilter, numCards, numShown, useQuery, ...props } = this.props;
const { filterInput, advancedOpen } = this.state;
const tokens = [];
const valid = Filter.tokenizeInput(filterInput, tokens) && Filter.verifyTokens(tokens);
const appliedText = (
'Filters applied'
+ (typeof numCards !== 'undefined' ? `: ${numCards} cards` : '')
+ (typeof numShown !== 'undefined' ? `, ${numShown} shown` : '')
+ '.'
);
return (
<Collapse {...props}>
<Container>
Expand Down Expand Up @@ -233,7 +239,7 @@ class FilterCollapse extends Component {
<h5>Filters</h5>
<p>
{!filter || filter.length === 0 ? <em>No filters applied.</em> :
<em>Filters applied{typeof numCards !== 'undefined' ? `: ${numCards} total results.` : '.'}</em>
<em>{appliedText}</em>
}
</p>
</Col>
Expand Down
12 changes: 9 additions & 3 deletions src/topcards.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class TopCards extends Component {
this.state = {
filter: [],
data: this.props.defaultData || [],
numResults: this.props.defaultNumResults || 0,
};

this.setFilter = this.setFilter.bind(this);
Expand All @@ -28,7 +29,10 @@ class TopCards extends Component {
const params = new URLSearchParams([['f', filterInput]]);
this.setState({ filter });
fetch('/tool/api/topcards?' + params.toString()).then(response => response.json()).then(json => {
this.setState({ data: json.data });
this.setState({
data: json.data,
numResults: json.numResults,
});
}).catch(err => console.error(err));
}

Expand All @@ -47,7 +51,8 @@ class TopCards extends Component {
isOpen={true}
filter={this.state.filter}
setFilter={this.setFilter}
numCards={this.state.data.length}
numCards={this.state.numResults}
numShown={this.state.data.length}
useQuery
/>
</div>
Expand All @@ -63,6 +68,7 @@ class TopCards extends Component {
}

const data = JSON.parse(document.getElementById('topcards').value);
const numResults = parseInt(document.getElementById('topcardsNumResults').value);
const wrapper = document.getElementById('react-root');
const element = <TopCards defaultData={data} />;
const element = <TopCards defaultData={data} defaultNumResults={numResults} />;
wrapper ? ReactDOM.render(element, wrapper) : false;
1 change: 1 addition & 0 deletions views/tool/topcards.pug
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extends ../layout

block content
include ../flash
input(type='hidden', id='topcardsNumResults', value=numResults)
input(type='hidden', id='topcards', value=JSON.stringify(data))
#react-root
script(src='/js/autocard.js')
Expand Down

0 comments on commit 366f332

Please sign in to comment.