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
40 changes: 11 additions & 29 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
.App {
text-align: center;
padding: 20px;
}

.App-logo {
height: 40vmin;
pointer-events: none;
table {
margin: 0 auto;
border-collapse: collapse;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
th, td {
border: 1px solid #ccc;
padding: 10px;
}

.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);
}
to {
transform: rotate(360deg);
}
button {
margin: 5px;
padding: 6px 12px;
cursor: pointer;
}
103 changes: 87 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,94 @@
import logo from './logo.svg';
import './App.css';
// src/App.js

import "./App.css";
import { useState } from "react";
import contactsData from "./contacts.json";

function App() {
// Iteration 1: store first 5 contacts in state
const [contacts, setContacts] = useState(contactsData.slice(0, 5));

// Iteration 3: add random contact
const addRandomContact = () => {
const remainingContacts = contactsData.filter(
(contact) => !contacts.some((c) => c.id === contact.id)
);

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

const randomIndex = Math.floor(Math.random() * remainingContacts.length);
const randomContact = remainingContacts[randomIndex];

setContacts([...contacts, randomContact]);
};

// Iteration 4: sort by name
const sortByName = () => {
const sorted = [...contacts].sort((a, b) =>
a.name.localeCompare(b.name)
);
setContacts(sorted);
};

// Iteration 4: sort by popularity
const sortByPopularity = () => {
const sorted = [...contacts].sort(
(a, b) => b.popularity - a.popularity
);
setContacts(sorted);
};

// Iteration 5: delete contact
const deleteContact = (id) => {
const filtered = contacts.filter((contact) => contact.id !== id);
setContacts(filtered);
};

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>
<h1>IronContacts</h1>

<div className="buttons">
<button onClick={addRandomContact}>Add Random Contact</button>
<button onClick={sortByName}>Sort by Name</button>
<button onClick={sortByPopularity}>Sort by Popularity</button>
</div>

<table>
<thead>
<tr>
<th>Picture</th>
<th>Name</th>
<th>Popularity</th>
<th>Won an Oscar</th>
<th>Won an Emmy</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
{contacts.map((contact) => (
<tr key={contact.id}>
<td>
<img
src={contact.pictureUrl}
alt={contact.name}
height="100"
/>
</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)}>
Delete
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
Expand Down