Skip to content

Commit

Permalink
fix(*): updating comments on blog page
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseaCodes committed Apr 12, 2024
1 parent efd12a1 commit 243d5bc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
26 changes: 12 additions & 14 deletions controllers/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,27 +267,24 @@ async function updateArticleComment(req, res) {

async function updateArticle(req, res) {
try {
const { title, subtitle, description, content, images, category } =
req.body;
res.clearCookie("articles-cache");
const { title, subtitle, description, content, images, category, comments } = req.body;

if (!images) {
logger.error("No image provided.");
res.clearCookie("user-cache");
return res.status(400).json({ msg: "No image upload" });
}
const originalArticle = await Articles.findOne({ _id: req.params.id });

res.clearCookie("articles-cache");

const originalBody = req.body;

await Articles.findOneAndUpdate(
{ _id: req.params.id },
{
title: title.toLowerCase(),
subtitle,
description,
content,
images,
category,
// title: title.toLowerCase(),
// subtitle,
// description,
// content,
// images,
// category,
comments: [originalArticle.comments, ...comments],
}
);

Expand All @@ -298,6 +295,7 @@ async function updateArticle(req, res) {
res.json({ msg: "Updated a article" });
} catch (err) {
logger.error(err);
console.log(err.message)

return res.status(500).json({ msg: err.message });
}
Expand Down
22 changes: 11 additions & 11 deletions src/Components/Article/MainContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import StickyFooter from '../Sticky/StickyFooter';
import BtnRender from './BtnRender';

const MainContainer = (props) => {
const { _id, likes, title, subtitle, description, images, markdown, comments } = props.detailArticle;
const { _id, likes, title, subtitle, description, images, markdown, comments } = props.detailArticle;
const timeFormater = props.timeFormater;
const readTime = props.readTime;
const user = props.user
Expand Down Expand Up @@ -59,7 +59,7 @@ const MainContainer = (props) => {
<BlogSubTitle>{subtitle}</BlogSubTitle>
<BlogDisplayImage src={images.url} alt={title} />
<BlogPhotoCredit>
Photo Credit by &nbsp;<u>{user.name}</u>
Photo Credit by &nbsp;<u>{user.name || "Anonymous"}</u>
</BlogPhotoCredit>
<BlogCard>
<br />
Expand All @@ -74,7 +74,7 @@ const MainContainer = (props) => {
></BlogContent>
<br />
<StickyFooter
comments
comments={comments}
viewComment={props.viewComment}
setViewComment={props.setViewComment}
id={_id}
Expand All @@ -84,16 +84,16 @@ const MainContainer = (props) => {
</BlogPost>
{props.isAdmin && props.isLoggedIn ? (
<>
<input
type="checkbox"
checked={true}
onChange={() => props.handleCheck(_id)}
<input
type="checkbox"
checked={true}
onChange={() => props.handleCheck(_id)}
/>
<BtnRender
article={props.article}
deleteArticle={props.deleteArticle}
<BtnRender
article={props.article}
deleteArticle={props.deleteArticle}
/>
</>
</>
) : null}
</BlogCard>
</section>
Expand Down
10 changes: 8 additions & 2 deletions src/Components/Article/RightColumn.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ const RightColumn = (props) => {
let filteredComments = res.data.comments.filter((comment) => {
return comment.blog === id;
});
setComments({comments: filteredComments});
setComments({ comments: filteredComments });
await axios.put(`/api/articles/${id}`, {
comments: filteredComments
})
}
getComments()
}
Expand All @@ -68,7 +71,10 @@ const RightColumn = (props) => {
let filteredComments = res.data.comments.filter((comment) => {
return comment.blog === id;
});
setComments({comments: filteredComments});
setComments({ comments: filteredComments });
await axios.put(`/api/articles/${id}`, {
comments: filteredComments,
});
} catch (error) {
console.log(error)
}
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Sticky/StickyFooter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const StickyFooter = ({id, likes, setViewComment, comments}) => {
<FaRegThumbsUp onClick={handleLike}/> &nbsp; <span>{postLikes}</span> &nbsp;
</JustifyContent>
<JustifyContent MarginRight>
<FaRegComment onClick={() => handleComment()}/> &nbsp; <span>{comments.length || 0}</span>
<FaRegComment onClick={() => handleComment()}/> &nbsp; <span>{comments.length - 1 || 0}</span>
</JustifyContent>
</JustifyContent>
<Font2>
Expand Down

0 comments on commit 243d5bc

Please sign in to comment.