Completed 2.18 (Phonebook Step 10)

This commit is contained in:
2021-09-18 09:55:44 +07:00
parent 81be82c0e8
commit 5fe3b09201
4 changed files with 62 additions and 34 deletions
+24 -14
View File
@@ -1,24 +1,34 @@
{ {
"persons": [ "persons": [
{ {
"name": "Arto Hellas", "name": "32iuy4322",
"number": "040-123456", "number": "ekfwjlkfd",
"id": 1 "id": 1
}, },
{ {
"name": "Ada Lovelace", "name": "THuan",
"number": "39-44-5323523", "number": "22",
"id": 2
},
{
"name": "Dan Abramov",
"number": "12-43-234345",
"id": 3
},
{
"name": "Thuan",
"number": "0988289099",
"id": 5 "id": 5
},
{
"name": "Th",
"number": "32049324324",
"id": 6
},
{
"name": "TTTTT",
"number": "32432432432",
"id": 7
},
{
"name": "TTTTTT",
"number": "111111",
"id": 8
},
{
"name": "Huy",
"number": "987654321",
"id": 9
} }
] ]
} }
+30 -13
View File
@@ -19,7 +19,7 @@ const App = () => {
}) })
}, []) }, [])
//console.log(persons) console.log(persons)
const handleNameChange = (event) => { const handleNameChange = (event) => {
//console.log(event.target.value) //console.log(event.target.value)
@@ -38,24 +38,41 @@ const App = () => {
const submitChange = (event) => { const submitChange = (event) => {
event.preventDefault() event.preventDefault()
const newPerson = {
name: newName,
number: newNumber,
id: persons.length + 1
}
// Check if newName already added to phonebook or not // Check if newName already added to phonebook or not
const check = persons.filter(person => person.name === newName ) const check = persons.filter(person => person.name === newName )
check.length === 0 ? setPersons(persons.concat(newPerson)) : alert(`${newName} is already added to phonebook`) const id = check.length === 0 ? persons[persons.length - 1].id + 1 : check[0].id
if (check.length === 0) { const newPerson = {
Contact name: newName,
.updateContact(newPerson) number: newNumber,
.then(response => { id: id
console.log(response)
})
} }
if (check.length === 0) {
if ((newPerson.name === "") || (newPerson.number === "")) {
alert(`Name or number cannot be empty`)
}
else {
Contact
.addContact(newPerson)
.then(response => {
console.log(response)
})
setPersons(persons.concat(newPerson))
}
}
else {
const result = window.confirm(`${newName} is already added to phonebook. Replace with new Number?`)
console.log(id)
if (result) {
Contact
.updateContact(id, newPerson)
.then(response => {
setPersons(persons.map(person => person.id !== id ? person : response))
})
}
}
setNewNumber('') setNewNumber('')
setNewName('') setNewName('')
+1 -1
View File
@@ -2,7 +2,7 @@ import React from 'react'
const Person = ({data,click}) => { const Person = ({data,click}) => {
const clickHandler = (person) => { const clickHandler = (person) => {
const result = window.confirm("Do you want to continue?") const result = window.confirm(`Do you want to delete ${person.name}?`)
if (result) { if (result) {
click(person.id) click(person.id)
} }
+7 -6
View File
@@ -9,7 +9,7 @@ const getContact = () => {
} }
const updateContact = newPerson => { const addContact = newPerson => {
const contact = axios.post(baseUrl, newPerson) const contact = axios.post(baseUrl, newPerson)
return contact.then(response => response.data) return contact.then(response => response.data)
} }
@@ -19,13 +19,14 @@ const deleteContact = id => {
return contact.then(response => response.data) return contact.then(response => response.data)
} }
/* const updateContact = (id, newPerson) => {
const update = (id, newObject) => { const contact = axios.put(`${baseUrl}/${id}`, newPerson)
return axios.put(`${baseUrl}/${id}`, newObject) return contact.then(response => response.data)
}*/ }
const Contact = { const Contact = {
getContact, updateContact, deleteContact getContact, addContact, deleteContact, updateContact
} }
export default Contact export default Contact