Add filter mode dropdown

This commit is contained in:
kyleplo 2018-07-27 12:54:59 -04:00 committed by GitHub
parent 3ebe44f86a
commit 539cfa5bcd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,6 +8,7 @@ const React = require('react');
const api = require('../../lib/api'); const api = require('../../lib/api');
const Button = require('../../components/forms/button.jsx'); const Button = require('../../components/forms/button.jsx');
const Form = require('../../components/forms/form.jsx');
const Grid = require('../../components/grid/grid.jsx'); const Grid = require('../../components/grid/grid.jsx');
const navigationActions = require('../../redux/navigation.js'); const navigationActions = require('../../redux/navigation.js');
const TitleBanner = require('../../components/title-banner/title-banner.jsx'); const TitleBanner = require('../../components/title-banner/title-banner.jsx');
@ -23,12 +24,14 @@ class Search extends React.Component {
super(props); super(props);
bindAll(this, [ bindAll(this, [
'getSearchState', 'getSearchState',
'handleChangeSortMode',
'handleGetSearchMore', 'handleGetSearchMore',
'getTab' 'getTab'
]); ]);
this.state = this.getSearchState(); this.state = this.getSearchState();
this.state.loaded = []; this.state.loaded = [];
this.state.loadNumber = 16; this.state.loadNumber = 16;
this.state.mode = '';
this.state.offset = 0; this.state.offset = 0;
this.state.loadMore = false; this.state.loadMore = false;
} }
@ -47,6 +50,20 @@ class Search extends React.Component {
} }
term = decodeURIComponent(term.split('+').join(' ')); term = decodeURIComponent(term.split('+').join(' '));
this.props.dispatch(navigationActions.setSearchTerm(term)); this.props.dispatch(navigationActions.setSearchTerm(term));
let mode = '';
const m = query.lastIndexOf('m=');
if (m !== -1) {
mode = query.substring(m + 2, query.length).toLowerCase();
}
while (mode.indexOf('/') > -1) {
mode = mode.substring(0, term.indexOf('/'));
}
while (term.indexOf('&') > -1) {
mode = mode.substring(0, term.indexOf('&'));
}
mode = decodeURIComponent(mode.split('+').join(' '));
this.state.mode = mode;
} }
componentDidUpdate (prevProps) { componentDidUpdate (prevProps) {
if (this.props.searchTerm !== prevProps.searchTerm) { if (this.props.searchTerm !== prevProps.searchTerm) {
@ -60,11 +77,20 @@ class Search extends React.Component {
} }
const start = pathname.lastIndexOf('/'); const start = pathname.lastIndexOf('/');
const type = pathname.substring(start + 1, pathname.length); const type = pathname.substring(start + 1, pathname.length);
const modeOptions = ['trending', 'popular', 'recent', ''];
return { return {
acceptableModes: modeOptions,
tab: type, tab: type,
loadNumber: 16 loadNumber: 16
}; };
} }
handleChangeSortMode (name, value) {
if (this.state.acceptableModes.indexOf(value) !== -1) {
const term = this.props.searchTerm.split(' ').join('+');
window.location =
`${window.location.origin}/search/${this.state.tab}?q=${term}&m=${value}`;
}
}
handleGetSearchMore () { handleGetSearchMore () {
let termText = ''; let termText = '';
if (this.props.searchTerm !== '') { if (this.props.searchTerm !== '') {
@ -73,7 +99,8 @@ class Search extends React.Component {
const locale = this.props.intl.locale; const locale = this.props.intl.locale;
const loadNumber = this.state.loadNumber; const loadNumber = this.state.loadNumber;
const offset = this.state.offset; const offset = this.state.offset;
const queryString = `limit=${loadNumber}&offset=${offset}&language=${locale}&mode=popular${termText}`; const mode = this.state.mode;
const queryString = `limit=${loadNumber}&offset=${offset}&language=${locale}&mode=${mode}${termText}`;
api({ api({
uri: `/search/${this.state.tab}?${queryString}` uri: `/search/${this.state.tab}?${queryString}`
@ -167,6 +194,29 @@ class Search extends React.Component {
{this.getTab('projects')} {this.getTab('projects')}
{this.getTab('studios')} {this.getTab('studios')}
</Tabs> </Tabs>
<div className="sort-controls">
<Form className="sort-mode">
<Select
name="sort"
options={[
{
value: 'trending',
label: this.props.intl.formatMessage({id: 'explore.trending'})
},
{
value: 'popular',
label: this.props.intl.formatMessage({id: 'explore.popular'})
},
{
value: 'recent',
label: this.props.intl.formatMessage({id: 'explore.recent'})
}
]}
value={this.state.mode}
onChange={this.handleChangeSortMode}
/>
</Form>
</div>
{this.getProjectBox()} {this.getProjectBox()}
</div> </div>
</div> </div>
@ -177,6 +227,7 @@ class Search extends React.Component {
Search.propTypes = { Search.propTypes = {
dispatch: PropTypes.func, dispatch: PropTypes.func,
intl: intlShape, intl: intlShape,
mode: PropTypes.string,
searchTerm: PropTypes.string searchTerm: PropTypes.string
}; };