From f18a52307760f7d9b91d9399949ec79eceb892bd Mon Sep 17 00:00:00 2001 From: Ibrahim Mkusa Date: Tue, 2 May 2023 19:54:15 -0400 Subject: ex2.12 store contacts locally and on a local database --- part2/phonebook/db.json | 7 ++++++- part2/phonebook/src/App.js | 13 +++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/part2/phonebook/db.json b/part2/phonebook/db.json index b4a4ccd..f12c730 100644 --- a/part2/phonebook/db.json +++ b/part2/phonebook/db.json @@ -19,6 +19,11 @@ "name": "Mary Poppendieck", "number": "39-23-6423122", "id": 4 + }, + { + "name": "Majaliwa Mtumishi", + "id": 5, + "number": "781-598-1070" } ] -} +} \ No newline at end of file diff --git a/part2/phonebook/src/App.js b/part2/phonebook/src/App.js index 7edf5a8..b8de8a6 100644 --- a/part2/phonebook/src/App.js +++ b/part2/phonebook/src/App.js @@ -31,19 +31,15 @@ const Filter = (props) => { } const App = () => { - //const [persons, setPersons] = useState([ - //{ name: 'Arto Hellas', number: '000-000-0000', id: 1 }, - //{ name: 'John F Kennedy', number: '1-800-NASA', id: 2 }, - //{ name: 'Julius K Nyerere', number: '1-800-UHURU', id: 3 } - //]) const [persons, setPersons] = useState([]) const [newName, setNewName] = useState('') const [number, setPhoneNumber] = useState('') const [searchTerm, setSearchTerm] = useState('') + const baseURL = 'http://localhost:3001/persons' useEffect(() => { axios - .get('http://localhost:3001/persons') + .get(baseURL) .then(response => { console.log('promise fulfilled') setPersons(response.data) @@ -62,6 +58,11 @@ const App = () => { id: persons.length + 1, // eslint-disable-next-line number: number } + // store new person locally and on the local server + axios + .post(baseURL, newPerson) + .then(response => + console.log(response)) setPersons(persons.concat(newPerson)) } } -- cgit v1.2.3