Skip to content

Commit 8209a2f

Browse files
committed
Lifting State
1 parent 5e7cb9b commit 8209a2f

File tree

1 file changed

+99
-88
lines changed

1 file changed

+99
-88
lines changed

src/App.js

Lines changed: 99 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ class App extends Component {
4343
searchTerm: DEFAULT_QUERY,
4444
error: null,
4545
isLoading: false,
46-
sortKey: 'NONE',
47-
isSortReverse: false,
4846
};
4947

5048
this.needsToSearchTopStories = this.needsToSearchTopStories.bind(this);
@@ -53,7 +51,6 @@ class App extends Component {
5351
this.onSearchChange = this.onSearchChange.bind(this);
5452
this.onSearchSubmit = this.onSearchSubmit.bind(this);
5553
this.onDismiss = this.onDismiss.bind(this);
56-
this.onSort = this.onSort.bind(this);
5754
}
5855

5956
needsToSearchTopStories(searchTerm) {
@@ -126,20 +123,13 @@ class App extends Component {
126123
});
127124
}
128125

129-
onSort(sortKey) {
130-
const isSortReverse = this.state.sortKey === sortKey && !this.state.isSortReverse;
131-
this.setState({ sortKey, isSortReverse });
132-
}
133-
134126
render() {
135127
const {
136128
searchTerm,
137129
results,
138130
searchKey,
139131
error,
140-
isLoading,
141-
sortKey,
142-
isSortReverse
132+
isLoading
143133
} = this.state;
144134

145135
const page = (
@@ -171,9 +161,6 @@ class App extends Component {
171161
</div>
172162
: <Table
173163
list={list}
174-
sortKey={sortKey}
175-
isSortReverse={isSortReverse}
176-
onSort={this.onSort}
177164
onDismiss={this.onDismiss}
178165
/>
179166
}
@@ -207,87 +194,111 @@ const Search = ({
207194
</button>
208195
</form>
209196

210-
const Table = ({
211-
list,
212-
sortKey,
213-
isSortReverse,
214-
onSort,
215-
onDismiss
216-
}) => {
217-
const sortedList = SORTS[sortKey](list);
218-
const reverseSortedList = isSortReverse
219-
? sortedList.reverse()
220-
: sortedList;
197+
class Table extends Component {
198+
constructor(props) {
199+
super(props);
221200

222-
return (
223-
<div className="table">
224-
<div className="table-header">
225-
<span style={{ width: '40%' }}>
226-
<Sort
227-
sortKey={'TITLE'}
228-
onSort={onSort}
229-
activeSortKey={sortKey}
230-
>
231-
Title
232-
</Sort>
233-
</span>
234-
<span style={{ width: '30%' }}>
235-
<Sort
236-
sortKey={'AUTHOR'}
237-
onSort={onSort}
238-
activeSortKey={sortKey}
239-
>
240-
Author
241-
</Sort>
242-
</span>
243-
<span style={{ width: '10%' }}>
244-
<Sort
245-
sortKey={'COMMENTS'}
246-
onSort={onSort}
247-
activeSortKey={sortKey}
248-
>
249-
Comments
250-
</Sort>
251-
</span>
252-
<span style={{ width: '10%' }}>
253-
<Sort
254-
sortKey={'POINTS'}
255-
onSort={onSort}
256-
activeSortKey={sortKey}
257-
>
258-
Points
259-
</Sort>
260-
</span>
261-
<span style={{ width: '10%' }}>
262-
Archive
263-
</span>
264-
</div>
265-
{reverseSortedList.map(item =>
266-
<div key={item.objectID} className="table-row">
267-
<span style={largeColumn}>
268-
<a href={item.url}>{item.title}</a>
269-
</span>
270-
<span style={midColumn}>
271-
{item.author}
201+
this.state = {
202+
sortKey: 'NONE',
203+
isSortReverse: false,
204+
};
205+
206+
this.onSort = this.onSort.bind(this);
207+
}
208+
209+
onSort(sortKey) {
210+
const isSortReverse = this.state.sortKey === sortKey &&
211+
!this.state.isSortReverse;
212+
213+
this.setState({ sortKey, isSortReverse });
214+
}
215+
216+
render() {
217+
const {
218+
list,
219+
onDismiss
220+
} = this.props;
221+
222+
const {
223+
sortKey,
224+
isSortReverse,
225+
} = this.state;
226+
227+
const sortedList = SORTS[sortKey](list);
228+
const reverseSortedList = isSortReverse
229+
? sortedList.reverse()
230+
: sortedList;
231+
232+
return (
233+
<div className="table">
234+
<div className="table-header">
235+
<span style={{ width: '40%' }}>
236+
<Sort
237+
sortKey={'TITLE'}
238+
onSort={this.onSort}
239+
activeSortKey={sortKey}
240+
>
241+
Title
242+
</Sort>
272243
</span>
273-
<span style={smallColumn}>
274-
{item.num_comments}
244+
<span style={{ width: '30%' }}>
245+
<Sort
246+
sortKey={'AUTHOR'}
247+
onSort={this.onSort}
248+
activeSortKey={sortKey}
249+
>
250+
Author
251+
</Sort>
275252
</span>
276-
<span style={smallColumn}>
277-
{item.points}
253+
<span style={{ width: '10%' }}>
254+
<Sort
255+
sortKey={'COMMENTS'}
256+
onSort={this.onSort}
257+
activeSortKey={sortKey}
258+
>
259+
Comments
260+
</Sort>
278261
</span>
279-
<span style={smallColumn}>
280-
<Button
281-
onClick={() => onDismiss(item.objectID)}
282-
className="button-inline"
262+
<span style={{ width: '10%' }}>
263+
<Sort
264+
sortKey={'POINTS'}
265+
onSort={this.onSort}
266+
activeSortKey={sortKey}
283267
>
284-
Dismiss
285-
</Button>
268+
Points
269+
</Sort>
270+
</span>
271+
<span style={{ width: '10%' }}>
272+
Archive
286273
</span>
287274
</div>
288-
)}
289-
</div>
290-
);
275+
{reverseSortedList.map(item =>
276+
<div key={item.objectID} className="table-row">
277+
<span style={largeColumn}>
278+
<a href={item.url}>{item.title}</a>
279+
</span>
280+
<span style={midColumn}>
281+
{item.author}
282+
</span>
283+
<span style={smallColumn}>
284+
{item.num_comments}
285+
</span>
286+
<span style={smallColumn}>
287+
{item.points}
288+
</span>
289+
<span style={smallColumn}>
290+
<Button
291+
onClick={() => onDismiss(item.objectID)}
292+
className="button-inline"
293+
>
294+
Dismiss
295+
</Button>
296+
</span>
297+
</div>
298+
)}
299+
</div>
300+
);
301+
}
291302
}
292303

293304
const Sort = ({

0 commit comments

Comments
 (0)