-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
postComment( | ||
parent: Int! | ||
text: String! | ||
isGif: Boolean! | ||
): Comment |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
const newComment = new CommentModel({ | ||
id, | ||
parent: comment.parent, | ||
text: comment.text, | ||
isGif: comment.isGif, | ||
creationTime: Date.now(), | ||
submitterId, | ||
}); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
if (parentNews) { | ||
parentNews.comments.unshift(id); | ||
CacheSingleton.setNewsItem(id, parentNews); | ||
} |
There was a problem hiding this comment.
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)
const [postComment] = useMutation(POST_COMMENT_MUTATION, { | ||
onError(error) { | ||
Router.push('/login'); | ||
}, | ||
variables: { id: 999, parent: newsItem.id, text, isGif }, | ||
}); |
There was a problem hiding this comment.
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
<form | ||
method="post" | ||
action="comment" | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
text.trim() !== '' ? postComment() : alert('Cannot post an empty comment!'); | ||
return Router.reload(); | ||
}} | ||
> |
There was a problem hiding this comment.
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>} |
There was a problem hiding this comment.
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
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 | ||
} | ||
} | ||
`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mutation for Post Comment
|
||
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); | ||
}, |
There was a problem hiding this comment.
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
No description provided.