-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(*): update client side saving of articles
- Loading branch information
1 parent
e1b27a3
commit a151c80
Showing
2 changed files
with
93 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,93 @@ | ||
import React, { useContext, useEffect } from "react"; | ||
import {StyledLeftContainer, JustifyContent} from '../../Layout/Container/styledArticle'; | ||
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 {MdBookmarkBorder} from 'react-icons/md'; | ||
import logo from '../../Assets/Images/newLogo.png'; | ||
import React, { useContext, useEffect, useState } from "react"; | ||
import { | ||
StyledLeftContainer, | ||
JustifyContent, | ||
} from "../../Layout/Container/styledArticle"; | ||
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 { MdBookmarkBorder, MdBookmark } from "react-icons/md"; | ||
import logo from "../../Assets/Images/newLogo.png"; | ||
import { Link } from "react-router-dom"; | ||
import {GlobalState} from '../../GlobalState'; | ||
import { GlobalState } from "../../GlobalState"; | ||
import { useParams } from "react-router-dom"; | ||
import axios from "axios"; | ||
|
||
const SideBar = (props) => { | ||
|
||
const [savedArticleState, setSavedArticleState] = useState( | ||
props.user.savedArticles.includes(props.article._id) | ||
); | ||
const param = useParams(); | ||
const state = useContext(GlobalState); | ||
|
||
const [isAdmin] = state.userAPI.isAdmin; | ||
|
||
const handleUnauthorized = async e => { | ||
const handleUnauthorized = async (e) => { | ||
e.preventDefault(); | ||
// Pass the error to display through state | ||
alert("You are not authorized to make a post. If you create an account you can begin making posts.") | ||
} | ||
alert( | ||
"You are not authorized to make a post. If you create an account you can begin making posts." | ||
); | ||
}; | ||
|
||
const handleSave = async e => { | ||
const handleSave = async (e) => { | ||
e.preventDefault(); | ||
if (props.user) { | ||
console.log(props.user); | ||
await axios.put(`/api/user/${props.user._id}`, { | ||
savedArticles: [props.article._id], | ||
}); | ||
} | ||
} | ||
if (props.user) { | ||
await axios.put(`/api/user/${props.user._id}`, { | ||
savedArticles: [props.article._id], | ||
}); | ||
setSavedArticleState(!savedArticleState) | ||
} | ||
}; | ||
|
||
const handleNotification = async e => { | ||
const handleNotification = async (e) => { | ||
e.preventDefault(); | ||
// Create notification function on backend | ||
alert("Start being alerted when user makes posts.") | ||
alert("Start being alerted when user makes posts."); | ||
if (props.user) { | ||
console.log(props.user); | ||
await axios.put(`/api/user/${props.user._id}`, { | ||
notifications: [props.user._id], | ||
}); | ||
} | ||
} | ||
return ( | ||
<StyledLeftContainer> | ||
<JustifyContent > | ||
<Link to="/"> | ||
<LogoImage src={logo} alt="logo"/> | ||
}; | ||
console.log(props); | ||
return ( | ||
<StyledLeftContainer> | ||
<JustifyContent> | ||
<Link to="/"> | ||
<LogoImage src={logo} alt="logo" /> | ||
</Link> | ||
</JustifyContent> | ||
<StackedAlignn> | ||
<Link to="/"> | ||
<BsHouseDoor style={{ marginBottom: "5rem" }} /> | ||
</Link> | ||
<BsBell onClick={handleNotification} style={{ marginBottom: "5rem" }} /> | ||
{!savedArticleState ? ( | ||
<MdBookmarkBorder | ||
onClick={handleSave} | ||
style={{ marginBottom: "5rem" }} | ||
/> | ||
) : ( | ||
<MdBookmark onClick={handleSave} style={{ marginBottom: "5rem" }} /> | ||
)} | ||
<ArticleHr /> | ||
{isAdmin ? ( | ||
<Link to="/admin/blog/new"> | ||
<HiOutlinePencilAlt style={{ marginBottom: "5rem" }} /> | ||
</Link> | ||
</JustifyContent> | ||
<StackedAlignn > | ||
<Link to="/"><BsHouseDoor style={{marginBottom: '5rem'}}/></Link> | ||
<BsBell onClick={handleNotification} style={{marginBottom: '5rem'}}/> | ||
<MdBookmarkBorder onClick={handleSave} style={{marginBottom: '5rem'}}/> | ||
<ArticleHr/> | ||
{isAdmin ? | ||
<Link to="/admin/blog/new"><HiOutlinePencilAlt style={{marginBottom: '5rem'}}/></Link> | ||
: | ||
<HiOutlinePencilAlt onClick={handleUnauthorized} style={{marginBottom: '5rem'}}/> | ||
} | ||
</StackedAlignn> | ||
</StyledLeftContainer> | ||
) | ||
} | ||
|
||
) : ( | ||
<HiOutlinePencilAlt | ||
onClick={handleUnauthorized} | ||
style={{ marginBottom: "5rem" }} | ||
/> | ||
)} | ||
</StackedAlignn> | ||
</StyledLeftContainer> | ||
); | ||
}; | ||
|
||
export default SideBar; |