From fe8ea2c3c166b3b1cff3a8d4e63ad69caaf28bb7 Mon Sep 17 00:00:00 2001 From: RalucaElenaB <128471649+RalucaElenaB@users.noreply.github.com> Date: Sat, 18 Nov 2023 12:29:00 +0200 Subject: [PATCH] commit --- package-lock.json | 4 +- src/components/App.jsx | 81 ++++++++++++++++++---- src/components/ContactForm/ContactForm.jsx | 70 +++++++++++++++++++ src/components/ContactList/ContactList.jsx | 14 ++++ src/components/Filter/Filter.jsx | 12 ++++ src/index.css | 79 +++++++++++++++++++-- 6 files changed, 242 insertions(+), 18 deletions(-) create mode 100644 src/components/ContactForm/ContactForm.jsx create mode 100644 src/components/ContactList/ContactList.jsx create mode 100644 src/components/Filter/Filter.jsx diff --git a/package-lock.json b/package-lock.json index a1d4f24..972298e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "react-homework-template", + "name": "goit-react-hw-03-phonebook", "version": "0.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "react-homework-template", + "name": "goit-react-hw-03-phonebook", "version": "0.1.0", "dependencies": { "@testing-library/jest-dom": "^5.16.3", diff --git a/src/components/App.jsx b/src/components/App.jsx index 49bdb93..b01063e 100644 --- a/src/components/App.jsx +++ b/src/components/App.jsx @@ -1,16 +1,73 @@ +import { useState } from 'react'; +import ContactForm from './ContactForm/ContactForm'; +import ContactList from './ContactList/ContactList'; +import Filter from './Filter/Filter'; +import { nanoid } from 'nanoid'; + +const initialContacts = [ + { id: 'id-1', name: 'Rosie Simpson', number: '459-12-56' }, + { id: 'id-2', name: 'Hermione Kline', number: '443-89-12' }, + { id: 'id-3', name: 'Eden Clements', number: '645-17-79' }, + { id: 'id-4', name: 'Annie Copeland', number: '227-91-26' }, +]; + export const App = () => { + const [contacts, setContacts] = useState({ + contacts: initialContacts, + filter: '', + name: '', + number: '', + }); + + const handleOnDeleteContact = id => { + setContacts(prevContacts => ({ + ...prevContacts, + contacts: prevContacts.contacts.filter(contact => contact.id !== id), + })); + }; + + const handleFilterChange = event => { + const filterValue = event.target.value.toLowerCase(); + setContacts(prevContacts => ({ ...prevContacts, filter: filterValue })); + }; + + const filteredContacts = contacts.contacts.filter(contact => + contact.name.toLowerCase().includes(contacts.filter) + ); + + const handleAddContact = newContact => { + const isDuplicate = contacts.contacts.some( + contact => contact.name.toLowerCase() === newContact.name.toLowerCase() + ); + + if (isDuplicate) { + alert(`${newContact.name} is already in contacts`); + } else { + setContacts(prevContacts => ({ + ...prevContacts, + contacts: [...prevContacts.contacts, { ...newContact, id: nanoid() }], + })); + } + }; + return ( -
- React homework template!!!!!! -
+
+

Phonebook

+
+

Add a contact

+ +
+
+

Search a contact

+ +
+
+

Contacts list

+ +
+
); }; diff --git a/src/components/ContactForm/ContactForm.jsx b/src/components/ContactForm/ContactForm.jsx new file mode 100644 index 0000000..6345e38 --- /dev/null +++ b/src/components/ContactForm/ContactForm.jsx @@ -0,0 +1,70 @@ +import { useState } from 'react'; + +const ContactForm = ({ onAddContact }) => { + const [formData, setFormData] = useState({ + name: '', + number: '', + }); + + const handleInputChange = event => { + const { name, value } = event.target; + setFormData(prevFormData => ({ + ...prevFormData, + [name]: value, + })); + }; + + const handleFormSubmit = event => { + event.preventDefault(); + if (formData.name.trim() === '' || formData.number.trim() === '') { + alert('Te rog completează toate câmpurile!'); + return; + } + onAddContact(formData); + + setFormData({ + name: '', + number: '', + }); + }; + + return ( +
+ +
+ +
+ +
+ ); +}; + +export default ContactForm; + + diff --git a/src/components/ContactList/ContactList.jsx b/src/components/ContactList/ContactList.jsx new file mode 100644 index 0000000..de8df1e --- /dev/null +++ b/src/components/ContactList/ContactList.jsx @@ -0,0 +1,14 @@ +const ContactList = ({ contacts, onDeleteContact }) => { + return ( + + ); +}; + +export default ContactList; diff --git a/src/components/Filter/Filter.jsx b/src/components/Filter/Filter.jsx new file mode 100644 index 0000000..26d83f2 --- /dev/null +++ b/src/components/Filter/Filter.jsx @@ -0,0 +1,12 @@ +const Filter = ({ value, onChange }) => { + return ( + + ); +}; + +export default Filter; diff --git a/src/index.css b/src/index.css index 1aac5f6..9d3b0f0 100644 --- a/src/index.css +++ b/src/index.css @@ -1,15 +1,86 @@ -@import-normalize; /* bring in normalize.css styles */ +@import-normalize; + +#root { + max-width: 1280px; + margin: 0 auto; + padding: 2rem; + text-align: center; +} body { margin: 0; + display: flex; + place-items: center; + /* min-width: 320px; + min-height: 100vh; */ + background-color: rgb(239, 239, 229); 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; + font-size: 24px; + border-radius: 25px; +} + +ul { + list-style-type: none; +} + +Button, +input { + background-color: rgb(215, 226, 153); + font-size: 20px; + border-radius: 25px; + color: black; + padding: 12px 10px; + text-align: left; + text-decoration: none; + margin: 4px 10px; + cursor: pointer; + border: 2px solid #333; +} + +Button:hover { + background-color: rgb(166, 189, 102); +} + +section { + background-color: rgb(185, 185, 174); + border-radius: 25px; + padding: 5px; + font-weight: bold; + border-style: groove; + border: 7px solid #333; +} + +h1 { + text-shadow: 4px 2px 0 #000; +} + +.AddContacts { + background-color: rgb(243, 241, 219); + border-radius: 25px; + border: 3px solid #333; +} + +.SearchContacts { + background-color: rgb(236, 247, 216); + padding: 2px; + border-radius: 25px; + border: 3px solid #333; +} + +.ContactsList { + background-color: rgb(215, 227, 173); + padding: 2px; + border-radius: 25px; + border: 3px solid #333; } -code { - font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', - monospace; +.SubmitButton { + background-color: rgb(160, 167, 165); + font-size: 14px; + font-weight: bold; + border: 3px solid #333; }