Skip to content

Commit

Permalink
chapter 6
Browse files Browse the repository at this point in the history
  • Loading branch information
rwieruch committed Oct 15, 2017
1 parent d594d4a commit 1191a4b
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 103 deletions.
217 changes: 115 additions & 102 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -31,8 +52,6 @@ class App extends Component {
searchTerm: DEFAULT_QUERY,
error: null,
isLoading: false,
sortKey: 'NONE',
isSortReverse: false,
};

this.needsToSearchTopStories = this.needsToSearchTopStories.bind(this);
Expand All @@ -41,12 +60,6 @@ class App extends Component {
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 });
}

needsToSearchTopStories(searchTerm) {
Expand All @@ -55,24 +68,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 = 0) {
Expand Down Expand Up @@ -126,9 +122,7 @@ class App extends Component {
results,
searchKey,
error,
isLoading,
sortKey,
isSortReverse
isLoading
} = this.state;

const page = (
Expand Down Expand Up @@ -160,9 +154,6 @@ class App extends Component {
</div>
: <Table
list={list}
sortKey={sortKey}
isSortReverse={isSortReverse}
onSort={this.onSort}
onDismiss={this.onDismiss}
/>
}
Expand Down Expand Up @@ -195,87 +186,109 @@ const Search = ({
</button>
</form>

const Table = ({
list,
sortKey,
isSortReverse,
onSort,
onDismiss
}) => {
const sortedList = SORTS[sortKey](list);
const reverseSortedList = isSortReverse
? sortedList.reverse()
: sortedList;

return(
<div className="table">
<div className="table-header">
<span style={{ width: '40%' }}>
<Sort
sortKey={'TITLE'}
onSort={onSort}
activeSortKey={sortKey}
>
Title
</Sort>
</span>
<span style={{ width: '30%' }}>
<Sort
sortKey={'AUTHOR'}
onSort={onSort}
activeSortKey={sortKey}
>
Author
</Sort>
</span>
<span style={{ width: '10%' }}>
<Sort
sortKey={'COMMENTS'}
onSort={onSort}
activeSortKey={sortKey}
>
Comments
</Sort>
</span>
<span style={{ width: '10%' }}>
<Sort
sortKey={'POINTS'}
onSort={onSort}
activeSortKey={sortKey}
>
Points
</Sort>
</span>
<span style={{ width: '10%' }}>
Archive
</span>
</div>
{reverseSortedList.map(item =>
<div key={item.objectID} className="table-row">
class Table extends Component {
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(
<div className="table">
<div className="table-header">
<span style={{ width: '40%' }}>
<a href={item.url}>{item.title}</a>
<Sort
sortKey={'TITLE'}
onSort={this.onSort}
activeSortKey={sortKey}
>
Title
</Sort>
</span>
<span style={{ width: '30%' }}>
{item.author}
<Sort
sortKey={'AUTHOR'}
onSort={this.onSort}
activeSortKey={sortKey}
>
Author
</Sort>
</span>
<span style={{ width: '10%' }}>
{item.num_comments}
<Sort
sortKey={'COMMENTS'}
onSort={this.onSort}
activeSortKey={sortKey}
>
Comments
</Sort>
</span>
<span style={{ width: '10%' }}>
{item.points}
<Sort
sortKey={'POINTS'}
onSort={this.onSort}
activeSortKey={sortKey}
>
Points
</Sort>
</span>
<span style={{ width: '10%' }}>
<Button
onClick={() => onDismiss(item.objectID)}
className="button-inline"
>
Dismiss
</Button>
Archive
</span>
</div>
)}
</div>
);
{reverseSortedList.map(item =>
<div key={item.objectID} className="table-row">
<span style={{ width: '40%' }}>
<a href={item.url}>{item.title}</a>
</span>
<span style={{ width: '30%' }}>
{item.author}
</span>
<span style={{ width: '10%' }}>
{item.num_comments}
</span>
<span style={{ width: '10%' }}>
{item.points}
</span>
<span style={{ width: '10%' }}>
<Button
onClick={() => onDismiss(item.objectID)}
className="button-inline"
>
Dismiss
</Button>
</span>
</div>
)}
</div>
);
}
}

const Sort = ({
Expand Down
2 changes: 1 addition & 1 deletion src/__snapshots__/App.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ exports[`Table has a valid snapshot 1`] = `
}
>
<button
className="button-inline button-active"
className="button-inline"
onClick={[Function]}
type="button"
>
Expand Down

0 comments on commit 1191a4b

Please sign in to comment.