-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
65 lines (53 loc) · 1.72 KB
/
App.js
File metadata and controls
65 lines (53 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, { useState } from 'react';
import './App.css';
import 'h8k-components';
import Articles from './components/Articles';
const title = "Sorting Articles";
function App({articles}) {
const [articlesList, setArticlesList] = useState(articles);
useEffect(() => {
sortByUpvotes(articles);
},[]);
const sortByUpvotes = () => {
var newArticles = [];
Object.assign(newArticles, articlesList);
newArticles.sort((a, b) => {
if (a.upvotes > b.upvotes) {
return -1;
}
if (a.upvotes < b.upvotes) {
return 1;
}
return 0;
})
setArticlesList(newArticles);
}
const sortByDates = () => {
var newArticles = [];
Object.assign(newArticles, articlesList);
newArticles.sort((a, b) => {
const aDate = new Date(a.date);
const bDate = new Date(b.date);
if (aDate > bDate) {
return -1;
}
if (aDate < bDate) {
return 1;
}
return 0;
})
setArticlesList(newArticles);
}
return (
<div className="App">
<h8k-navbar header={title}></h8k-navbar>
<div className="layout-row align-items-center justify-content-center my-20 navigation">
<label className="form-hint mb-0 text-uppercase font-weight-light">Sort By</label>
<button data-testid="most-upvoted-link" className="small" onClick={() => sortByUpvotes()}>Most Upvoted</button>
<button data-testid="most-recent-link" className="small" onClick={() => sortByDates()}>Most Recent</button>
</div>
<Articles articles={articlesList}/>
</div>
);
}
export default App;