extract service methods
This commit is contained in:
parent
dfeec4d353
commit
71de2807f4
2 changed files with 50 additions and 10 deletions
|
|
@ -29,6 +29,41 @@
|
|||
"id": "bbb",
|
||||
"name": "bbb",
|
||||
"number": "333"
|
||||
},
|
||||
{
|
||||
"id": "",
|
||||
"name": "",
|
||||
"number": ""
|
||||
},
|
||||
{
|
||||
"id": "",
|
||||
"name": "",
|
||||
"number": ""
|
||||
},
|
||||
{
|
||||
"id": "",
|
||||
"name": "",
|
||||
"number": ""
|
||||
},
|
||||
{
|
||||
"id": "",
|
||||
"name": "",
|
||||
"number": ""
|
||||
},
|
||||
{
|
||||
"id": "",
|
||||
"name": "",
|
||||
"number": ""
|
||||
},
|
||||
{
|
||||
"id": "",
|
||||
"name": "",
|
||||
"number": ""
|
||||
},
|
||||
{
|
||||
"id": "ccc",
|
||||
"name": "ccc",
|
||||
"number": "333"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -41,8 +41,19 @@ const App = () => {
|
|||
const [newName, setNewName] = useState("");
|
||||
const [newNumber, setNewNumber] = useState("");
|
||||
|
||||
const getPersons = () => {
|
||||
return axios.get("http://localhost:3001/persons");
|
||||
};
|
||||
const addPerson = () => {
|
||||
return axios.post("http://localhost:3001/persons", {
|
||||
name: newName,
|
||||
number: newNumber,
|
||||
id: newName,
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
axios.get("http://localhost:3001/persons").then((response) => {
|
||||
getPersons().then((response) => {
|
||||
console.log(response.data);
|
||||
setPersons(response.data);
|
||||
});
|
||||
|
|
@ -59,15 +70,9 @@ const App = () => {
|
|||
return;
|
||||
}
|
||||
|
||||
axios
|
||||
.post("http://localhost:3001/persons", {
|
||||
name: newName,
|
||||
number: newNumber,
|
||||
id: newName,
|
||||
})
|
||||
.then((response) => {
|
||||
setPersons(persons.concat(response.data));
|
||||
});
|
||||
addPerson().then((response) => {
|
||||
setPersons(persons.concat(response.data));
|
||||
});
|
||||
setNewName("");
|
||||
setNewNumber("");
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue