Skip to content

react blog - #1

Open
Madhumitha2213 wants to merge 2 commits into
mainfrom
reactblog
Open

react blog#1
Madhumitha2213 wants to merge 2 commits into
mainfrom
reactblog

Conversation

@Madhumitha2213

Copy link
Copy Markdown
Contributor

No description provided.

2. implemented the edit and delete functionality
const confirmDelete = window.confirm("Are you sure you want to delete this blog?");
if (!confirmDelete) return;

try {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create service

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);
});
}, []);
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create service

console.error("Error during submission:", error);
alert("Network error. Please try again later.");
alert("An error occurred while saving the blog.");
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create service

Comment thread src/server.js
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],
});
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create service

Comment thread src/server.js
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!" });
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants