Merge pull request #564 from outline/jori/route-fixes

Search and routing fixes
This commit is contained in:
Jori Lallo
2018-02-06 21:56:24 -08:00
committed by GitHub
2 changed files with 11 additions and 9 deletions

View File

@ -67,7 +67,8 @@ class Search extends Component {
@observable query: string = ''; @observable query: string = '';
@observable offset: number = 0; @observable offset: number = 0;
@observable allowLoadMore: boolean = true; @observable allowLoadMore: boolean = true;
@observable isFetching = false; @observable isFetching: boolean = false;
@observable pinToTop: boolean = false;
componentDidMount() { componentDidMount() {
this.handleQueryChange(); this.handleQueryChange();
@ -103,7 +104,9 @@ class Search extends Component {
handleQueryChange = () => { handleQueryChange = () => {
const query = this.props.match.params.query; const query = this.props.match.params.query;
this.query = query ? decodeURIComponent(query) : ''; this.query = query ? query : '';
this.resultIds = [];
this.offset = 0;
this.allowLoadMore = true; this.allowLoadMore = true;
// To prevent "no results" showing before debounce kicks in // To prevent "no results" showing before debounce kicks in
@ -137,6 +140,7 @@ class Search extends Component {
limit: DEFAULT_PAGINATION_LIMIT, limit: DEFAULT_PAGINATION_LIMIT,
}); });
this.resultIds = this.resultIds.concat(newResults); this.resultIds = this.resultIds.concat(newResults);
if (this.resultIds.length > 0) this.pinToTop = true;
if ( if (
newResults.length === 0 || newResults.length === 0 ||
newResults.length < DEFAULT_PAGINATION_LIMIT newResults.length < DEFAULT_PAGINATION_LIMIT
@ -150,6 +154,7 @@ class Search extends Component {
} }
} else { } else {
this.resultIds = []; this.resultIds = [];
this.pinToTop = false;
} }
this.isFetching = false; this.isFetching = false;
@ -172,8 +177,8 @@ class Search extends Component {
render() { render() {
const { documents, notFound } = this.props; const { documents, notFound } = this.props;
const hasResults = this.resultIds.length > 0; const showEmpty =
const showEmpty = !this.isFetching && this.query && !hasResults; !this.isFetching && this.query && this.resultIds.length === 0;
return ( return (
<Container auto> <Container auto>
@ -185,14 +190,14 @@ class Search extends Component {
<p>Were unable to find the page youre accessing.</p> <p>Were unable to find the page youre accessing.</p>
</div> </div>
)} )}
<ResultsWrapper pinToTop={hasResults} column auto> <ResultsWrapper pinToTop={this.pinToTop} column auto>
<SearchField <SearchField
onKeyDown={this.handleKeyDown} onKeyDown={this.handleKeyDown}
onChange={this.updateLocation} onChange={this.updateLocation}
value={this.query} value={this.query}
/> />
{showEmpty && <Empty>No matching documents.</Empty>} {showEmpty && <Empty>No matching documents.</Empty>}
<ResultList column visible={hasResults}> <ResultList column visible={this.pinToTop}>
<StyledArrowKeyNavigation <StyledArrowKeyNavigation
mode={ArrowKeyNavigation.mode.VERTICAL} mode={ArrowKeyNavigation.mode.VERTICAL}
defaultActiveChildIndex={0} defaultActiveChildIndex={0}

View File

@ -94,8 +94,5 @@ router.get('*', async ctx => {
// middleware // middleware
koa.use(subdomainRedirect()); koa.use(subdomainRedirect());
koa.use(router.routes()); koa.use(router.routes());
koa.use(async () => {
throw httpErrors.NotFound();
});
export default koa; export default koa;