Skip to content

Commit

Permalink
Merge pull request #9 from tomwayson/fix/items-route
Browse files Browse the repository at this point in the history
Fix/items route
  • Loading branch information
tomwayson authored Jan 30, 2019
2 parents c480943 + 3b3990d commit 3e6e667
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
5 changes: 5 additions & 0 deletions app.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ body {
background: linear-gradient(rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2)),
url(/static/images/Banner9.jpg) center top/cover no-repeat;
}

/* items */
.search-form-inline {
margin-top: 5px;
}
17 changes: 8 additions & 9 deletions components/AgoSearch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import React from 'react';

function inputGroupClass(size) {
const sizeClass = size ? `input-group-${size}` : '';
return `input-group ${sizeClass}`.trim();
}

class AgoSearch extends React.Component {
constructor(props) {
super(props);
Expand All @@ -18,15 +13,19 @@ class AgoSearch extends React.Component {
onSearch = e => {
// don't actually submit the form
e.preventDefault();
if (this.props.onSearch) {
const { onSearch } = this.props;
if (onSearch) {
// call search function that was passed in as a prop
this.props.onSearch(this.state.searchCopy);
onSearch(this.state.searchCopy);
}
};
render() {
const { size, className } = this.props;
const formClassName = `search-form ${className}`;
const inputGroupClass = `input-group ${size && `input-group-${size}`}`;
return (
<form className="search-form" onSubmit={this.onSearch}>
<div className={inputGroupClass(this.props.size)}>
<form className={formClassName} onSubmit={this.onSearch}>
<div className={inputGroupClass}>
<input
className="form-control"
placeholder="search for items"
Expand Down
2 changes: 1 addition & 1 deletion components/ItemsLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function ItemsLayout({ results, total, num, q, start, onParamsChange }) {
<div className="row mb-2">
<div className="col-9">
<h2>
Your search for "{q}" yielded {total} items
Your search for &quot;{q}&quot; yielded {total} items
</h2>
</div>
<div className="col-3">
Expand Down
8 changes: 6 additions & 2 deletions pages/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class Items extends React.Component {
start: 1
};
const searchForm = { ...defaults, ...query };
if (!searchForm.q) {
// invalid search term, emulate an empty response rather than sending a request
return { results: [], total: 0 };
}
// execute search and update state
return searchItems({
searchForm //,
Expand All @@ -35,13 +39,13 @@ class Items extends React.Component {
// TODO: should we not catch here and instead
// let users be taken to the error route?
// see: https://nextjs.org/docs#custom-error-handling
this.setState({
return {
pathname,
error: e.message || e,
results: null,
total: 0,
...searchForm
});
};
});
}
onParamsChange = (q, start) => {
Expand Down

0 comments on commit 3e6e667

Please sign in to comment.