diff --git a/component/layout/BlogPost/BlogPost.jsx b/component/layout/BlogPost/BlogPost.jsx index 46c5e26d..8839ed92 100644 --- a/component/layout/BlogPost/BlogPost.jsx +++ b/component/layout/BlogPost/BlogPost.jsx @@ -25,40 +25,38 @@ export default function BlogPost(props) { * @author sNkr-10 */ //posts having no votes field will have null as initial state for voteType and votes are created during createPost - const [voteType, setVoteType] = useState(0); - console.log(voteType, 'voteType'); - useEffect(() => { - const votes = props.blog.votes?.find((item) => item.userId == props.userId); - if (votes) { - setVoteType(votes.type); - } - }, [props.blog]); + const [voteType, setVoteType] = useState(); const [voteCount, setVoteCount] = useState(0); const getVoteCount = async () => { try { - const response = await PostService.getVotes( - props.blog.id, - ); - console.log(response, 'vote value'); - if (response.data != null) { - setVoteCount( - response.data.upvotes - - response.data.downvotes - ); - console.log(voteCount); - } - return response; + const response = await PostService.getVotes(props.blog.id); + if (response.data != null) { + setVoteCount(response.data.upvotes - response.data.downvotes); + } + return response; } catch (err) { - notify(err?.response?.data?.message ?? err?.message, 'error'); + notify(err?.response?.data?.message ?? err?.message, "error"); + } + }; + + const getVoteType = async () => { + try { + const response = await PostService.getVotesType(props.blog.id); + if (response.data != null) { + setVoteType(response.data.vote_kind); + } + return response; + } catch (err) { + notify(err?.response?.data?.message ?? err?.message, "error"); } }; useEffect(() => { getVoteCount(); + getVoteType(); }, []); - /** * function triggered during upvotes/downvotes * @author sNkr-10 @@ -67,18 +65,15 @@ export default function BlogPost(props) { const setVotes = async (value) => { try { if (props.userId) { - const response = await PostService.setVotes( - value, - props.blog.id, - ); + const response = await PostService.setVotes(value, props.blog.id); if (response.data != null) { getVoteCount(); - setVoteType(value); + getVoteType(); } return response; } } catch (err) { - notify(err?.response?.data?.message ?? err?.message, 'error'); + notify(err?.response?.data?.message ?? err?.message, "error"); } }; @@ -87,7 +82,7 @@ export default function BlogPost(props) { if (props.userId != props.blog.user.id) { setVotes(value); } - !props.userId && notify("Please login for further actions", 'dark'); + !props.userId && notify("Please login for further actions", "dark"); }; const [headings, setHeadings] = useState(); @@ -100,7 +95,6 @@ export default function BlogPost(props) { text: item.innerText }; }); - // console.log({ h2Tags }); setHeadings(hTags); hljs.highlightAll(); }, []); @@ -141,7 +135,12 @@ export default function BlogPost(props) {
- handleClick(1)} className="uo"> + { + handleClick(1); + }} + className="up" + > {voteCount} - handleClick(-1)} className="down"> + { + handleClick(-1); + }} + className="down" + > diff --git a/config/config.js b/config/config.js index fbde84ea..3416467e 100644 --- a/config/config.js +++ b/config/config.js @@ -39,9 +39,11 @@ const publishedPostsUrl = "api/posts/published"; const logoutUrl = `${apiVerUrl}/logout`; const setVoteUrl = `${apiVerUrl}/post-vote`; const getVoteUrl = `${apiVerUrl}/post-votes`; +const getVoteTypeUrl = `${apiVerUrl}/post-vote-by-user`; const configVars = { baseUrl, + getVoteTypeUrl, postCount, setVoteUrl, getVoteUrl, diff --git a/pages/[blogPost].js b/pages/[blogPost].js index 42d99d38..2e6183da 100644 --- a/pages/[blogPost].js +++ b/pages/[blogPost].js @@ -69,7 +69,6 @@ export default function BlogListing({ blog , relatedPosts }) { status: "published", }; const response = await PostService.getPostCount(UserId, postParams); - console.log(response.data.count, 'count'); setPostsCount(response.data.count); }; diff --git a/services/PostService.js b/services/PostService.js index f90b9540..3830b651 100644 --- a/services/PostService.js +++ b/services/PostService.js @@ -9,6 +9,7 @@ import { postCount, setVoteUrl, postUser, + getVoteTypeUrl, postBySlug, changeStatusUrl, adminReviewUrl, @@ -136,8 +137,6 @@ async function deletePostById(userCookie, postId) { } } - - // async function changePostStatus(userCookie, postId, statusUpdate) { // try { // const { data } = await axios.post( @@ -220,7 +219,7 @@ async function getPostsByQuery(query, clickNo) { * @returns {Promise} */ - async function getUserPostsByUser(UserId, reqParams) { +async function getUserPostsByUser(UserId, reqParams) { try { const res = await axios.get(`${baseUrl}/${postUser}/${UserId}`, { params: reqParams @@ -253,11 +252,19 @@ async function getPostsByQuery(query, clickNo) { * @param {String} postId * @returns {Promise} */ - async function getVotes(postId) { +async function getVotes(postId) { + try { + const { data } = await axios.get(`${baseUrl}/${getVoteUrl}/${postId}`); + return data; + } catch (err) { + console.log(err); + throw err; + } +} +async function getVotesType(postId) { try { - const { data } = await axios.get( - `${baseUrl}/${getVoteUrl}/${postId}`); + const { data } = await axios.get(`${baseUrl}/${getVoteTypeUrl}/${postId}`); return data; } catch (err) { console.log(err); @@ -266,12 +273,10 @@ async function getPostsByQuery(query, clickNo) { } async function setVotes(value, postId) { - try { - const { data } = await axios.post( - `${baseUrl}/${setVoteUrl}/${postId}`, - { value: value } - ); + const { data } = await axios.post(`${baseUrl}/${setVoteUrl}/${postId}`, { + value: value + }); return data; } catch (err) { console.log(err); @@ -282,16 +287,14 @@ async function setVotes(value, postId) { /** * Service to call PostCount Api * @author JasirTp - * @param {String} userId + * @param {String} userId * @returns {Promise} */ - async function getPostCount(userId, postDetails) { - +async function getPostCount(userId, postDetails) { try { - const { data } = await axios.get( - `${baseUrl}/${postCount}/${userId}`,{ - params: postDetails - }); + const { data } = await axios.get(`${baseUrl}/${postCount}/${userId}`, { + params: postDetails + }); return data; } catch (err) { console.log(err); @@ -327,6 +330,7 @@ async function getPostsByMultipleTags(tags, clickNo) { const PostService = { getPostById, + getVotesType, getPostsByUsers, getPostBySlug, adminReview,