From 828e874d52339caefa5175f06dc3e7d5b6f469f2 Mon Sep 17 00:00:00 2001 From: Dhiraj Borse Date: Sun, 30 Oct 2022 23:53:32 +0530 Subject: [PATCH 1/4] delete button with funtionality is added --- server/dbConnect/db.js | 3 ++- server/server.js | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/server/dbConnect/db.js b/server/dbConnect/db.js index 3d59d27..86502f9 100644 --- a/server/dbConnect/db.js +++ b/server/dbConnect/db.js @@ -1,5 +1,6 @@ const mongoose = require("mongoose"); -const url = process.env.DATABASE_URI; +// const url = process.env.DATABASE_URI; +const url = "mongodb://localhost:27017/NotesDB"; mongoose.connect(url, {useNewUrlParser: true}) const NotesSchema = new mongoose.Schema({ diff --git a/server/server.js b/server/server.js index 7b0f742..645531f 100644 --- a/server/server.js +++ b/server/server.js @@ -8,7 +8,7 @@ const cors = require("cors"); app.use( cors({ origin : "*", // allow from any origin - methods : ['GET', 'POST', 'DELETE', 'PUT', 'PATCH'], // allowed methods + methods : "*", // allowed methods credentials: true }) ) @@ -35,6 +35,19 @@ app.post("/", async(req, res)=>{ } }) +app.delete("/", async (req, res)=>{ + let reqNoteId = req.body.noteId + Note.findByIdAndDelete(reqNoteId, (err, blog) => { + if (err) { + console.log(err); + } else { + // console.log("blog " + blog.title + "is deleted !!"); + res.send("delted successfully") + } + }) + +}) + app.listen(3001, () => { console.log("server started at server 3001"); }); From 041e02d68bece52cac26c032d5951997de12e911 Mon Sep 17 00:00:00 2001 From: Dhiraj Borse Date: Sun, 30 Oct 2022 23:54:00 +0530 Subject: [PATCH 2/4] delete button with funtionality is added --- client/src/components/Card.js | 7 +++-- client/src/components/DeleteButton.js | 20 +++----------- client/src/components/Modal.js | 10 +++---- client/src/pages/Home.js | 38 ++++++++++++++++++++------- 4 files changed, 43 insertions(+), 32 deletions(-) diff --git a/client/src/components/Card.js b/client/src/components/Card.js index 7d7facc..07a92ce 100644 --- a/client/src/components/Card.js +++ b/client/src/components/Card.js @@ -3,8 +3,11 @@ import React from 'react'; import Card from 'react-bootstrap/Card'; import DeleteButton from './DeleteButton'; -const CardComp = ({ title, subtitle, body, link }) => { +const CardComp = ({ title, subtitle, body, link, _id, noteIdfunc }) => { + const noteId = (id)=>{ + noteIdfunc(id) + } return ( @@ -14,7 +17,7 @@ const CardComp = ({ title, subtitle, body, link }) => { {subtitle} {body} {link} - + diff --git a/client/src/components/DeleteButton.js b/client/src/components/DeleteButton.js index d8499e0..e7cc3de 100644 --- a/client/src/components/DeleteButton.js +++ b/client/src/components/DeleteButton.js @@ -2,26 +2,14 @@ import React, {useEffect, useState} from 'react' import classes from './DeleteButton.module.css' const DeleteButton = (props) => { - const [notes, setNotes] = useState([]) - useEffect(() => { - let items = JSON.parse(localStorage.getItem("todos")); - setNotes(items) - - }, []) - console.log(notes) - - const handleClickOnDelBtn = ()=>{ - const newNotes = notes.filter((note)=>{ - return note != props.title - }) - console.log(newNotes) - // localStorage.clear(); - localStorage.setItem('todos', JSON.stringify(newNotes)) + const handleClickOnDelBtn = (id)=>{ + props.noteId(id) + console.log(id) } return ( -
+
{handleClickOnDelBtn(props.id)}} > diff --git a/client/src/components/Modal.js b/client/src/components/Modal.js index d3578f6..e3cecac 100644 --- a/client/src/components/Modal.js +++ b/client/src/components/Modal.js @@ -17,15 +17,13 @@ const Dialog = ({ show, setShow, notes, setnotes, close }) => { const handleSubmit = (event) => { const note = { - title: "title1fsdasdfa1", - subtitle: "subtfasditle2", - body: "desasdfc", + title: title, + subtitle: subtitle, + body: desc, } event.preventDefault(); if(title !== "") { setError(false); - - setnotes([ ...notes, note ]); close(false); } else { setError(true); @@ -47,7 +45,9 @@ const Dialog = ({ show, setShow, notes, setnotes, close }) => { Credentials: "include", }); const data = await response.json(); + setnotes([ ...notes, {title : data.title, subtitle : data.subtitle, body : data.body, _id : data._id} ]); console.log(data); + }; return ( diff --git a/client/src/pages/Home.js b/client/src/pages/Home.js index 16be405..50a7b9a 100644 --- a/client/src/pages/Home.js +++ b/client/src/pages/Home.js @@ -8,9 +8,6 @@ import AddButton from '../components/AddButton'; const Home = () => { const [notes, setNotes] = React.useState([]); // var notes = []; const [loading, setLoading] = useState(false) - // React.useEffect(() => { - // setNotes([...JSON.parse(localStorage.getItem('notes'))]); - // }, []); const fetchAllNotes = async ()=>{ setLoading(true); @@ -30,14 +27,37 @@ const Home = () => { } }, []) + const noteIdfunc = (id)=>{ + console.log(id) + try { + console.log("first") + delNote(id) + } catch (error) { + console.log("suuuuu") + } + + } - // const addNotes = async ()=>{ - // const response = await fetch("http://localhost:3001/", { - // method : "POST", + const delNote = async (noteId) => { + await fetch("http://localhost:3001/", { + method: "DELETE", + body: JSON.stringify({noteId : noteId}), + headers: { + "Content-Type": "application/json", + }, + Credentials: "include", + }); - // }) - // } + setNotes((prevNotes)=>{ + return ( + prevNotes.filter((note)=>{ + return note._id != noteId + }) + ) + }) + }; + return (
@@ -55,7 +75,7 @@ const Home = () => { {notes.map((todo, index) => (
- +
))} From 4b6e7c2f4f4f99e6ca65ace66aea6437b6108882 Mon Sep 17 00:00:00 2001 From: Dhiraj Borse Date: Tue, 1 Nov 2022 00:16:29 +0530 Subject: [PATCH 3/4] small changes --- client/src/components/AddButton.js | 15 +++++--- client/src/components/DeleteButton.module.css | 4 --- client/src/pages/Home.js | 2 ++ server/.env | 3 +- server/package-lock.json | 35 ------------------- server/package.json | 1 - server/routes/noteRoute.js | 4 +-- 7 files changed, 14 insertions(+), 50 deletions(-) delete mode 100644 client/src/components/DeleteButton.module.css diff --git a/client/src/components/AddButton.js b/client/src/components/AddButton.js index 69c3ad1..9067a1e 100644 --- a/client/src/components/AddButton.js +++ b/client/src/components/AddButton.js @@ -3,7 +3,7 @@ import Card from 'react-bootstrap/Card'; import Dialog from './Modal'; -const AddButton = ({ notes, change }) => { +const AddButton = ({ todos, change }) => { const [show, setShow] = React.useState(false); @@ -13,9 +13,14 @@ const AddButton = ({ notes, change }) => { handleShow(); - change([ - ...notes, - ]); + // change([ + // ...todos, + // { + // title: "A", + // subtitle: "S", + // body: "description" + // } + // ]); console.log("Added"); } @@ -24,7 +29,7 @@ const AddButton = ({ notes, change }) => { + - +
) } diff --git a/client/src/components/DeleteButton.module.css b/client/src/components/DeleteButton.module.css deleted file mode 100644 index c353021..0000000 --- a/client/src/components/DeleteButton.module.css +++ /dev/null @@ -1,4 +0,0 @@ -.delBtn:hover{ - color: red; - cursor: pointer; -} \ No newline at end of file diff --git a/client/src/pages/Home.js b/client/src/pages/Home.js index 50a7b9a..266bee7 100644 --- a/client/src/pages/Home.js +++ b/client/src/pages/Home.js @@ -39,6 +39,7 @@ const Home = () => { } const delNote = async (noteId) => { + setLoading(true) await fetch("http://localhost:3001/", { method: "DELETE", body: JSON.stringify({noteId : noteId}), @@ -47,6 +48,7 @@ const Home = () => { }, Credentials: "include", }); + setLoading(false) setNotes((prevNotes)=>{ return ( diff --git a/server/.env b/server/.env index 4a471e4..0eeed79 100644 --- a/server/.env +++ b/server/.env @@ -1,2 +1 @@ -# DATABASE_URI= mongodb+srv://oluwatomisin:oluwatomisin@cluster0.k4zdr.mongodb.net/TO-DO?retryWrites=true&w=majority -DATABASE_URI= "mongodb://localhost:27017/NotesDB" \ No newline at end of file +DATABASE_URI= mongodb+srv://oluwatomisin:oluwatomisin@cluster0.k4zdr.mongodb.net/TO-DO?retryWrites=true&w=majority \ No newline at end of file diff --git a/server/package-lock.json b/server/package-lock.json index 30f062b..60ab0da 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -10,7 +10,6 @@ "license": "ISC", "dependencies": { "body-parser": "^1.20.0", - "cors": "^2.8.5", "express": "^4.18.1", "mongoose": "^6.6.3" }, @@ -272,18 +271,6 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, - "node_modules/cors": { - "version": "2.8.5", - "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", - "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", - "dependencies": { - "object-assign": "^4", - "vary": "^1" - }, - "engines": { - "node": ">= 0.10" - } - }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -870,14 +857,6 @@ "node": ">=0.10.0" } }, - "node_modules/object-assign": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", - "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/object-inspect": { "version": "1.12.2", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.2.tgz", @@ -1465,15 +1444,6 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, - "cors": { - "version": "2.8.5", - "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", - "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", - "requires": { - "object-assign": "^4", - "vary": "^1" - } - }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -1903,11 +1873,6 @@ "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", "dev": true }, - "object-assign": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", - "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==" - }, "object-inspect": { "version": "1.12.2", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.2.tgz", diff --git a/server/package.json b/server/package.json index ddd5a1b..41fb920 100644 --- a/server/package.json +++ b/server/package.json @@ -11,7 +11,6 @@ "license": "ISC", "dependencies": { "body-parser": "^1.20.0", - "cors": "^2.8.5", "express": "^4.18.1", "mongoose": "^6.6.3" }, diff --git a/server/routes/noteRoute.js b/server/routes/noteRoute.js index f8ae911..e944851 100644 --- a/server/routes/noteRoute.js +++ b/server/routes/noteRoute.js @@ -1,10 +1,8 @@ const express = require('express'); const router = express.Router(); -const app = express(); router.route('/').get().post(); -router.route('/').delete().put(); -// router.route("/delete/:noteId").delete() +router.route('/:id').delete().put(); module.exports = router; From 2e96a40c36c1af64a0841fd1bd43c8a111a57661 Mon Sep 17 00:00:00 2001 From: Dhiraj Borse Date: Tue, 1 Nov 2022 00:17:06 +0530 Subject: [PATCH 4/4] small changes --- client/src/components/AddButton.js | 15 +++++---------- client/src/components/DeleteButton.module.css | 4 ++++ 2 files changed, 9 insertions(+), 10 deletions(-) create mode 100644 client/src/components/DeleteButton.module.css diff --git a/client/src/components/AddButton.js b/client/src/components/AddButton.js index 9067a1e..69c3ad1 100644 --- a/client/src/components/AddButton.js +++ b/client/src/components/AddButton.js @@ -3,7 +3,7 @@ import Card from 'react-bootstrap/Card'; import Dialog from './Modal'; -const AddButton = ({ todos, change }) => { +const AddButton = ({ notes, change }) => { const [show, setShow] = React.useState(false); @@ -13,14 +13,9 @@ const AddButton = ({ todos, change }) => { handleShow(); - // change([ - // ...todos, - // { - // title: "A", - // subtitle: "S", - // body: "description" - // } - // ]); + change([ + ...notes, + ]); console.log("Added"); } @@ -29,7 +24,7 @@ const AddButton = ({ todos, change }) => { + - +
) } diff --git a/client/src/components/DeleteButton.module.css b/client/src/components/DeleteButton.module.css new file mode 100644 index 0000000..c353021 --- /dev/null +++ b/client/src/components/DeleteButton.module.css @@ -0,0 +1,4 @@ +.delBtn:hover{ + color: red; + cursor: pointer; +} \ No newline at end of file