Skip to content

Commit

Permalink
chapter 5
Browse files Browse the repository at this point in the history
  • Loading branch information
rwieruch committed Feb 14, 2018
1 parent 7eae32b commit 4ca37ea
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 28 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"private": true,
"dependencies": {
"axios": "^0.17.1",
"classnames": "^2.2.5",
"lodash": "^4.17.5",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-scripts": "1.1.1"
Expand Down
156 changes: 136 additions & 20 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { Component } from 'react';
import axios from 'axios';
import { sortBy } from 'lodash';
import classNames from 'classnames';
import './App.css';

const DEFAULT_QUERY = 'redux';
Expand All @@ -11,6 +13,14 @@ const PARAM_SEARCH = 'query=';
const PARAM_PAGE = 'page=';
const PARAM_HPP = 'hitsPerPage=';

const SORTS = {
NONE: list => list,
TITLE: list => sortBy(list, 'title'),
AUTHOR: list => sortBy(list, 'author'),
COMMENTS: list => sortBy(list, 'num_comments').reverse(),
POINTS: list => sortBy(list, 'points').reverse(),
};

class App extends Component {
_isMounted = false;

Expand All @@ -22,6 +32,9 @@ class App extends Component {
searchKey: '',
searchTerm: DEFAULT_QUERY,
error: null,
isLoading: false,
sortKey: 'NONE',
isSortReverse: false,
};

this.needsToSearchTopStories = this.needsToSearchTopStories.bind(this);
Expand All @@ -30,6 +43,7 @@ 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);
}

needsToSearchTopStories(searchTerm) {
Expand All @@ -53,11 +67,14 @@ class App extends Component {
results: {
...results,
[searchKey]: { hits: updatedHits, page }
}
},
isLoading: false
});
}

fetchSearchTopStories(searchTerm, page = 0) {
this.setState({ isLoading: true });

axios(`${PATH_BASE}${PATH_SEARCH}?${PARAM_SEARCH}${searchTerm}&${PARAM_PAGE}${page}&${PARAM_HPP}${DEFAULT_HPP}`)
.then(result => this.setSearchTopStories(result.data))
.catch(error => this._isMounted && this.setState({ error }));
Expand Down Expand Up @@ -105,12 +122,20 @@ class App extends Component {
});
}

onSort(sortKey) {
const isSortReverse = this.state.sortKey === sortKey && !this.state.isSortReverse;
this.setState({ sortKey, isSortReverse });
}

render() {
const {
searchTerm,
results,
searchKey,
error
error,
isLoading,
sortKey,
isSortReverse
} = this.state;

const page = (
Expand Down Expand Up @@ -142,13 +167,18 @@ class App extends Component {
</div>
: <Table
list={list}
sortKey={sortKey}
isSortReverse={isSortReverse}
onSort={this.onSort}
onDismiss={this.onDismiss}
/>
}
<div className="interactions">
<Button onClick={() => this.fetchSearchTopStories(searchKey, page + 1)}>
<ButtonWithLoading
isLoading={isLoading}
onClick={() => this.fetchSearchTopStories(searchKey, page + 1)}>
More
</Button>
</ButtonWithLoading>
</div>
</div>
);
Expand All @@ -172,33 +202,109 @@ const Search = ({
</button>
</form>

const Table = ({ list, onDismiss }) =>
<div className="table">
{list.map(item =>
<div key={item.objectID} className="table-row">
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%' }}>
<a href={item.url}>{item.title}</a>
<Sort
sortKey={'TITLE'}
onSort={onSort}
activeSortKey={sortKey}
>
Title
</Sort>
</span>
<span style={{ width: '30%' }}>
{item.author}
<Sort
sortKey={'AUTHOR'}
onSort={onSort}
activeSortKey={sortKey}
>
Author
</Sort>
</span>
<span style={{ width: '10%' }}>
{item.num_comments}
<Sort
sortKey={'COMMENTS'}
onSort={onSort}
activeSortKey={sortKey}
>
Comments
</Sort>
</span>
<span style={{ width: '10%' }}>
{item.points}
<Sort
sortKey={'POINTS'}
onSort={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 = ({
sortKey,
activeSortKey,
onSort,
children
}) => {
const sortClass = classNames(
'button-inline',
{ 'button-active': sortKey === activeSortKey }
);

return (
<Button
onClick={() => onSort(sortKey)}
className={sortClass}
>
{children}
</Button>
);
}

const Button = ({
onClick,
Expand All @@ -213,6 +319,16 @@ const Button = ({
{children}
</button>

const Loading = () =>
<div>Loading ...</div>

const withLoading = (Component) => ({ isLoading, ...rest }) =>
isLoading
? <Loading />
: <Component { ...rest } />

const ButtonWithLoading = withLoading(Button);

export {
Button,
Search,
Expand Down
2 changes: 2 additions & 0 deletions src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ describe('Table', () => {
{ title: '1', author: '1', num_comments: 1, points: 2, objectID: 'y' },
{ title: '2', author: '2', num_comments: 1, points: 2, objectID: 'z' },
],
sortKey: 'TITLE',
isSortReverse: false,
};

it('renders without crashing', () => {
Expand Down
Loading

0 comments on commit 4ca37ea

Please sign in to comment.