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
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"react-scripts": "^5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
110 changes: 82 additions & 28 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,38 +1,92 @@
.App {
text-align: center;

}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
transform: rotate(360deg);
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
text-align: center;
}
to {
transform: rotate(360deg);

/* Button styles */
button {
margin: 5px;
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
}

button:hover {
background-color: #0056b3;
}

/* Table styles */
.contacts-table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}

.contacts-table th, .contacts-table td {
padding: 10px;
border-bottom: 1px solid #ccc;
}

.contacts-table th {
background-color: #f2f2f2;
font-weight: bold;
}

.contacts-table img {
max-width: 50px;
border-radius: 50%;
}


.contacts-table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}

.contacts-table th,
.contacts-table td {
padding: 10px;
border-bottom: 1px solid #ccc;
}

.contacts-table th {
background-color: #f2f2f2;
font-weight: bold;
}

.contacts-table img {
max-width: 50px;
border-radius: 50%;
}


.contacts-table .delete-btn {
margin: 0;
padding: 5px 10px;
background-color: #dc3545;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
transition: background-color 0.3s;
}

.contacts-table .delete-btn:hover {
background-color: #c82333;
}
96 changes: 82 additions & 14 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,91 @@

import logo from './logo.svg';
import './App.css';
import contacts from "./contacts.json";
import React,{ useState } from 'react';

function App() {
const initialContacts = contacts.slice(0,5);
const initialRemainingContacts = contacts.slice(5);

const[displayedContacts, setDisplayContacts] = useState(initialContacts);
const [remainingContacts, setRemainingContacts] = useState(initialRemainingContacts);

const addRandomContact = () => {
if(remainingContacts.length > 0) {
const randomIndex = Math.floor(Math.random() * remainingContacts.length);
const randomContact = remainingContacts[randomIndex];

setDisplayContacts((prevContacts) => [...prevContacts, randomContact]);

const updatedRemainingContacts = remainingContacts.filter(
(contact) => contact.id !== randomContact.id
);

setRemainingContacts(updatedRemainingContacts);
} else{
console.log('No more contacts to add!');
}
};

const deleteContact = (id) => {
const updatedDisplayedContacts = displayedContacts.filter((contact) => contact.id !== id);
setDisplayContacts(updatedDisplayedContacts);

const deleteContact = displayedContacts.find((contact) => contact.id === id);
if(deleteContact) {
setRemainingContacts((prevRemaining) => [...prevRemaining, deleteContact]);
}
};
const sortByName = () => {
const sortedContacts = [...displayedContacts].sort((a, b) =>
a.name.localeCompare(b.name)
);
setDisplayContacts(sortedContacts);
};

const sortByPopularity = () => {
const sortedContacts = [...displayedContacts].sort((a, b) => b.popularity - a.popularity);
setDisplayContacts(sortedContacts);
};

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>

<div className="container">
<h1>Contact List</h1>
<button onClick={addRandomContact}>Add Random Contact</button>
<button onClick={sortByName}>Sort by Name</button>
<button onClick={sortByPopularity}>Sort by Popularity</button>
<table className="contacts-table">
<thead>
<tr>
<th>Picture</th>
<th>Name</th>
<th>Popularity</th>
<th>Won an Oscar</th>
<th>Won an Emmy</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{displayedContacts.map((contact) => (
<tr key={contact.id}>
<td>
<img src={contact.pictureUrl} alt={contact.name} style={{ width: '50px' }} />
</td>
<td>{contact.name}</td>
<td>{contact.popularity.toFixed(2)}</td>
<td>{contact.wonOscar ? '🏆' : '-'}</td>
<td>{contact.wonEmmy ? '🏆' : '-'}</td>
<td>
<button className="delete-btn" onClick={() => deleteContact(contact.id)}>Delete</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
}
Expand Down