-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocialNetwork.js
60 lines (56 loc) · 1.73 KB
/
SocialNetwork.js
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
import React, { useState, useEffect } from 'react';
import { get } from './mockBackend/fetch';
export default function SocialNetwork() {
// Split each state and effect into it's own section of code to make it easier to manage and test.
// Initially, these were all combined.
const [menu, setMenu] = useState(null);
useEffect(() => {
get('/menu').then(response => setMenu(response.data));
}, []);
const [newsFeed, setNewsFeed] = useState(null);
useEffect(() => {
get('/news-feed').then(response => setNewsFeed(response.data));
}, []);
const [friends, setFriends] = useState(null);
useEffect(() => {
get('/friends').then(response => setFriends(response.data));
}, []);
return (
<div className='App'>
<h1>My Network</h1>
{!menu ? <p>Loading..</p> : (
<nav>
{menu.map((menuItem) => (
<button key={menuItem}>{menuItem}</button>
))}
</nav>
)}
<div className='content'>
{!newsFeed ? <p>Loading..</p> : (
<section>
{newsFeed.map(({ id, title, message, imgSrc }) => (
<article key={id}>
<h3>{title}</h3>
<p>{message}</p>
<img src={imgSrc} alt='' />
</article>
))}
</section>
)}
{!friends ? <p>Loading..</p> : (
<aside>
<ul>
{friends
.sort((a, b) => (a.isOnline && !b.isOnline ? -1 : 0))
.map(({ id, name, isOnline }) => (
<li key={id} className={isOnline ? 'online' : 'offline'}>
{name}
</li>
))}
</ul>
</aside>
)}
</div>
</div>
);
}