From d1cfd1e53ef0675f63cd4b393154c6a1c29879f7 Mon Sep 17 00:00:00 2001 From: Sahil Date: Fri, 7 Jun 2024 00:29:01 +0530 Subject: [PATCH] 1st done --- package-lock.json | 2 +- package.json | 2 +- src/App.css | 110 ++++++++++++++++++++++++++++++++++------------ src/App.js | 96 ++++++++++++++++++++++++++++++++++------ 4 files changed, 166 insertions(+), 44 deletions(-) diff --git a/package-lock.json b/package-lock.json index 37cdec8..743e3e3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "@testing-library/user-event": "^13.5.0", "react": "^18.2.0", "react-dom": "^18.2.0", - "react-scripts": "5.0.1", + "react-scripts": "^5.0.1", "web-vitals": "^2.1.4" } }, diff --git a/package.json b/package.json index 216788b..62f5cdd 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "@testing-library/user-event": "^13.5.0", "react": "^18.2.0", "react-dom": "^18.2.0", - "react-scripts": "5.0.1", + "react-scripts": "^5.0.1", "web-vitals": "^2.1.4" }, "scripts": { diff --git a/src/App.css b/src/App.css index 74b5e05..da200ce 100644 --- a/src/App.css +++ b/src/App.css @@ -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; + } \ No newline at end of file diff --git a/src/App.js b/src/App.js index 3784575..24408f2 100644 --- a/src/App.js +++ b/src/App.js @@ -1,23 +1,91 @@ + import logo from './logo.svg'; import './App.css'; +import contacts from "./contacts.json"; +import React,{ useState } from 'react'; function App() { + const initialContacts = contacts.slice(0,5); + const initialRemainingContacts = contacts.slice(5); + + const[displayedContacts, setDisplayContacts] = 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]; + + setDisplayContacts((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); + setDisplayContacts(updatedDisplayedContacts); + + const deleteContact = displayedContacts.find((contact) => contact.id === id); + if(deleteContact) { + setRemainingContacts((prevRemaining) => [...prevRemaining, deleteContact]); + } + }; + const sortByName = () => { + const sortedContacts = [...displayedContacts].sort((a, b) => + a.name.localeCompare(b.name) + ); + setDisplayContacts(sortedContacts); + }; + + const sortByPopularity = () => { + const sortedContacts = [...displayedContacts].sort((a, b) => b.popularity - a.popularity); + setDisplayContacts(sortedContacts); + }; + return (
-
- logo -

- Edit src/App.js and save to reload. -

- - Learn React - -
+ +
+

Contact List

+ + + + + + + + + + + + + + + + {displayedContacts.map((contact) => ( + + + + + + + + + ))} + +
PictureNamePopularityWon an OscarWon an EmmyAction
+ {contact.name} + {contact.name}{contact.popularity.toFixed(2)}{contact.wonOscar ? '🏆' : '-'}{contact.wonEmmy ? '🏆' : '-'} + +
+
); }