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
6 changes: 6 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous"
/>
<title>LAB | React IronContacts</title>
</head>
<body>
Expand Down
42 changes: 0 additions & 42 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,42 +0,0 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
104 changes: 102 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,109 @@
import "./App.css";
import contactsData from "./contacts.json";
import { useState } from "react";

function App() {
const [contacts, setContacts] = useState(contactsData.slice(0, 5));

const handleAddRandomContact = () => {
const remainingContacts = contactsData.filter(
(contact) =>
!contacts.some((existingContact) => existingContact.id === contact.id)
);

if (remainingContacts.length === 0) return;

const randomContact =
remainingContacts[Math.floor(Math.random() * remainingContacts.length)];
setContacts([...contacts, randomContact]);
};

const handleSortByPopularity = () => {
const sortedContacts = [...contacts];
sortedContacts.sort((a, b) => b.popularity - a.popularity);
setContacts(sortedContacts);
};

const handleSortByName = () => {
const sortedContacts = [...contacts];
sortedContacts.sort((a, b) => a.name.localeCompare(b.name));
setContacts(sortedContacts);
};

const handleDeleteContact = (id) => {
const updatedContacts = contacts.filter((contact) => contact.id !== id);
setContacts(updatedContacts);
};

return (
<div className="App">
<h1>LAB | React IronContacts</h1>
<div
className="container d-flex justify-content-center align-items-center"
style={{ minHeight: "100vh" }}
>
<div className="p-5 w-100">
<h1 className="text-center mb-4">IronContacts</h1>
<div className="buttons mb-4">
<button
onClick={handleAddRandomContact}
className="btn btn-primary me-2"
>
Add Random Contact
</button>
<button
onClick={handleSortByPopularity}
className="btn btn-secondary me-2"
>
Sort by popularity
</button>
<button onClick={handleSortByName} className="btn btn-secondary">
Sort by name
</button>
</div>

<div className="table-responsive">
<table className="table table-bordered text-center">
<thead>
<tr>
<th scope="col">Picture</th>
<th scope="col">Name</th>
<th scope="col">Popularity</th>
<th scope="col">Won Oscar</th>
<th scope="col">Won Emmy</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
{contacts.map((contact) => (
<tr key={contact.id}>
<td>
<img
src={contact.pictureUrl}
alt={contact.name}
style={{
maxWidth: "100px",
height: "auto",
borderRadius: "10px",
}}
/>
</td>
<td>{contact.name}</td>
<td>{contact.popularity.toFixed(2)}</td>
<td>{contact.wonOscar ? "🏆" : ""}</td>
<td>{contact.wonEmmy ? "🌟" : ""}</td>
<td>
<button
onClick={() => handleDeleteContact(contact.id)}
className="btn btn-danger btn-sm"
>
Delete
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
}
Expand Down