generated from agiledev-students-fall2023/generic-project-repository
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from agiledev-students-fall2023/joyc7
Implemented the Friends Page
- Loading branch information
Showing
4 changed files
with
219 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
.page-title { | ||
font-size: 2em; | ||
text-align: center; | ||
margin-bottom: 20px; | ||
} | ||
|
||
.balance-section { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
background-color: #E5E5E5; | ||
padding: 15px; | ||
border-radius: 10px; | ||
margin-bottom: 20px; | ||
} | ||
|
||
.avatar { | ||
border-radius: 50%; | ||
width: 50px; | ||
height: 50px; | ||
margin-right: 20px; | ||
} | ||
|
||
.balance-title { | ||
font-weight: bold; | ||
margin-bottom: 10px; | ||
} | ||
|
||
.balance-details { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.friends-list h3 { | ||
margin-top: 20px; | ||
margin-bottom: 10px; | ||
} | ||
|
||
.friend-item { | ||
display: flex; | ||
align-items: center; | ||
gap: 10px; | ||
} | ||
|
||
.friend-avatar { | ||
border-radius: 50%; | ||
margin-right: 10px; | ||
width: 50px; | ||
height: 50px; | ||
} | ||
|
||
.positive-balance { | ||
color: green; | ||
} | ||
|
||
.negative-balance { | ||
color: red; | ||
} | ||
|
||
|
||
.navbar { | ||
position: absolute; | ||
top: 10px; | ||
left: 10px; | ||
background-color: #E5E5E5; | ||
padding: 5px 10px; | ||
border-radius: 5px; | ||
} | ||
|
||
.add-friends-btn { | ||
position: absolute; | ||
top: 10px; | ||
right: 10px; | ||
padding: 5px 10px; | ||
background-color: #007BFF; | ||
color: white; | ||
border: none; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
} | ||
|
||
.modal { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
background-color: rgba(0, 0, 0, 0.5); | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
} | ||
|
||
.modal-content { | ||
background-color: white; | ||
padding: 20px; | ||
border-radius: 10px; | ||
width: 300px; | ||
position: relative; | ||
} | ||
|
||
.close { | ||
position: absolute; | ||
right: 10px; | ||
top: 5px; | ||
cursor: pointer; | ||
} | ||
|
||
.modal-content h2 { | ||
margin-top: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import './FriendsPage.css'; | ||
|
||
function FriendsPage() { | ||
const [userData, setUserData] = useState(null); | ||
const [showModal, setShowModal] = useState(false); | ||
|
||
const backupData = {"id":1,"name":"Carmon Stickel","email":"[email protected]","phone":"574-505-0071","avatar":"https://robohash.org/expeditaautemlaudantium.png?size=50x50\u0026set=set1","friends":[{"id":4,"name":"Leeanne Dunrige","email":"[email protected]","phone":"594-821-5418","balance":"$62.71"},{"id":2,"name":"Doti Sorrel","email":"[email protected]","phone":"852-501-6560","balance":"$55.97"},{"id":5,"name":"Hobart Sallan","email":"[email protected]","phone":"476-640-9589","balance":"$-36.35"},{"id":3,"name":"Anetta Heningam","email":"[email protected]","phone":"180-587-2730","balance":"$-54.82"}]}; | ||
|
||
useEffect(() => { | ||
async function fetchData() { | ||
try { | ||
const response = await fetch("https://my.api.mockaroo.com/users.json?key=04d5ee10"); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! Status: ${response.status}`); | ||
} | ||
const data = await response.json(); | ||
setUserData(data); | ||
} catch (error) { | ||
console.error("Error fetching user data:", error); | ||
console.log(backupData); | ||
// Setting backup data when API call fails | ||
setUserData(backupData); | ||
} | ||
} | ||
fetchData(); | ||
}, []); | ||
|
||
if (!userData) return <div>Loading...</div>; | ||
|
||
const totalBalance = userData.friends && userData.friends.length ? userData.friends.reduce((acc, friend) => acc + parseFloat(friend.balance.replace('$', '')), 0) : 0; | ||
|
||
return ( | ||
<div className="friends-page"> | ||
<h1 className="page-title">Friends</h1> | ||
<div className="navbar"> | ||
Navigation Bar | ||
</div> | ||
<button className="add-friends-btn" onClick={() => setShowModal(true)}> | ||
Add Friends | ||
</button> | ||
<div className="balance-section"> | ||
<img src={userData.avatar} alt="User Avatar" className="avatar"/> | ||
<div> | ||
<div className="balance-title">Total balance</div> | ||
<div className="balance-details"> | ||
{totalBalance > 0 ? ( | ||
<div>You are owed ${totalBalance.toFixed(2)}</div> | ||
) : ( | ||
<div>You owe ${Math.abs(totalBalance).toFixed(2)}</div> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div className="friends-list"> | ||
<ul> | ||
{userData.friends.map((friend) => ( | ||
<li key={friend.id} className="friend-item"> | ||
<img src={`https://robohash.org/${friend.name}.png?size=50x50&set=set1`} alt={`${friend.name}'s avatar`} className="friend-avatar"/> | ||
<span>{friend.name}</span> | ||
<span className={parseFloat(friend.balance.replace('$', '')) < 0 ? "negative-balance" : "positive-balance"}> | ||
{friend.balance} | ||
</span> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
|
||
|
||
{showModal && ( | ||
<div className="modal"> | ||
<div className="modal-content"> | ||
<span className="close" onClick={() => setShowModal(false)}>×</span> | ||
<h2>Add Friends</h2> | ||
<input type="text" placeholder="Email or Phone" /> | ||
<button>Search</button> | ||
</div> | ||
</div> | ||
)} | ||
|
||
</div> | ||
); | ||
} | ||
|
||
export default FriendsPage; |