Skip to content
Open

done #40

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
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}
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;
}
81 changes: 81 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
import logo from './logo.svg';
import './App.css';
import React, { useState } from 'react';
import contactsData from './contacts.json';

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

const [displayedContacts, setDisplayedContacts] = 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];

setDisplayedContacts((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);
setDisplayedContacts(updatedDisplayedContacts);

const deletedContact = displayedContacts.find((contact) => contact.id === id);
if (deletedContact) {
setRemainingContacts((prevRemaining) => [...prevRemaining, deletedContact]);
}
};

const sortByName = () => {
const sortedContacts = [...displayedContacts].sort((a, b) =>
a.name.localeCompare(b.name)
);
setDisplayedContacts(sortedContacts);
};

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

return (
<div className="App">
<header className="App-header">
Expand All @@ -18,6 +65,40 @@ function App() {
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