diff options
author | Ibrahim Mkusa <ibrahimmkusa@gmail.com> | 2023-05-02 19:54:15 -0400 |
---|---|---|
committer | Ibrahim Mkusa <ibrahimmkusa@gmail.com> | 2023-05-02 19:54:15 -0400 |
commit | f18a52307760f7d9b91d9399949ec79eceb892bd (patch) | |
tree | 642de7f8177b1334a045b61309a6b925c72e68da | |
parent | 0f337367c966be01c6601b62897aa981534fb155 (diff) |
ex2.12 store contacts locally and on a local database
-rw-r--r-- | part2/phonebook/db.json | 7 | ||||
-rw-r--r-- | 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)) } } |