Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"bootstrap": "^5.3.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
Expand Down
86 changes: 70 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,77 @@
import logo from './logo.svg';
import './App.css';
import React, { useState } from "react";
import contacts from "./contacts.json";
import 'bootstrap/dist/css/bootstrap.min.css';


function App() {
const [contactList, setContactList] = useState(contacts.slice(0, 5));
const addRandomContact = () => {
const remainingContacts = contacts.filter(
(contact) => !contactList.includes(contact)
);
const randomContact =
remainingContacts[Math.floor(Math.random() * remainingContacts.length)];
setContactList([...contactList, randomContact]);
};
const sortByName = () => {
const sortedContacts = [...contactList].sort((a, b) =>
a.name.localeCompare(b.name)
);
setContactList(sortedContacts);
};

const sortByPopularity = () => {
const sortedContacts = [...contactList].sort(
(a, b) => b.popularity - a.popularity
);
setContactList(sortedContacts);
};
const deleteContact = (id) => {
const updatedContacts = contactList.filter((contact) => contact.id !== id);
setContactList(updatedContacts);
};


return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
<table className="table table-striped table-hover">
<thead className="thead-dark">
<tr>
<th>Picture</th>
<th>Name</th>
<th>Popularity</th>
<th>Won an Oscar</th>
<th>Won an Emmy</th>
</tr>
</thead>
<tbody>
{contactList.map((contact) => (
<tr key={contact.id}>
<td>
<img
src={contact.pictureUrl}
alt={contact.name}
width="50"
/>
</td>
<td>{contact.name}</td>
<td>{contact.popularity.toFixed(2)}</td>
<td>{contact.wonOscar ? "🏆" : ""}</td>
<td>{contact.wonEmmy ? "🏆" : ""}</td>
<td>
<button onClick={() => deleteContact(contact.id)}
className="btn btn-danger delete-button">Delete</button>
</td>
</tr>
))}
<button onClick={addRandomContact}
className="btn btn-success">Add Random Contact</button>
<button onClick={sortByName}
className="btn btn-primary">Sort by Name</button>
<button onClick={sortByPopularity}
className="btn btn-warning">Sort by Popularity</button>
</tbody>
</table>
</div>
);
}
Expand Down