Skip to content

Add logic for posting comments locally and a part of adding Gif comments #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jun 25, 2020

Conversation

anhduc109
Copy link
Owner

No description provided.

Comment on lines +167 to +171
postComment(
parent: Int!
text: String!
isGif: Boolean!
): Comment
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new mutation for posting the comment

submitterId: string,
context: IGraphQlSchemaContext
) {
const id = Math.floor(Math.random() * 10000);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to make the random id in here for the local comment. However, this generate Id can be improved more

Comment on lines +33 to +40
const newComment = new CommentModel({
id,
parent: comment.parent,
text: comment.text,
isGif: comment.isGif,
creationTime: Date.now(),
submitterId,
});
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making new Comment variable before caching it

submitterId,
});

const parentNews = await context.NewsItemService.getNewsItem(comment.parent);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trying to get the parent news to add the new comment into it

Comment on lines +44 to +47
if (parentNews) {
parentNews.comments.unshift(id);
CacheSingleton.setNewsItem(id, parentNews);
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store new comments into parent news (locally)

Comment on lines +19 to +24
const [postComment] = useMutation(POST_COMMENT_MUTATION, {
onError(error) {
Router.push('/login');
},
variables: { id: 999, parent: newsItem.id, text, isGif },
});
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the GraphQL mutation for posting comment

Comment on lines +30 to +38
<form
method="post"
action="comment"
onSubmit={(e) => {
e.preventDefault();
text.trim() !== '' ? postComment() : alert('Cannot post an empty comment!');
return Router.reload();
}}
>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to handle some cases like User posting empty comment or user hasn't logged in yet

@@ -87,7 +88,7 @@ export class Comment extends React.Component<ICommentProps> {
<br />
<div className="comment">
<span className="c00">
<span>{renderHTML(text)}</span>
{isGif ? <img alt={text} src={text} /> : <span>{renderHTML(text)}</span>}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trying to display <img> as gif instead of span if isGif = true

Comment on lines +1 to +11
import gql from 'graphql-tag';

export const POST_COMMENT_MUTATION = gql`
mutation PostComment($parent: Int!, $text: String!, $isGif: Boolean!) {
postComment(parent: $parent, text: $text, isGif: $isGif) {
parent
text
isGif
}
}
`;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mutation for Post Comment

Comment on lines +92 to +97

postComment(_, comment, context) {
if (!context.userId) throw new Error('Must be logged in to post a comment.');

return context.CommentService.postComment(comment, context.userId, context);
},
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolver of posting comment. Added also error handler for not logging in

@anhduc109 anhduc109 merged commit f264f27 into master Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant