Skip to content

Commit 1191a4b

Browse files
committed
chapter 6
1 parent d594d4a commit 1191a4b

File tree

2 files changed

+116
-103
lines changed

2 files changed

+116
-103
lines changed

Diff for: src/App.js

+115-102
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ const SORTS = {
2121
POINTS: list => sortBy(list, 'points').reverse(),
2222
};
2323

24+
const updateSearchTopStoriesState = (hits, page) => (prevState) => {
25+
const { searchKey, results } = prevState;
26+
27+
const oldHits = results && results[searchKey]
28+
? results[searchKey].hits
29+
: [];
30+
31+
const updatedHits = [
32+
...oldHits,
33+
...hits
34+
];
35+
36+
return {
37+
results: {
38+
...results,
39+
[searchKey]: { hits: updatedHits, page }
40+
},
41+
isLoading: false
42+
};
43+
};
44+
2445
class App extends Component {
2546
constructor(props) {
2647
super(props);
@@ -31,8 +52,6 @@ class App extends Component {
3152
searchTerm: DEFAULT_QUERY,
3253
error: null,
3354
isLoading: false,
34-
sortKey: 'NONE',
35-
isSortReverse: false,
3655
};
3756

3857
this.needsToSearchTopStories = this.needsToSearchTopStories.bind(this);
@@ -41,12 +60,6 @@ class App extends Component {
4160
this.onSearchChange = this.onSearchChange.bind(this);
4261
this.onSearchSubmit = this.onSearchSubmit.bind(this);
4362
this.onDismiss = this.onDismiss.bind(this);
44-
this.onSort = this.onSort.bind(this);
45-
}
46-
47-
onSort(sortKey) {
48-
const isSortReverse = this.state.sortKey === sortKey && !this.state.isSortReverse;
49-
this.setState({ sortKey, isSortReverse });
5063
}
5164

5265
needsToSearchTopStories(searchTerm) {
@@ -55,24 +68,7 @@ class App extends Component {
5568

5669
setSearchTopStories(result) {
5770
const { hits, page } = result;
58-
const { searchKey, results } = this.state;
59-
60-
const oldHits = results && results[searchKey]
61-
? results[searchKey].hits
62-
: [];
63-
64-
const updatedHits = [
65-
...oldHits,
66-
...hits
67-
];
68-
69-
this.setState({
70-
results: {
71-
...results,
72-
[searchKey]: { hits: updatedHits, page }
73-
},
74-
isLoading: false
75-
});
71+
this.setState(updateSearchTopStoriesState(hits, page));
7672
}
7773

7874
fetchSearchTopStories(searchTerm, page = 0) {
@@ -126,9 +122,7 @@ class App extends Component {
126122
results,
127123
searchKey,
128124
error,
129-
isLoading,
130-
sortKey,
131-
isSortReverse
125+
isLoading
132126
} = this.state;
133127

134128
const page = (
@@ -160,9 +154,6 @@ class App extends Component {
160154
</div>
161155
: <Table
162156
list={list}
163-
sortKey={sortKey}
164-
isSortReverse={isSortReverse}
165-
onSort={this.onSort}
166157
onDismiss={this.onDismiss}
167158
/>
168159
}
@@ -195,87 +186,109 @@ const Search = ({
195186
</button>
196187
</form>
197188

198-
const Table = ({
199-
list,
200-
sortKey,
201-
isSortReverse,
202-
onSort,
203-
onDismiss
204-
}) => {
205-
const sortedList = SORTS[sortKey](list);
206-
const reverseSortedList = isSortReverse
207-
? sortedList.reverse()
208-
: sortedList;
209-
210-
return(
211-
<div className="table">
212-
<div className="table-header">
213-
<span style={{ width: '40%' }}>
214-
<Sort
215-
sortKey={'TITLE'}
216-
onSort={onSort}
217-
activeSortKey={sortKey}
218-
>
219-
Title
220-
</Sort>
221-
</span>
222-
<span style={{ width: '30%' }}>
223-
<Sort
224-
sortKey={'AUTHOR'}
225-
onSort={onSort}
226-
activeSortKey={sortKey}
227-
>
228-
Author
229-
</Sort>
230-
</span>
231-
<span style={{ width: '10%' }}>
232-
<Sort
233-
sortKey={'COMMENTS'}
234-
onSort={onSort}
235-
activeSortKey={sortKey}
236-
>
237-
Comments
238-
</Sort>
239-
</span>
240-
<span style={{ width: '10%' }}>
241-
<Sort
242-
sortKey={'POINTS'}
243-
onSort={onSort}
244-
activeSortKey={sortKey}
245-
>
246-
Points
247-
</Sort>
248-
</span>
249-
<span style={{ width: '10%' }}>
250-
Archive
251-
</span>
252-
</div>
253-
{reverseSortedList.map(item =>
254-
<div key={item.objectID} className="table-row">
189+
class Table extends Component {
190+
constructor(props) {
191+
super(props);
192+
193+
this.state = {
194+
sortKey: 'NONE',
195+
isSortReverse: false,
196+
};
197+
198+
this.onSort = this.onSort.bind(this);
199+
}
200+
201+
onSort(sortKey) {
202+
const isSortReverse = this.state.sortKey === sortKey && !this.state.isSortReverse;
203+
this.setState({ sortKey, isSortReverse });
204+
}
205+
206+
render() {
207+
const {
208+
list,
209+
onDismiss
210+
} = this.props;
211+
212+
const {
213+
sortKey,
214+
isSortReverse,
215+
} = this.state;
216+
217+
const sortedList = SORTS[sortKey](list);
218+
const reverseSortedList = isSortReverse
219+
? sortedList.reverse()
220+
: sortedList;
221+
222+
return(
223+
<div className="table">
224+
<div className="table-header">
255225
<span style={{ width: '40%' }}>
256-
<a href={item.url}>{item.title}</a>
226+
<Sort
227+
sortKey={'TITLE'}
228+
onSort={this.onSort}
229+
activeSortKey={sortKey}
230+
>
231+
Title
232+
</Sort>
257233
</span>
258234
<span style={{ width: '30%' }}>
259-
{item.author}
235+
<Sort
236+
sortKey={'AUTHOR'}
237+
onSort={this.onSort}
238+
activeSortKey={sortKey}
239+
>
240+
Author
241+
</Sort>
260242
</span>
261243
<span style={{ width: '10%' }}>
262-
{item.num_comments}
244+
<Sort
245+
sortKey={'COMMENTS'}
246+
onSort={this.onSort}
247+
activeSortKey={sortKey}
248+
>
249+
Comments
250+
</Sort>
263251
</span>
264252
<span style={{ width: '10%' }}>
265-
{item.points}
253+
<Sort
254+
sortKey={'POINTS'}
255+
onSort={this.onSort}
256+
activeSortKey={sortKey}
257+
>
258+
Points
259+
</Sort>
266260
</span>
267261
<span style={{ width: '10%' }}>
268-
<Button
269-
onClick={() => onDismiss(item.objectID)}
270-
className="button-inline"
271-
>
272-
Dismiss
273-
</Button>
262+
Archive
274263
</span>
275264
</div>
276-
)}
277-
</div>
278-
);
265+
{reverseSortedList.map(item =>
266+
<div key={item.objectID} className="table-row">
267+
<span style={{ width: '40%' }}>
268+
<a href={item.url}>{item.title}</a>
269+
</span>
270+
<span style={{ width: '30%' }}>
271+
{item.author}
272+
</span>
273+
<span style={{ width: '10%' }}>
274+
{item.num_comments}
275+
</span>
276+
<span style={{ width: '10%' }}>
277+
{item.points}
278+
</span>
279+
<span style={{ width: '10%' }}>
280+
<Button
281+
onClick={() => onDismiss(item.objectID)}
282+
className="button-inline"
283+
>
284+
Dismiss
285+
</Button>
286+
</span>
287+
</div>
288+
)}
289+
</div>
290+
);
291+
}
279292
}
280293

281294
const Sort = ({

Diff for: src/__snapshots__/App.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ exports[`Table has a valid snapshot 1`] = `
151151
}
152152
>
153153
<button
154-
className="button-inline button-active"
154+
className="button-inline"
155155
onClick={[Function]}
156156
type="button"
157157
>

0 commit comments

Comments
 (0)