Skip to content

Commit

Permalink
fix: nan issue vote api connected and author details #421
Browse files Browse the repository at this point in the history
fixed some author details issue and vote api connectd
  • Loading branch information
jasurobo committed Nov 26, 2021
1 parent 76a69e1 commit 9255eac
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 43 deletions.
2 changes: 1 addition & 1 deletion component/layout/BlogListing/ListingFeatured.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function ListingFeatured({ blog }) {
</p>

<Details
username={blog?.user.username}
username={blog?.user.user_name}
avatar={blog?.user.avatar}
/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion component/layout/BlogListing/ListingItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function ListingItem(props) {
</p>
</div>
<Details
username={item.user.username}
username={item.user.user_name}
avatar={item.user.avatar}
/>
</div>
Expand Down
64 changes: 39 additions & 25 deletions component/layout/BlogPost/BlogPost.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,55 @@ 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("");
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 [voteCount, setVoteCount] = useState(
props.blog.votes?.filter((item) => item.type == "up").length -
props.blog.votes?.filter((item) => item.type == "down").length
);
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;
} catch (err) {
notify(err?.response?.data?.message ?? err?.message, 'error');
}
};

useEffect(() => {
getVoteCount();
}, []);


/**
* function triggered during upvotes/downvotes
* @author sNkr-10
* @returns {Promise}
*/
const setVotes = async (type) => {
const setVotes = async (value) => {
try {
if (props.userId) {
const response = await PostService.setVotes(
type,
props.blog._id,
props.token
);
if (response.posts != null) {
setVoteCount(
response.posts.votes.filter((item) => item.type == "up").length -
response.posts.votes.filter((item) => item.type == "down").length
);

setVoteType(
response.posts.votes.find((item) => item.userId == props.userId)
.type
);
value,
props.blog.id,
);
if (response.data != null) {
getVoteCount();
setVoteType(value);
}
return response;
}
Expand All @@ -70,7 +84,7 @@ export default function BlogPost(props) {

const handleClick = (value) => {
// Prevents users to vote their own posts.
if (props.userId != props.blog.primaryAuthor._id) {
if (props.userId != props.blog.user.id) {
setVotes(value);
}
!props.userId && notify("Please login for further actions", 'dark');
Expand Down Expand Up @@ -127,7 +141,7 @@ export default function BlogPost(props) {
<div className={styles.postHeader}>
<div className={styles.wrapVote}>
<div className={styles.vote}>
<a onClick={() => handleClick("up")} className="uo">
<a onClick={() => handleClick(1)} className="uo">
<svg
width="37"
height="28"
Expand All @@ -137,12 +151,12 @@ 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 == "up" ? "#ff590f" : "#CFCFCF"}
fill={voteType == 1 ? "#ff590f" : "#CFCFCF"}
/>
</svg>
</a>
<span className="count">{voteCount}</span>
<a onClick={() => handleClick("down")} className="down">
<a onClick={() => handleClick(-1)} className="down">
<svg
width="37"
height="28"
Expand All @@ -152,7 +166,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 == "down" ? "#ff590f" : "#CFCFCF"}
fill={voteType == -1 ? "#ff590f" : "#CFCFCF"}
/>
</svg>
</a>
Expand Down
1 change: 0 additions & 1 deletion component/layout/BlogPost/SectionAuthor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import notify from "../../../lib/notify";

export default function SectionAuthor({ primaryAuthor, postCount }) {
const { bio, user_name, avatar } = primaryAuthor;
console.log(postCount);
useEffect(() => {
}, [primaryAuthor]);
return (
Expand Down
4 changes: 4 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ const changePasswordUrl = `${apiVerUrl}/update-password`;
const publishedPostsUrl = "api/posts/published";
// const publishedPostsCountUrl = "api/posts/published/count";
const logoutUrl = `${apiVerUrl}/logout`;
const setVoteUrl = `${apiVerUrl}/post-vote`;
const getVoteUrl = `${apiVerUrl}/post-votes`;

const configVars = {
baseUrl,
setVoteUrl,
getVoteUrl,
postTagsUrl,
postSkillsUrl,
postSkillUrl,
Expand Down
8 changes: 4 additions & 4 deletions pages/[blogPost].js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export async function getServerSideProps(context) {
try {
const slug = context.params.blogPost;
const response = await PostService.getPostBySlug(slug);
console.log(response.data.tags[0].name, "Plost");
const tagName = response.data.tags[0].name;
const { data } = await PostService.getPostByTags(tagName);
return {
Expand All @@ -42,7 +41,8 @@ export async function getServerSideProps(context) {
}

export default function BlogListing({ blog , relatedPosts }) {
console.log("bloga", relatedPosts, 'error');
// console.log("bloga", relatedPosts, 'error');
const [postsCount, setPostsCount] = useState(0);
const {
html,
user,
Expand All @@ -58,7 +58,7 @@ export default function BlogListing({ blog , relatedPosts }) {
// const {}
const cookies = new Cookies();
const userCookie = cookies.get("userNullcast");
// console.log({ blog });

return (
<>
<SiteHeader />
Expand Down Expand Up @@ -139,7 +139,7 @@ export default function BlogListing({ blog , relatedPosts }) {
blog={blog}
html={html}
/>
<SectionAuthor primaryAuthor={user} />
<SectionAuthor primaryAuthor={user} postCount={postsCount} />
<SectionRelated title="Related Blogs" posts={relatedPosts} />
<SectionSwag />
<SiteFooter />
Expand Down
1 change: 0 additions & 1 deletion pages/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export async function getServerSideProps() {
// with_table: "users, tags"
};
const { data } = await PostService.getPostsByUsers(postParams);
console.log(data.posts,'json')
if (data.posts.length > 0) {
return {
props: {
Expand Down
29 changes: 19 additions & 10 deletions services/PostService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
allPostsUrl,
postUrl,
postsUrl,
getVoteUrl,
s3Url,
setVoteUrl,
postUser,
postBySlug,
changeStatusUrl,
Expand Down Expand Up @@ -252,18 +254,24 @@ async function getPostsByQuery(query, clickNo) {
* @param {String} token
* @returns {Promise}
*/
async function setVotes(type, postId, token) {
const url = getUrl();
async function getVotes(postId) {

try {
const { data } = await axios.put(
`${url}/${postUrl}/vote/${postId}`,
{ type: type },
{
headers: {
"x-access-token": `${token}`
}
}
const { data } = await axios.get(
`${baseUrl}/${getVoteUrl}/${postId}`);
return data;
} catch (err) {
console.log(err);
throw err;
}
}

async function setVotes(value, postId) {

try {
const { data } = await axios.post(
`${baseUrl}/${setVoteUrl}/${postId}`,
{ value: value }
);
return data;
} catch (err) {
Expand Down Expand Up @@ -315,6 +323,7 @@ const PostService = {
getUserPostsByUser,
// getPublishedPostsCountByUserId,
setVotes,
getVotes,
getPostsByMultipleTags
};

Expand Down

0 comments on commit 9255eac

Please sign in to comment.