This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/frontend/scenes/Search/Search.js

95 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-05-12 00:23:56 +00:00
// @flow
import React from 'react';
2016-07-18 03:59:32 +00:00
import { observer } from 'mobx-react';
2016-08-12 12:56:39 +00:00
import _ from 'lodash';
2016-08-05 15:44:15 +00:00
import { Flex } from 'reflexbox';
import { withRouter } from 'react-router';
import { searchUrl } from 'utils/routeHelpers';
2016-07-19 07:30:47 +00:00
2017-05-10 07:02:11 +00:00
import SearchField from './components/SearchField';
2016-07-19 07:30:47 +00:00
import styles from './Search.scss';
import SearchStore from './SearchStore';
2016-07-18 03:59:32 +00:00
2017-05-10 07:02:11 +00:00
import Layout, { Title } from 'components/Layout';
import CenteredContent from 'components/CenteredContent';
import DocumentPreview from 'components/DocumentPreview';
import PageTitle from 'components/PageTitle';
2017-05-10 07:02:11 +00:00
2017-05-12 00:23:56 +00:00
type Props = {
history: Object,
match: Object,
2017-05-17 07:11:13 +00:00
notFound: ?boolean,
2017-05-12 00:23:56 +00:00
};
@observer class Search extends React.Component {
2017-05-12 00:23:56 +00:00
props: Props;
store: SearchStore;
2016-07-19 07:30:47 +00:00
2017-05-12 00:23:56 +00:00
constructor(props: Props) {
2016-07-19 07:30:47 +00:00
super(props);
this.store = new SearchStore();
this.updateSearchResults();
2016-07-19 07:30:47 +00:00
}
componentDidUpdate(prevProps) {
if (prevProps.match.params.query !== this.props.match.params.query) {
this.updateSearchResults();
}
}
handleKeyDown = ev => {
if (ev.which === 27) {
ev.preventDefault();
this.props.history.goBack();
}
};
updateSearchResults = _.debounce(() => {
this.store.search(this.props.match.params.query);
}, 250);
updateQuery = query => {
this.props.history.replace(searchUrl(query));
};
2016-07-18 03:59:32 +00:00
render() {
const query = this.props.match.params.query;
2017-05-12 00:23:56 +00:00
const title = <Title content="Search" />;
2016-07-19 07:30:47 +00:00
2016-07-18 03:59:32 +00:00
return (
<Layout title={title} search={false} loading={this.store.isFetching}>
<PageTitle title="Search" />
2016-07-18 03:59:32 +00:00
<CenteredContent>
2017-05-17 07:11:13 +00:00
{this.props.notFound &&
2017-02-10 03:57:35 +00:00
<div>
<h1>Not Found</h1>
<p>We're unable to find the page you're accessing.</p>
<hr />
</div>}
2017-02-10 03:57:35 +00:00
2016-08-05 15:44:15 +00:00
<Flex column auto>
<Flex auto>
2016-07-19 07:30:47 +00:00
<img
src={require('assets/icons/search.svg')}
className={styles.icon}
2016-08-05 15:44:15 +00:00
alt="Search"
2016-07-19 07:30:47 +00:00
/>
<SearchField
searchTerm={this.store.searchTerm}
onKeyDown={this.handleKeyDown}
onChange={this.updateQuery}
value={query}
2016-07-19 07:30:47 +00:00
/>
</Flex>
{this.store.documents.map(document => (
<DocumentPreview key={document.id} document={document} />
))}
2016-07-18 03:59:32 +00:00
</Flex>
</CenteredContent>
</Layout>
);
}
}
export default withRouter(Search);