diff options
author | Ibrahim Mkusa <ibrahimmkusa@gmail.com> | 2023-04-30 11:43:07 -0400 |
---|---|---|
committer | Ibrahim Mkusa <ibrahimmkusa@gmail.com> | 2023-04-30 11:43:07 -0400 |
commit | b5dce51a8516ac1ef05be044d59e2cb9626b854b (patch) | |
tree | 6c825f39a73cf070c10661c6fbb804ab0ac5c9de /part2 | |
parent | 0a53877f5d13054b1d0baa4c2120b50673c534bd (diff) |
ex2.7 alert user if contact already present
Diffstat (limited to 'part2')
-rw-r--r-- | part2/phonebook/src/App.js | 17 |
1 files 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 ( <div> <h2>Phonebook</h2> - <form onSubmit={addNumber}> + <form onSubmit={addContact}> <div> name: <input value={newName} onChange={handleTypedName} /> </div> |