Guest User

Untitled

a guest
Aug 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. <template>
  2. <div class="Post__create">
  3. <section class="Post__create-comment">
  4. <div class="Post__create-comment-input">
  5. <textarea aria-label="Add a comment…" ref="input" placeholder="Add a comment…" @keydown.enter.prevent="saveComment" v-model="text" class="Post__create-comment-field">
  6. </textarea>
  7. </div>
  8. </section >
  9. </div>
  10. </template>
  11.  
  12.  
  13. <script>
  14. import { ALL_POSTS_QUERY, CREATE_COMMENT_MUTATION } from '../constants/graphql'
  15. import { GC_USER_ID} from '../constants/settings'
  16.  
  17. export default {
  18. name: 'CreateComment',
  19. props: ['postId', 'userId'],
  20. data () {
  21. return {
  22. loading: 0,
  23. text:""
  24. }
  25. },
  26. methods: {
  27. focus: function () {
  28. this.$refs.input.focus()
  29. },
  30. saveComment () {
  31. console.log("comment", this.text);
  32. console.log("comment", this.postId);
  33. console.log("comment", this.userId);
  34. const {text} = this.$data
  35. const userId = this.userId
  36. const postId = this.postId
  37. this.$apollo.mutate({
  38. mutation: CREATE_COMMENT_MUTATION,
  39. variables: {
  40. text,
  41. postId,
  42. userId
  43. },
  44. update: (store, {data: { createComment}}) => {
  45. this.updateStoreAfterComment(store, createComment, postId)
  46. }
  47. })
  48. this.text = "";
  49. },
  50. updateStoreAfterComment(store, createComment, postId) {
  51. const data = store.readQuery({query: ALL_POSTS_QUERY})
  52. const commentedPost = data.allPosts.find(post => post.id === postId)
  53. commentedPost.comments.push(createComment)
  54. store.writeQuery({ query: ALL_POSTS_QUERY, data })
  55. }
  56. }
  57. }
  58. </script>
Add Comment
Please, Sign In to add comment