Skip to content
Open

done #56

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
80 changes: 64 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,71 @@
import logo from './logo.svg';
import './App.css';
import React, { useState } from "react";
import "./App.css";
import contactsData from "./contacts.json";

function App() {
// Step 1: Initialize contacts with the first 5 contacts from contacts.json
const [contacts, setContacts] = useState(contactsData.slice(0, 5));

// Step 3: Function to add a random contact from the remaining contacts
const addRandomContact = () => {
const remainingContacts = contactsData.filter(
(contact) => !contacts.includes(contact)
);
const randomContact =
remainingContacts[Math.floor(Math.random() * remainingContacts.length)];
setContacts([...contacts, randomContact]);
};

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

// Step 4: Function to sort contacts by popularity (highest first)
const sortByPopularity = () => {
setContacts([...contacts].sort((a, b) => b.popularity - a.popularity));
};

// Step 5: Function to remove a contact by id
const removeContact = (id) => {
setContacts(contacts.filter((contact) => contact.id !== id));
};

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>Contact Management</h1>
<button onClick={addRandomContact}>Add Random Contact</button>
<button onClick={sortByName}>Sort by Name</button>
<button onClick={sortByPopularity}>Sort by Popularity</button>

<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>
{contacts.map((contact) => (
<tr key={contact.id}>
<td>
<img src={contact.pictureUrl} alt={contact.name} width="50" />
</td>
<td>{contact.name}</td>
<td>{contact.popularity}</td>
<td>{contact.wonOscar ? "🏆" : ""}</td>
<td>{contact.wonEmmy ? "🏆" : ""}</td>
<td>
<button onClick={() => removeContact(contact.id)}>Delete</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
Expand Down