2016-04-18 16:51:45 -04:00
|
|
|
var injectIntl = require('react-intl').injectIntl;
|
|
|
|
var FormattedMessage = require('react-intl').FormattedMessage;
|
|
|
|
var React = require('react');
|
|
|
|
var render = require('../../lib/render.jsx');
|
|
|
|
|
2016-06-14 17:33:54 -04:00
|
|
|
var api = require('../../lib/api');
|
2016-04-18 16:51:45 -04:00
|
|
|
|
2016-04-30 14:07:11 -04:00
|
|
|
var Page = require('../../components/page/www/page.jsx');
|
2016-08-12 16:31:09 -04:00
|
|
|
var TitleBanner = require('../../components/title-banner/title-banner.jsx');
|
|
|
|
var Form = require('../../components/forms/form.jsx');
|
|
|
|
var Input = require('../../components/forms/input.jsx');
|
|
|
|
var Button = require('../../components/forms/button.jsx');
|
2016-04-30 14:07:11 -04:00
|
|
|
var Tabs = require('../../components/tabs/tabs.jsx');
|
2016-05-10 00:54:46 -04:00
|
|
|
var Grid = require('../../components/grid/grid.jsx');
|
2016-10-26 10:20:16 -04:00
|
|
|
|
|
|
|
var navigationDetails = require('../../../redux/navigation.js');
|
2016-04-18 16:51:45 -04:00
|
|
|
|
|
|
|
require('./search.scss');
|
|
|
|
|
2016-05-10 00:54:46 -04:00
|
|
|
// @todo migrate to React-Router once available
|
2016-04-18 16:51:45 -04:00
|
|
|
var Search = injectIntl(React.createClass({
|
|
|
|
type: 'Search',
|
2016-05-10 00:54:46 -04:00
|
|
|
getDefaultProps: function () {
|
2016-05-10 08:16:46 -04:00
|
|
|
var query = window.location.search;
|
2016-06-09 07:32:25 -04:00
|
|
|
var pathname = window.location.pathname.toLowerCase();
|
|
|
|
if (pathname[pathname.length - 1] === '/') {
|
|
|
|
pathname = pathname.substring(0, pathname.length - 1);
|
|
|
|
}
|
2016-05-10 08:16:46 -04:00
|
|
|
var start = pathname.lastIndexOf('/');
|
2016-06-09 07:32:25 -04:00
|
|
|
var type = pathname.substring(start + 1, pathname.length);
|
2016-05-10 08:16:46 -04:00
|
|
|
var q = query.lastIndexOf('q=');
|
2016-06-09 07:32:25 -04:00
|
|
|
var term = '';
|
|
|
|
if (q !== -1) {
|
|
|
|
term = query.substring(q + 2, query.length).toLowerCase();
|
2016-04-30 16:03:11 -04:00
|
|
|
}
|
2016-05-10 00:54:46 -04:00
|
|
|
while (term.indexOf('/') > -1) {
|
2016-06-09 07:32:25 -04:00
|
|
|
term = term.substring(0, term.indexOf('/'));
|
2016-04-30 16:38:48 -04:00
|
|
|
}
|
2016-05-10 00:54:46 -04:00
|
|
|
while (term.indexOf('&') > -1) {
|
2016-06-09 07:32:25 -04:00
|
|
|
term = term.substring(0, term.indexOf('&'));
|
2016-04-30 16:03:11 -04:00
|
|
|
}
|
2016-05-10 00:54:46 -04:00
|
|
|
term = term.split('+').join(' ');
|
|
|
|
|
|
|
|
return {
|
2016-05-10 08:16:46 -04:00
|
|
|
tab: type,
|
2016-05-10 00:54:46 -04:00
|
|
|
searchTerm: term,
|
|
|
|
loadNumber: 16
|
|
|
|
};
|
2016-04-18 16:51:45 -04:00
|
|
|
},
|
2016-05-10 00:54:46 -04:00
|
|
|
getInitialState: function () {
|
|
|
|
return {
|
|
|
|
loaded: [],
|
|
|
|
offset: 0
|
|
|
|
};
|
|
|
|
},
|
|
|
|
componentDidMount: function () {
|
|
|
|
this.getSearchMore();
|
2016-04-18 16:51:45 -04:00
|
|
|
},
|
|
|
|
getSearchMore: function () {
|
|
|
|
var termText = '';
|
2016-06-09 07:32:25 -04:00
|
|
|
if (this.props.searchTerm !== '') {
|
2016-05-10 00:54:46 -04:00
|
|
|
termText = '&q=' + this.props.searchTerm;
|
2016-04-30 16:38:48 -04:00
|
|
|
}
|
2016-06-14 17:33:54 -04:00
|
|
|
api({
|
2016-06-09 07:32:25 -04:00
|
|
|
uri: '/search/' + this.props.tab +
|
|
|
|
'?limit=' + this.props.loadNumber +
|
|
|
|
'&offset=' + this.state.offset +
|
2016-07-07 08:38:10 -04:00
|
|
|
'&language=' + this.props.intl.locale +
|
2016-08-12 16:31:09 -04:00
|
|
|
'&mode=popular' +
|
2016-06-09 07:32:25 -04:00
|
|
|
termText
|
2016-04-18 16:51:45 -04:00
|
|
|
}, function (err, body) {
|
2016-05-10 00:54:46 -04:00
|
|
|
var loadedSoFar = this.state.loaded;
|
2016-06-09 07:32:25 -04:00
|
|
|
Array.prototype.push.apply(loadedSoFar, body);
|
2016-05-10 00:54:46 -04:00
|
|
|
this.setState({loaded: loadedSoFar});
|
|
|
|
var currentOffset = this.state.offset + this.props.loadNumber;
|
|
|
|
this.setState({offset: currentOffset});
|
2016-04-18 16:51:45 -04:00
|
|
|
}.bind(this));
|
|
|
|
},
|
2016-08-12 16:31:09 -04:00
|
|
|
onSearchSubmit: function (formData) {
|
|
|
|
window.location.href = '/search/projects?q=' + formData.q;
|
|
|
|
},
|
2016-04-30 16:38:48 -04:00
|
|
|
getTab: function (type) {
|
2016-05-10 08:16:46 -04:00
|
|
|
var term = this.props.searchTerm.split(' ').join('+');
|
2016-06-09 07:32:25 -04:00
|
|
|
var allTab = <a href={'/search/' + type + '?q=' + term + '/'}>
|
2016-08-22 13:30:44 -04:00
|
|
|
<li>
|
2016-08-12 16:31:09 -04:00
|
|
|
<img src={'/svgs/tabs/' + type + '-inactive.svg'} className={'tab-icon ' + type} />
|
2016-06-09 07:32:25 -04:00
|
|
|
<FormattedMessage id={'general.' + type} />
|
2016-04-18 16:51:45 -04:00
|
|
|
</li>
|
|
|
|
</a>;
|
2016-05-10 00:54:46 -04:00
|
|
|
if (this.props.tab == type) {
|
2016-06-09 07:32:25 -04:00
|
|
|
allTab = <a href={'/search/' + type + '?q=' + term + '/'}>
|
2016-04-30 16:38:48 -04:00
|
|
|
<li className='active'>
|
2016-08-12 16:31:09 -04:00
|
|
|
<img src={'/svgs/tabs/' + type + '-active.svg'} className={'tab-icon ' + type} />
|
2016-06-09 07:32:25 -04:00
|
|
|
<FormattedMessage id={'general.' + type} />
|
2016-04-18 16:51:45 -04:00
|
|
|
</li>
|
|
|
|
</a>;
|
|
|
|
}
|
|
|
|
return allTab;
|
2016-10-26 10:20:16 -04:00
|
|
|
},
|
|
|
|
setSearchTerm: function (searchTerm) {
|
|
|
|
this.props.searchTerm = searchTerm;
|
2016-04-18 16:51:45 -04:00
|
|
|
},
|
|
|
|
render: function () {
|
2016-06-09 07:57:41 -04:00
|
|
|
var formatMessage = this.props.intl.formatMessage;
|
2016-08-12 16:31:09 -04:00
|
|
|
|
2016-04-18 16:51:45 -04:00
|
|
|
return (
|
|
|
|
<div>
|
2016-04-30 16:38:48 -04:00
|
|
|
<div className='outer'>
|
2016-08-12 16:31:09 -04:00
|
|
|
<TitleBanner className="masthead">
|
|
|
|
<div className="inner">
|
|
|
|
<h1>Search</h1>
|
|
|
|
<div className="search">
|
|
|
|
<Form onSubmit={this.onSearchSubmit}>
|
|
|
|
<Button type="submit" className="btn-search" />
|
|
|
|
<Input type="text"
|
|
|
|
aria-label={formatMessage({id: 'general.search'})}
|
|
|
|
placeholder={formatMessage({id: 'general.search'})}
|
|
|
|
defaultValue={decodeURI(this.props.searchTerm)}
|
|
|
|
name="q" />
|
|
|
|
</Form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</TitleBanner>
|
2016-04-30 14:07:11 -04:00
|
|
|
<Tabs>
|
2016-04-30 16:38:48 -04:00
|
|
|
{this.getTab('projects')}
|
|
|
|
{this.getTab('studios')}
|
2016-04-30 14:07:11 -04:00
|
|
|
</Tabs>
|
2016-04-30 16:38:48 -04:00
|
|
|
<div id='projectBox' key='projectBox'>
|
2016-08-12 16:31:09 -04:00
|
|
|
<Grid items={this.state.loaded}
|
|
|
|
itemType={this.props.tab}
|
|
|
|
cards={true}
|
|
|
|
showAvatar={true}
|
|
|
|
showLoves={false}
|
|
|
|
showFavorites={false}
|
|
|
|
showViews={false} />
|
|
|
|
<Button onClick={this.getSearchMore} className="white">
|
|
|
|
<FormattedMessage id='general.loadMore' />
|
|
|
|
</Button>
|
2016-04-18 16:51:45 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
render(<Page><Search /></Page>, document.getElementById('app'));
|