Skip to content

Commit

Permalink
added 2.19-2.20 - phonebook
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickSalmi committed Dec 30, 2022
1 parent 47b0555 commit 69159ba
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
15 changes: 10 additions & 5 deletions part2/phonebook/db.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
{
"persons": [
{
"name": "Patrick Salmi",
"number": "555555555",
"id": 8
},
{
"name": "Arto Hellas",
"number": "040-1231242",
"id": 9
},
{
"name": "Ada Lovelace",
"number": "123123123",
"id": 10
},
{
"name": "asdasd",
"number": "asdasd",
"id": 11
}
]
}
19 changes: 17 additions & 2 deletions part2/phonebook/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import Persons from './components/Persons'
import Filter from './components/Filter'
import PersonForm from './components/PersonForm'
import personService from './services/persons'
import {Notification, Error} from './components/Notification'

const App = () => {
const [persons, setPersons] = useState([])
const [newName, setNewName] = useState("")
const [newNumber, setNewNumber] = useState("")
const [filter, setfilter] = useState("")
const [notification, setNotification] = useState(null)
const [errorMessage, setErrorMessage] = useState(null)

const handleNameChange = (event) => {
setNewName(event.target.value)
Expand Down Expand Up @@ -41,6 +44,10 @@ const App = () => {
.create(personObject)
.then(returnedPerson => {
setPersons(persons.concat(returnedPerson))
setNotification(`Added ${newName}`)
setTimeout(() => {
setNotification(null)
}, 3000)
})

}
Expand All @@ -58,7 +65,10 @@ const App = () => {
setPersons(persons.map(person => person.id !== id ? person : returnedPerson))
})
.catch(error => {
alert(`${person.name} was already deleted from the server`)
setErrorMessage(`Information of ${person.name} has already been removed from the server`)
setTimeout(() => {
setErrorMessage(null)
}, 3000)
setPersons(persons.filter(p => p.id !== id))
})

Expand All @@ -78,7 +88,10 @@ const App = () => {
personService
.remove(id)
.catch(error => {
alert(`${person.name} was already deleted from the server`)
setErrorMessage(`Information of ${person.name} has already been removed from the server`)
setTimeout(() => {
setErrorMessage(null)
}, 3000)
})
setPersons(persons.filter(p => p.id !== id))
}
Expand All @@ -87,6 +100,8 @@ const App = () => {
return (
<div>
<h2>Phonebook</h2>
<Notification message={notification}/>
<Error message={errorMessage}/>
<Filter filter={filter} handleFilter={handleFilter} />
<h2>Add a new</h2>
<PersonForm
Expand Down
47 changes: 47 additions & 0 deletions part2/phonebook/src/components/Notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const Notification = ({ message }) => {
const notificationStyle = {
color: 'green',
background: 'lightgrey',
fontSize: 20,
borderStyle: 'solid',
borderRadius: 5,
padding: 10,
marginBottom: 10
}

if (message === null) {
return null
}

return (
<div style={notificationStyle}>
{message}
</div>
)
}

const Error = ({ message }) => {
const notificationStyle = {
color: 'red',
background: 'lightgrey',
fontSize: 20,
borderStyle: 'solid',
borderRadius: 5,
padding: 10,
marginBottom: 10
}

if (message === null) {
return null
}

return (
<div style={notificationStyle}>
{message}
</div>
)
}



export { Notification, Error }

0 comments on commit 69159ba

Please sign in to comment.