react blog - #1
Open
Madhumitha2213 wants to merge 2 commits into
Open
Conversation
2. implemented the edit and delete functionality
| const confirmDelete = window.confirm("Are you sure you want to delete this blog?"); | ||
| if (!confirmDelete) return; | ||
|
|
||
| try { |
Comment on lines
+15
to
+23
| fetch("http://localhost:5000/list") | ||
| .then((response) => response.json()) | ||
| .then((data) => { | ||
| setBlogs(data); | ||
| setLoading(false); | ||
| }) | ||
| .catch((error) => { | ||
| console.error("Error fetching blogs:", error); | ||
| setLoading(false); | ||
| }); | ||
| }, []); | ||
| }; |
| console.error("Error during submission:", error); | ||
| alert("Network error. Please try again later."); | ||
| alert("An error occurred while saving the blog."); | ||
| } |
Comment on lines
+62
to
+84
| app.put("/bloglist/:id", (req, res) => { | ||
| const { id } = req.params; | ||
| const { title, category, content, postedBy, date } = req.body; | ||
|
|
||
| if (!title || !category || !content || !postedBy || !date) { | ||
| return res.status(400).json({ message: "All fields are required." }); | ||
| } | ||
|
|
||
| const blogs = readDataFromFile(); | ||
| const blogIndex = blogs.findIndex((blog) => blog.id === parseInt(id)); | ||
|
|
||
| if (blogIndex === -1) { | ||
| return res.status(404).json({ message: "Blog not found." }); | ||
| } | ||
|
|
||
| blogs[blogIndex] = { id: parseInt(id), title, category, content, postedBy, date }; | ||
| writeDataToFile(blogs); | ||
|
|
||
| res.status(200).json({ | ||
| message: "Blog updated successfully!", | ||
| blog: blogs[blogIndex], | ||
| }); | ||
| }); |
Comment on lines
+87
to
+100
| app.delete("/bloglist/:id", (req, res) => { | ||
| const { id } = req.params; | ||
|
|
||
| const blogs = readDataFromFile(); | ||
| const filteredBlogs = blogs.filter((blog) => blog.id !== parseInt(id)); | ||
|
|
||
| if (blogs.length === filteredBlogs.length) { | ||
| return res.status(404).json({ message: "Blog not found." }); | ||
| } | ||
|
|
||
| writeDataToFile(filteredBlogs); | ||
|
|
||
| res.status(200).json({ message: "Blog deleted successfully!" }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.