diff --git a/src/App.js b/src/App.js index a2aa8ca..1f28a4d 100644 --- a/src/App.js +++ b/src/App.js @@ -21,6 +21,27 @@ const SORTS = { POINTS: list => sortBy(list, 'points').reverse(), }; +const updateSearchTopstoriesState = (hits, page) => (prevState) => { + const { searchKey, results } = prevState; + + const oldHits = results && results[searchKey] + ? results[searchKey].hits + : []; + + const updatedHits = [ + ...oldHits, + ...hits + ]; + + return { + results: { + ...results, + [searchKey]: { hits: updatedHits, page } + }, + isLoading: false + }; +}; + class App extends Component { constructor(props) { super(props); @@ -30,22 +51,14 @@ class App extends Component { searchKey: '', searchTerm: DEFAULT_QUERY, isLoading: false, - sortKey: 'NONE', - isSortReverse: false, }; - this.needsToSearchTopstories = this.needsToSearchTopstories.bind(this); this.setSearchTopstories = this.setSearchTopstories.bind(this); this.fetchSearchTopstories = this.fetchSearchTopstories.bind(this); this.onSearchChange = this.onSearchChange.bind(this); this.onSearchSubmit = this.onSearchSubmit.bind(this); this.onDismiss = this.onDismiss.bind(this); - this.onSort = this.onSort.bind(this); - } - - onSort(sortKey) { - const isSortReverse = this.state.sortKey === sortKey && !this.state.isSortReverse; - this.setState({ sortKey, isSortReverse }); + this.needsToSearchTopstories = this.needsToSearchTopstories.bind(this); } needsToSearchTopstories(searchTerm) { @@ -54,24 +67,7 @@ class App extends Component { setSearchTopstories(result) { const { hits, page } = result; - const { searchKey, results } = this.state; - - const oldHits = results && results[searchKey] - ? results[searchKey].hits - : []; - - const updatedHits = [ - ...oldHits, - ...hits - ]; - - this.setState({ - results: { - ...results, - [searchKey]: { hits: updatedHits, page } - }, - isLoading: false - }); + this.setState(updateSearchTopstoriesState(hits, page)); } fetchSearchTopstories(searchTerm, page) { @@ -123,9 +119,7 @@ class App extends Component { searchTerm, results, searchKey, - isLoading, - sortKey, - isSortReverse + isLoading } = this.state; const page = ( @@ -153,9 +147,6 @@ class App extends Component {