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 {
@@ -187,87 +178,110 @@ const Search = ({ -const Table = ({ - list, - sortKey, - isSortReverse, - onSort, - onDismiss -}) => { - const sortedList = SORTS[sortKey](list); - const reverseSortedList = isSortReverse - ? sortedList.reverse() - : sortedList; +class Table extends Component { - return ( -
-
- - - Title - - - - - Author - - - - - Comments - - - - - Points - - - - Archive - -
- { reverseSortedList.map(item => -
+ constructor(props) { + super(props); + + this.state = { + sortKey: 'NONE', + isSortReverse: false, + }; + + this.onSort = this.onSort.bind(this); + } + + onSort(sortKey) { + const isSortReverse = this.state.sortKey === sortKey && !this.state.isSortReverse; + this.setState({ sortKey, isSortReverse }); + } + + render() { + const { + list, + onDismiss + } = this.props; + + const { + sortKey, + isSortReverse, + } = this.state; + + const sortedList = SORTS[sortKey](list); + const reverseSortedList = isSortReverse + ? sortedList.reverse() + : sortedList; + + return( +
+
- {item.title} + + Title + - {item.author} + + Author + - {item.num_comments} + + Comments + - {item.points} + + Points + - + Archive
- )} -
- ); + { reverseSortedList.map(item => +
+ + {item.title} + + + {item.author} + + + {item.num_comments} + + + {item.points} + + + + +
+ )} +
+ ); + } } const Sort = ({