Skip to content

Commit

Permalink
fix(articles): update notifications button
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseaCodes committed May 22, 2024
1 parent 8541bee commit 7fb4d13
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
52 changes: 38 additions & 14 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ async function getUser(req, res) {

async function updateProfile(req, res) {
try {

const originalBody = req.body;
const {
notifications,
Expand All @@ -242,20 +241,44 @@ async function updateProfile(req, res) {
} = originalBody;

const originalUser = await Users.findOne({ _id: req.params.id });

if (notifications) {
const newNotifications = originalUser.notifications.concat(notifications);
const uniqueNotifications = [...new Set(newNotifications)];
await Users.findOneAndUpdate(
{ _id: req.params.id },
{
notifications: uniqueNotifications,
}
const existingNotificationsArticle = originalUser.notifications.filter(
(notification) => notification !== req.params.id
);
if (existingNotificationsArticle.length == 1) {
const removeNotificationsArticles = originalUser.notifications.filter(
(notification) => notification == req.params.id
);
console.log(
"removeNotificationsArticles",
req.params.id,
removeNotificationsArticles
);
await Users.findOneAndUpdate(
{ _id: req.params.id },
{
notifications: removeNotificationsArticles,
}
);
} else {
const newNotifications =
originalUser.notifications.concat(notifications);
const uniqueNotifications = [...new Set(newNotifications)];
console.log({notifications})
console.log("uniqueNotifications", req.params.id, uniqueNotifications);
await Users.findOneAndUpdate(
{ _id: req.params.id },
{
notifications: uniqueNotifications,
}
);
}
}

if (favoriteArticles) {
const newFavoriteArticles =
originalUser.favoriteArticles.concat(favoriteArticles);
originalUser.favoriteArticles.concat(favoriteArticles);
const uniqueFavoriteArticles = [...new Set(newFavoriteArticles)];
await Users.findOneAndUpdate(
{ _id: req.params.id },
Expand All @@ -264,7 +287,7 @@ async function updateProfile(req, res) {
}
);
}

if (savedArticles) {
const existingSavedArticle = originalUser.savedArticles.filter(
(article) => article !== req.params.id
Expand All @@ -273,17 +296,18 @@ async function updateProfile(req, res) {
const removeSavedArticles = originalUser.savedArticles.filter(
(article) => article == req.params.id
);
console.log('removeSavedArticles', req.params.id, removeSavedArticles);
console.log("removeSavedArticles", req.params.id, removeSavedArticles);
await Users.findOneAndUpdate(
{ _id: req.params.id },
{
savedArticles: removeSavedArticles,
}
);
} else {
const newSavedArticles = originalUser.savedArticles.concat(savedArticles);
const newSavedArticles =
originalUser.savedArticles.concat(savedArticles);
const uniqueSavedArticles = [...new Set(newSavedArticles)];
console.log('uniqueSavedArticles', req.params.id, uniqueSavedArticles);
console.log("uniqueSavedArticles", req.params.id, uniqueSavedArticles);
await Users.findOneAndUpdate(
{ _id: req.params.id },
{
Expand Down
22 changes: 18 additions & 4 deletions src/Components/NavBar/SideBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { LogoImage } from "../../Layout/Image/styledImage";
import { StackedAlignn } from "../../Layout/Icon/styledIcons";
import { ArticleHr } from "../../Layout/Hr/styledHr";
import { HiOutlinePencilAlt } from "react-icons/hi";
import { BsHouseDoor, BsBell } from "react-icons/bs";
import { BsHouseDoor, BsBell, BsBellFill } from "react-icons/bs";
import { MdBookmarkBorder, MdBookmark } from "react-icons/md";
import logo from "../../Assets/Images/newLogo.png";
import { Link } from "react-router-dom";
Expand All @@ -19,6 +19,9 @@ const SideBar = (props) => {
const [savedArticleState, setSavedArticleState] = useState(
props.user.savedArticles.includes(props.article._id)
);
const [notifications, setNotifications] = useState(
props.user.notifications.includes(props.article._id)
);
const param = useParams();
const state = useContext(GlobalState);

Expand All @@ -34,6 +37,7 @@ const SideBar = (props) => {

const handleSave = async (e) => {
e.preventDefault();
alert("Start being alerted when user makes posts.");
if (props.user) {
await axios.put(`/api/user/${props.user._id}`, {
savedArticles: [props.article._id],
Expand All @@ -49,11 +53,11 @@ const SideBar = (props) => {
if (props.user) {
console.log(props.user);
await axios.put(`/api/user/${props.user._id}`, {
notifications: [props.user._id],
notifications: [props.article._id],
});
setNotifications(!notifications);
}
};
console.log(props);
return (
<StyledLeftContainer>
<JustifyContent>
Expand All @@ -65,7 +69,17 @@ const SideBar = (props) => {
<Link to="/">
<BsHouseDoor style={{ marginBottom: "5rem" }} />
</Link>
<BsBell onClick={handleNotification} style={{ marginBottom: "5rem" }} />
{notifications ? (
<BsBellFill
onClick={handleNotification}
style={{ marginBottom: "5rem" }}
/>
) : (
<BsBell
onClick={handleNotification}
style={{ marginBottom: "5rem" }}
/>
)}
{!savedArticleState ? (
<MdBookmarkBorder
onClick={handleSave}
Expand Down

0 comments on commit 7fb4d13

Please sign in to comment.