From 6691671c86af833ff3e025d74aeb2b75d4722ae6 Mon Sep 17 00:00:00 2001 From: counterweight Date: Sun, 25 May 2025 15:04:45 +0200 Subject: [PATCH] following along --- parts/2/renderingCollections/db.json | 66 ++++++++++++++----- parts/2/renderingCollections/src/App.jsx | 33 +++++++--- .../src/components/Note.jsx | 15 ++++- .../src/services/notes.js | 20 ++++++ 4 files changed, 105 insertions(+), 29 deletions(-) create mode 100644 parts/2/renderingCollections/src/services/notes.js 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 (

Notes

@@ -49,7 +57,16 @@ const App = ({ startingNotes = [] }) => { show {showAll ? "important" : "all"}{" "} {" "}
- +
diff --git a/parts/2/renderingCollections/src/components/Note.jsx b/parts/2/renderingCollections/src/components/Note.jsx index 67dc31f..1dd2d86 100644 --- a/parts/2/renderingCollections/src/components/Note.jsx +++ b/parts/2/renderingCollections/src/components/Note.jsx @@ -1,5 +1,14 @@ -const Note = ({ note }) => { - return
  • {note.content}
  • ; +const Note = ({ note, toggleImportance }) => { + const importanceLabel = note.important + ? "make not important" + : "make important"; + return ( +
  • + {" "} + {note.content}{" "} + +
  • + ); }; -export default Note \ No newline at end of file +export default Note; diff --git a/parts/2/renderingCollections/src/services/notes.js b/parts/2/renderingCollections/src/services/notes.js new file mode 100644 index 0000000..1dd00a9 --- /dev/null +++ b/parts/2/renderingCollections/src/services/notes.js @@ -0,0 +1,20 @@ +import axios from "axios"; +const baseUrl = "http://localhost:3001/notes"; + +const getAll = () => { + return axios.get(baseUrl); +}; + +const create = (newObject) => { + return axios.post(baseUrl, newObject); +}; + +const update = (id, newObject) => { + return axios.put(`${baseUrl}/${id}`, newObject); +}; + +export default { + getAll: getAll, + create: create, + update: update, +};