From fc28318fd11b5471e315e2cc020c4ca4485792e6 Mon Sep 17 00:00:00 2001 From: hoseacodes Date: Thu, 3 Aug 2023 15:32:59 -0500 Subject: [PATCH 01/83] fix(*): handle blog like button --- controllers/article.js | 33 ++++++++++++------------ controllers/user.js | 27 ++++++++++++------- models/article.js | 4 +++ src/Components/Article/MainContainer.jsx | 7 +++-- src/Components/Footer/Footer.jsx | 2 +- src/Components/Sticky/StickyFooter.jsx | 21 ++++++++++++--- src/Pages/Auth/login.jsx | 4 +-- 7 files changed, 62 insertions(+), 36 deletions(-) diff --git a/controllers/article.js b/controllers/article.js index 312aa5f7..a19c6bcb 100755 --- a/controllers/article.js +++ b/controllers/article.js @@ -144,29 +144,30 @@ async function deletePostcomment(req, res) { await Articles.findByIdAndDelete(req.params.id) res.clearCookie('comments-cache'); res.json({ msg: "Deleted a article" }) - } catch (err) { - + } catch (err) { + logger.error(err) - + return res.status(500).json({ msg: err.message }) + } } -} - + async function updateLikes(req, res) { try { + + const post_id = req.params.id + let { likes } = req.body; + likes += 1; - const uid = req.body.uid - const post_id = req.params.id - const { likes } = req.body; - - const values = [ uid, post_id ] - console.log(values) - likes += 1; - await Articles.findOneAndUpdate({ _id: req.params.id }, - {likes}); - + await Articles.findOneAndUpdate({ _id: post_id }, + {likes}); + + res.json({ + msg: `${post_id} received a new like`, + totalLikes: likes + }) } catch (err) { - + console.log(err) logger.error(err); return res.status(500).json({ msg: err.message }); diff --git a/controllers/user.js b/controllers/user.js index 0c2a33d7..76117e4b 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -53,16 +53,17 @@ async function register(req, res) { function refreshToken(req, res) { try { - const rf_token = req.cookies.refreshtoken.replace(/^JWT\s/, ""); - if (!rf_token) return res.status(400).json({ msg: "Please Login or Register" }) + let rf_token = req.cookies.refreshtoken + if (rf_token) rf_token = rf_token = req.cookies.refreshtoken.replace(/^JWT\s/, ""); + if (!rf_token) return res.status(400).json({ msg: "Please Login or Register" }) - jwt.verify(rf_token, process.env.REFRESH_TOKEN_SECRET, (err, user) => { - if (err) return res.status(400).json({ msg: "Please Login or Register" }) + jwt.verify(rf_token, process.env.REFRESH_TOKEN_SECRET, (err, user) => { + if (err) return res.status(400).json({ msg: "Please Verify Info & Login or Register" }) - const accesstoken = createAccessToken({ id: user.id }) + const accesstoken = createAccessToken({ id: user.id }) - res.json({ accesstoken }) - }) + res.json({ accesstoken }) + }) } catch (err) { return res.status(500).json({ msg: err.message, "err": err }) } @@ -89,7 +90,7 @@ async function login(req, res) { maxAge: 7 * 25 * 60 * 60 * 1000 }) } - res.json({accesstoken }) + res.json({ accesstoken }) } catch (err) { return res.status(500).json({ msg: err.message }) @@ -214,10 +215,16 @@ async function getUser(req, res) { async function updateProfile(req, res) { try { - const { name, avatar, title, work, education, skills, location, phone, socialMedia, websites } = req.body; + const { name, avatar, title, work, education, + skills, location, phone, socialMedia, websites, + socialMediaHandles, articles, cart, role, username, + aboutMe, projects } = req.body; await Users.findOneAndUpdate({ _id: req.params.id }, { - name, avatar, title, work, education, skills, location, phone, socialMedia, websites + name, avatar, title, work, education, + skills, location, phone, socialMedia, websites, + socialMediaHandles, articles, cart, role, username, + aboutMe, projects }) res.clearCookie('users-cache'); diff --git a/models/article.js b/models/article.js index 0f7a99e5..8ad05486 100755 --- a/models/article.js +++ b/models/article.js @@ -50,6 +50,10 @@ const articleSchema = new mongoose.Schema({ type: String, required: true }, + likes: { + type: Number, + default: 0 + }, // tags: { // type: [String] // }, diff --git a/src/Components/Article/MainContainer.jsx b/src/Components/Article/MainContainer.jsx index 10180f2d..aa4ee45c 100644 --- a/src/Components/Article/MainContainer.jsx +++ b/src/Components/Article/MainContainer.jsx @@ -1,6 +1,6 @@ import React from 'react'; import Alert from 'react-bootstrap/Alert' -import { AiFillStar, AiFillPlayCircle } from 'react-icons/ai'; +import { AiFillStar } from 'react-icons/ai'; import {JustifyContent, StyledMainContainer, PaddingContent, BlogCard, BlogPost} from '../../Layout/Container/styledArticle'; import {CircleImage, BlogDisplayImage} from '../../Layout/Image/styledImage'; @@ -16,8 +16,7 @@ import RelatedPosts from './RelatedPosts'; import StickyFooter from '../Sticky/StickyFooter'; const MainContainer = (props) => { - - const { title, subtitle, description, images, markdown } = props.detailArticle; + const { _id, likes, title, subtitle, description, images, markdown } = props.detailArticle; const timeFormater = props.timeFormater; const readTime = props.readTime; const user = props.user @@ -66,7 +65,7 @@ const MainContainer = (props) => {

- + {/* { const year = new Date().getFullYear(); - if (isHome == true) { + if (isHome === true) { return ( <>