Skip to content

Commit

Permalink
Merge pull request #635 from jasirneoito/634/fix-blog-vote-error
Browse files Browse the repository at this point in the history
634/fix blog vote error
  • Loading branch information
jasirtp authored Dec 2, 2021
2 parents 089880c + 2fcad17 commit d143ab2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 53 deletions.
72 changes: 38 additions & 34 deletions component/layout/BlogPost/BlogPost.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
}
};

Expand All @@ -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();
Expand All @@ -100,7 +95,6 @@ export default function BlogPost(props) {
text: item.innerText
};
});
// console.log({ h2Tags });
setHeadings(hTags);
hljs.highlightAll();
}, []);
Expand Down Expand Up @@ -141,7 +135,12 @@ export default function BlogPost(props) {
<div className={styles.postHeader}>
<div className={styles.wrapVote}>
<div className={styles.vote}>
<a onClick={() => handleClick(1)} className="uo">
<a
onClick={() => {
handleClick(1);
}}
className="up"
>
<svg
width="37"
height="28"
Expand All @@ -151,12 +150,17 @@ export default function BlogPost(props) {
>
<path
d="M14.805 2.013L.977 22.21C.338 23.144 0 24.084 0 24.865c0 1.51 1.212 2.445 3.241 2.445h29.886c2.027 0 3.237-.933 3.237-2.44 0-.783-.339-1.707-.98-2.642L21.557 2.02C20.666.72 19.467 0 18.18 0c-1.286 0-2.484.712-3.375 2.013z"
fill={voteType == 1 ? "#ff590f" : "#CFCFCF"}
fill={voteType == "up" ? "#ff590f" : "#CFCFCF"}
/>
</svg>
</a>
<span className="count">{voteCount}</span>
<a onClick={() => handleClick(-1)} className="down">
<a
onClick={() => {
handleClick(-1);
}}
className="down"
>
<svg
width="37"
height="28"
Expand All @@ -166,7 +170,7 @@ export default function BlogPost(props) {
>
<path
d="M14.805 25.751L.977 5.553C.338 4.62 0 3.68 0 2.899 0 1.389 1.212.454 3.241.454h29.886c2.027 0 3.237.933 3.237 2.44 0 .782-.339 1.707-.98 2.642L21.557 25.744c-.891 1.3-2.09 2.02-3.377 2.02-1.286 0-2.484-.712-3.375-2.013z"
fill={voteType == -1 ? "#ff590f" : "#CFCFCF"}
fill={voteType == "down" ? "#ff590f" : "#CFCFCF"}
/>
</svg>
</a>
Expand Down
2 changes: 2 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion pages/[blogPost].js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
40 changes: 22 additions & 18 deletions services/PostService.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
postCount,
setVoteUrl,
postUser,
getVoteTypeUrl,
postBySlug,
changeStatusUrl,
adminReviewUrl,
Expand Down Expand Up @@ -136,8 +137,6 @@ async function deletePostById(userCookie, postId) {
}
}



// async function changePostStatus(userCookie, postId, statusUpdate) {
// try {
// const { data } = await axios.post(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -327,6 +330,7 @@ async function getPostsByMultipleTags(tags, clickNo) {

const PostService = {
getPostById,
getVotesType,
getPostsByUsers,
getPostBySlug,
adminReview,
Expand Down

0 comments on commit d143ab2

Please sign in to comment.