diff --git a/parts/2/renderingCollections/db.json b/parts/2/renderingCollections/db.json index eca9efb..9f89ba6 100644 --- a/parts/2/renderingCollections/db.json +++ b/parts/2/renderingCollections/db.json @@ -1,19 +1,49 @@ { - "notes": [ - { - "id": "1", - "content": "HTML is easy", - "important": true - }, - { - "id": "2", - "content": "Browser can execute only JavaScript", - "important": false - }, - { - "id": "3", - "content": "GET and POST are the most important methods of HTTP protocol", - "important": true - } - ] - } \ No newline at end of file + "notes": [ + { + "id": "1", + "content": "HTML is easy", + "important": false + }, + { + "id": "2", + "content": "Browser can execute only JavaScript", + "important": true + }, + { + "id": "3", + "content": "GET and POST are the most important methods of HTTP protocol", + "important": true + }, + { + "id": "4", + "content": "123", + "important": false + }, + { + "id": "5", + "content": "1233123111", + "important": true + }, + { + "id": "6", + "content": "", + "important": true + }, + { + "id": "7", + "content": "333", + "important": false + }, + { + "id": "8", + "content": "123", + "important": false + }, + { + "id": "9", + "content": "11111111111", + "important": false + } + ] +} \ No newline at end of file diff --git a/parts/2/renderingCollections/src/App.jsx b/parts/2/renderingCollections/src/App.jsx index b1a5615..ea012f6 100644 --- a/parts/2/renderingCollections/src/App.jsx +++ b/parts/2/renderingCollections/src/App.jsx @@ -1,6 +1,6 @@ import { useState, useEffect } from "react"; -import axios from "axios"; import Note from "./components/Note"; +import noteService from "./services/notes"; const App = ({ startingNotes = [] }) => { const [notes, setNotes] = useState(startingNotes); @@ -8,16 +8,13 @@ const App = ({ startingNotes = [] }) => { const [showAll, setShowAll] = useState(true); useEffect(() => { - console.log("effect"); - axios.get("http://localhost:3001/notes").then((response) => { - console.log("promise fulfilled"); + noteService.getAll().then((response) => { setTimeout(() => { setNotes(response.data); }, 2000); }); }, []); - console.log("render", notes.length, "notes"); const addNote = (event) => { event.preventDefault(); const newNoteObject = { @@ -26,12 +23,14 @@ const App = ({ startingNotes = [] }) => { id: String(notes.length + 1), }; - setNotes(notes.concat(newNoteObject)); + noteService.create(newNoteObject).then((response) => { + setNotes(notes.concat(response.data)); + }); + setNewNote(""); }; const handleNoteChange = (event) => { - console.log(event.target.value); setNewNote(event.target.value); }; @@ -39,6 +38,15 @@ const App = ({ startingNotes = [] }) => { ? notes : notes.filter((note) => note.important === true); + const toggleImportanceOf = (id) => { + const note = notes.find((n) => n.id === id); + const changedNote = { ...note, important: !note.important }; + + noteService.update(id, changedNote).then((response) => { + setNotes(notes.map((note) => (note.id === id ? response.data : note))); + }); + }; + return (