From b5dce51a8516ac1ef05be044d59e2cb9626b854b Mon Sep 17 00:00:00 2001 From: Ibrahim Mkusa Date: Sun, 30 Apr 2023 11:43:07 -0400 Subject: ex2.7 alert user if contact already present --- part2/phonebook/src/App.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/part2/phonebook/src/App.js b/part2/phonebook/src/App.js index 2e430f9..2843875 100644 --- a/part2/phonebook/src/App.js +++ b/part2/phonebook/src/App.js @@ -17,15 +17,18 @@ const App = () => { ]) const [newName, setNewName] = useState('') - const addNumber = (event) => { + const addContact = (event) => { event.preventDefault() console.log(newName, persons) - const newPerson = { - name: newName, - id: persons.length + 1, - } + if (persons.find(person => person.name === newName)) { + alert(`${newName} is already added to phonebook`) + } else { + const newPerson = { + name: newName, + id: persons.length + 1, + } setPersons(persons.concat(newPerson)) - } + }} const handleTypedName = (event) => { console.log(event.target.value) @@ -36,7 +39,7 @@ const App = () => { return (

Phonebook

-
+
name:
-- cgit v1.2.3