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
35 changes: 35 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.App {
text-align: center;

}

.App-logo {
Expand Down Expand Up @@ -36,3 +37,37 @@
transform: rotate(360deg);
}
}
img{
width: 200px;
height:300px
}
table{
width: 1100px;
margin-inline: auto;
border-collapse: collapse;
margin-top: 20px;
}
th, td{
font-size: 25px;
text-align: center;
vertical-align: bottom;
border-bottom: 1px solid #282c34;
padding:8px;
}
tr:nth-child(even){
background-color: chocolate;
}
tr:hover{
background-color:chartreuse;
}
button{
background-color: #04AA6D;
font-size: 16px;
padding: 14px 40px;
border-radius: 8px;
transition-duration: 0.4s;
margin-right: 30px;
}
button:hover{
color: white;
}
107 changes: 92 additions & 15 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,100 @@
import logo from './logo.svg';
import React, { useState } from 'react';
import './App.css';
import contacts from './contacts.json';

function App() {
const [contactsList, setContactsList] = useState(contacts.slice(0, 7));
const remainingContacts = contacts.slice(5);

//function to add a random contact
const addRandomContact = () => {
if (remainingContacts.length === 0) {
// Handling the case where there are no more remaining contacts
alert('No more remaining contacts');
return;
}

// Randomly selecting a contact from the remaining contacts
const randomIndex = Math.floor(Math.random() * remainingContacts.length);
const randomContact = remainingContacts[randomIndex];

// Adding the selected contact to the array in the state
setContactsList((prevContacts) => [...prevContacts, randomContact]);

// Removing the selected contact from the remaining contacts
const updatedRemainingContacts = [
...remainingContacts.slice(0, randomIndex),
...remainingContacts.slice(randomIndex + 1),
];

// Updating the state of remaining contacts if needed
// setRemainingContacts(updatedRemainingContacts);
};
//function to sort by name alphabetically
const sortByName = () => {
//create new array and updating state with sorted array
const sortedByName = [...contactsList].sort((a,b)=> a.name.localeCompare(b.name));
setContactsList(sortedByName);
}
//fucntion to sort by popularity in descending order
const sortByPopularity = () => {
//create new array and updating state with sorted array
const sortedByPopularity = [...contactsList].sort((a,b)=> b.popularity - a.popularity);
setContactsList(sortedByPopularity);
}

const removeContact = (id) =>{
//finding the index of the contact with the given ID
const contactIndex =contactsList.findIndex((contact) => contact.id === id);

if (contactIndex !== -1){
//creating a new array exluding the contact to be removed
const updateContactsList = [
...contactsList.slice(0, contactIndex),
...contactsList.slice(contactIndex + 1)
];

//updating the stae variable with the modified new array of contacts list
setContactsList(updateContactsList)
}


}

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>Contacts List</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>
{contactsList.map((contact, index) => (
<tr key={index}>
<td>
<img src={contact.pictureUrl} alt={contact.name} />
</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