@@ -21,6 +21,27 @@ const SORTS = {
21
21
POINTS : list => sortBy ( list , 'points' ) . reverse ( ) ,
22
22
} ;
23
23
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
+
24
45
class App extends Component {
25
46
constructor ( props ) {
26
47
super ( props ) ;
@@ -30,22 +51,14 @@ class App extends Component {
30
51
searchKey : '' ,
31
52
searchTerm : DEFAULT_QUERY ,
32
53
isLoading : false ,
33
- sortKey : 'NONE' ,
34
- isSortReverse : false ,
35
54
} ;
36
55
37
- this . needsToSearchTopstories = this . needsToSearchTopstories . bind ( this ) ;
38
56
this . setSearchTopstories = this . setSearchTopstories . bind ( this ) ;
39
57
this . fetchSearchTopstories = this . fetchSearchTopstories . bind ( this ) ;
40
58
this . onSearchChange = this . onSearchChange . bind ( this ) ;
41
59
this . onSearchSubmit = this . onSearchSubmit . bind ( this ) ;
42
60
this . onDismiss = this . onDismiss . bind ( this ) ;
43
- this . onSort = this . onSort . bind ( this ) ;
44
- }
45
-
46
- onSort ( sortKey ) {
47
- const isSortReverse = this . state . sortKey === sortKey && ! this . state . isSortReverse ;
48
- this . setState ( { sortKey, isSortReverse } ) ;
61
+ this . needsToSearchTopstories = this . needsToSearchTopstories . bind ( this ) ;
49
62
}
50
63
51
64
needsToSearchTopstories ( searchTerm ) {
@@ -54,24 +67,7 @@ class App extends Component {
54
67
55
68
setSearchTopstories ( result ) {
56
69
const { hits, page } = result ;
57
- const { searchKey, results } = this . state ;
58
-
59
- const oldHits = results && results [ searchKey ]
60
- ? results [ searchKey ] . hits
61
- : [ ] ;
62
-
63
- const updatedHits = [
64
- ...oldHits ,
65
- ...hits
66
- ] ;
67
-
68
- this . setState ( {
69
- results : {
70
- ...results ,
71
- [ searchKey ] : { hits : updatedHits , page }
72
- } ,
73
- isLoading : false
74
- } ) ;
70
+ this . setState ( updateSearchTopstoriesState ( hits , page ) ) ;
75
71
}
76
72
77
73
fetchSearchTopstories ( searchTerm , page ) {
@@ -123,9 +119,7 @@ class App extends Component {
123
119
searchTerm,
124
120
results,
125
121
searchKey,
126
- isLoading,
127
- sortKey,
128
- isSortReverse
122
+ isLoading
129
123
} = this . state ;
130
124
131
125
const page = (
@@ -153,9 +147,6 @@ class App extends Component {
153
147
</ div >
154
148
< Table
155
149
list = { list }
156
- sortKey = { sortKey }
157
- isSortReverse = { isSortReverse }
158
- onSort = { this . onSort }
159
150
onDismiss = { this . onDismiss }
160
151
/>
161
152
< div className = "interactions" >
@@ -187,87 +178,110 @@ const Search = ({
187
178
</ button >
188
179
</ form >
189
180
190
- const Table = ( {
191
- list,
192
- sortKey,
193
- isSortReverse,
194
- onSort,
195
- onDismiss
196
- } ) => {
197
- const sortedList = SORTS [ sortKey ] ( list ) ;
198
- const reverseSortedList = isSortReverse
199
- ? sortedList . reverse ( )
200
- : sortedList ;
181
+ class Table extends Component {
201
182
202
- return (
203
- < div className = "table" >
204
- < div className = "table-header" >
205
- < span style = { { width : '40%' } } >
206
- < Sort
207
- sortKey = { 'TITLE' }
208
- onSort = { onSort }
209
- activeSortKey = { sortKey }
210
- >
211
- Title
212
- </ Sort >
213
- </ span >
214
- < span style = { { width : '30%' } } >
215
- < Sort
216
- sortKey = { 'AUTHOR' }
217
- onSort = { onSort }
218
- activeSortKey = { sortKey }
219
- >
220
- Author
221
- </ Sort >
222
- </ span >
223
- < span style = { { width : '10%' } } >
224
- < Sort
225
- sortKey = { 'COMMENTS' }
226
- onSort = { onSort }
227
- activeSortKey = { sortKey }
228
- >
229
- Comments
230
- </ Sort >
231
- </ span >
232
- < span style = { { width : '10%' } } >
233
- < Sort
234
- sortKey = { 'POINTS' }
235
- onSort = { onSort }
236
- activeSortKey = { sortKey }
237
- >
238
- Points
239
- </ Sort >
240
- </ span >
241
- < span style = { { width : '10%' } } >
242
- Archive
243
- </ span >
244
- </ div >
245
- { reverseSortedList . map ( item =>
246
- < div key = { item . objectID } className = "table-row" >
183
+ constructor ( props ) {
184
+ super ( props ) ;
185
+
186
+ this . state = {
187
+ sortKey : 'NONE' ,
188
+ isSortReverse : false ,
189
+ } ;
190
+
191
+ this . onSort = this . onSort . bind ( this ) ;
192
+ }
193
+
194
+ onSort ( sortKey ) {
195
+ const isSortReverse = this . state . sortKey === sortKey && ! this . state . isSortReverse ;
196
+ this . setState ( { sortKey, isSortReverse } ) ;
197
+ }
198
+
199
+ render ( ) {
200
+ const {
201
+ list,
202
+ onDismiss
203
+ } = this . props ;
204
+
205
+ const {
206
+ sortKey,
207
+ isSortReverse,
208
+ } = this . state ;
209
+
210
+ const sortedList = SORTS [ sortKey ] ( list ) ;
211
+ const reverseSortedList = isSortReverse
212
+ ? sortedList . reverse ( )
213
+ : sortedList ;
214
+
215
+ return (
216
+ < div className = "table" >
217
+ < div className = "table-header" >
247
218
< span style = { { width : '40%' } } >
248
- < a href = { item . url } > { item . title } </ a >
219
+ < Sort
220
+ sortKey = { 'TITLE' }
221
+ onSort = { this . onSort }
222
+ activeSortKey = { sortKey }
223
+ >
224
+ Title
225
+ </ Sort >
249
226
</ span >
250
227
< span style = { { width : '30%' } } >
251
- { item . author }
228
+ < Sort
229
+ sortKey = { 'AUTHOR' }
230
+ onSort = { this . onSort }
231
+ activeSortKey = { sortKey }
232
+ >
233
+ Author
234
+ </ Sort >
252
235
</ span >
253
236
< span style = { { width : '10%' } } >
254
- { item . num_comments }
237
+ < Sort
238
+ sortKey = { 'COMMENTS' }
239
+ onSort = { this . onSort }
240
+ activeSortKey = { sortKey }
241
+ >
242
+ Comments
243
+ </ Sort >
255
244
</ span >
256
245
< span style = { { width : '10%' } } >
257
- { item . points }
246
+ < Sort
247
+ sortKey = { 'POINTS' }
248
+ onSort = { this . onSort }
249
+ activeSortKey = { sortKey }
250
+ >
251
+ Points
252
+ </ Sort >
258
253
</ span >
259
254
< span style = { { width : '10%' } } >
260
- < Button
261
- onClick = { ( ) => onDismiss ( item . objectID ) }
262
- className = "button-inline"
263
- >
264
- Dismiss
265
- </ Button >
255
+ Archive
266
256
</ span >
267
257
</ div >
268
- ) }
269
- </ div >
270
- ) ;
258
+ { reverseSortedList . map ( item =>
259
+ < div key = { item . objectID } className = "table-row" >
260
+ < span style = { { width : '40%' } } >
261
+ < a href = { item . url } > { item . title } </ a >
262
+ </ span >
263
+ < span style = { { width : '30%' } } >
264
+ { item . author }
265
+ </ span >
266
+ < span style = { { width : '10%' } } >
267
+ { item . num_comments }
268
+ </ span >
269
+ < span style = { { width : '10%' } } >
270
+ { item . points }
271
+ </ span >
272
+ < span style = { { width : '10%' } } >
273
+ < Button
274
+ onClick = { ( ) => onDismiss ( item . objectID ) }
275
+ className = "button-inline"
276
+ >
277
+ Dismiss
278
+ </ Button >
279
+ </ span >
280
+ </ div >
281
+ ) }
282
+ </ div >
283
+ ) ;
284
+ }
271
285
}
272
286
273
287
const Sort = ( {
0 commit comments