From b3f53a58b5c20e3288a0a2df7e16459b35b7b3ee Mon Sep 17 00:00:00 2001 From: Muhammad Irshad <85778819+Irshadmdk19@users.noreply.github.com> Date: Fri, 22 Nov 2024 20:01:04 +0530 Subject: [PATCH] Done all iterations --- src/App.js | 57 ++++++++++++++++++++++++---------- src/components/Buttons.js | 55 ++++++++++++++++++++++++++++++++ src/components/ContactRow.js | 37 ++++++++++++++++++++++ src/components/ContactTable.js | 38 +++++++++++++++++++++++ src/index.css | 13 -------- 5 files changed, 170 insertions(+), 30 deletions(-) create mode 100644 src/components/Buttons.js create mode 100644 src/components/ContactRow.js create mode 100644 src/components/ContactTable.js diff --git a/src/App.js b/src/App.js index 3784575..cc42754 100644 --- a/src/App.js +++ b/src/App.js @@ -1,23 +1,46 @@ -import logo from './logo.svg'; -import './App.css'; +import React, { useState } from "react"; +import contacts from "./contacts.json"; +import ContactTable from "./components/ContactTable"; +import Buttons from "./components/Buttons"; function App() { + const [contactList, setContactList] = useState(contacts.slice(0, 5)); + + function addRandomContact() { + const remainingContacts = contacts.filter( + (contact) => !contactList.includes(contact) + ); + if (remainingContacts.length > 0) { + const randomContact = + remainingContacts[Math.floor(Math.random() * remainingContacts.length)]; + setContactList([...contactList, randomContact]); + } + } + + function sortByName() { + const sorted = [...contactList].sort((a, b) => a.name.localeCompare(b.name)); + setContactList(sorted); + } + + function sortByPopularity() { + const sorted = [...contactList].sort((a, b) => b.popularity - a.popularity); + setContactList(sorted); + } + + function deleteContact(id) { + const filteredContacts = contactList.filter((contact) => contact.id !== id); + setContactList(filteredContacts); + } + return ( -
-
- logo -

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

- - Learn React - -
+
+

IronContacts

+ +
); } diff --git a/src/components/Buttons.js b/src/components/Buttons.js new file mode 100644 index 0000000..4a6edf5 --- /dev/null +++ b/src/components/Buttons.js @@ -0,0 +1,55 @@ +import React from "react"; + +function Buttons({ onAddRandom, onSortByName, onSortByPopularity }) { + return ( +
+ + + +
+ ); +} + +export default Buttons; diff --git a/src/components/ContactRow.js b/src/components/ContactRow.js new file mode 100644 index 0000000..8fddd24 --- /dev/null +++ b/src/components/ContactRow.js @@ -0,0 +1,37 @@ +import React from "react"; + +function ContactRow({ contact, onDelete }) { + return ( + + + {contact.name} + + {contact.name} + {contact.popularity.toFixed(2)} + {contact.wonOscar ? "🏆" : ""} + {contact.wonEmmy ? "🏆" : ""} + + + + + ); +} + +export default ContactRow; diff --git a/src/components/ContactTable.js b/src/components/ContactTable.js new file mode 100644 index 0000000..a851063 --- /dev/null +++ b/src/components/ContactTable.js @@ -0,0 +1,38 @@ +import React from "react"; +import ContactRow from "./ContactRow"; + +function ContactTable({ contacts, onDelete }) { + return ( + + + + + + + + + + + + + {contacts.map((contact) => ( + + ))} + +
+ Picture + + Name + + Popularity + + Won an Oscar + + Won an Emmy + + Actions +
+ ); +} + +export default ContactTable; diff --git a/src/index.css b/src/index.css index ec2585e..e69de29 100644 --- a/src/index.css +++ b/src/index.css @@ -1,13 +0,0 @@ -body { - margin: 0; - font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', - 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', - sans-serif; - -webkit-font-smoothing: antialiased; - -moz-osx-font-smoothing: grayscale; -} - -code { - font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', - monospace; -}