_slug.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <div class="md-layout md-alignment-center" style="margin: 5em 0">
  3. <div class="md-layout-item md-size-75 md-small-size-80 md-xsmall-size-100">
  4. <!--Headline Markup-->
  5. <md-card>
  6. <md-card-media style="height: 300px" md-ratio="16:9">
  7. <img :src="headline.urlToImage" :alt="headline.title">
  8. </md-card-media>
  9. <md-card-header>
  10. <div class="md-title">
  11. <a :href="headline.url" target="_blank">{{ headline.title }}</a>
  12. </div>
  13. <div>
  14. {{ headline.source.name }}
  15. <md-icon>book</md-icon>
  16. </div>
  17. <span v-if="headline.author" class="md-subhead">
  18. {{ headline.author }}
  19. <md-icon>face</md-icon>
  20. </span>
  21. </md-card-header>
  22. <md-card-content>{{ headline.content }}</md-card-content>
  23. </md-card>
  24. <!-- Comment Form -->
  25. <form @submit.prevent="sendComment">
  26. <md-field>
  27. <label for="">Enter Your Comment</label>
  28. <md-textarea v-model="text" :disabled="loading || !user" />
  29. <md-icon>description</md-icon>
  30. </md-field>
  31. <md-button :disabled="loading || !user" class="md-primary md-raised" type="submit">
  32. Send Comment
  33. </md-button>
  34. </form>
  35. <!-- Comments -->
  36. <md-list class="md-triple-line" style="margin-top: 1em">
  37. <md-list-item v-for="comment in headline.comments" :key="comment.id">
  38. <md-avatar><img :src="comment.user.avatar" :alt="comment.user.username"></md-avatar>
  39. <div class="md-list-item-text">
  40. <span>{{ comment.user.username }}</span>
  41. <span>{{ comment.publishedAt | commentTimeToNow }}</span>
  42. <p>{{ comment.text }}</p>
  43. </div>
  44. <md-badge :md-content="comment.likes" class="md-primary" md-postion="bottom" />
  45. <md-button @click="likeComment(comment.id)" :disabled="loading || !user" class="md-icon-button">
  46. <md-icon>thumb_up</md-icon>
  47. </md-button>
  48. </md-list-item>
  49. </md-list>
  50. <!-- Back Button -->
  51. <md-button @click="$router.go(-1)" class="md-fixed md-fab-bottom-right md-fab md-primary">
  52. <md-icon>arrow_back</md-icon>
  53. </md-button>
  54. </div>
  55. </div>
  56. </template>
  57. <script>
  58. import uuidv4 from 'uuid/v4'
  59. export default {
  60. data: () => ({
  61. text: ''
  62. }),
  63. computed: {
  64. headline () {
  65. return this.$store.getters.headline
  66. },
  67. user () {
  68. return this.$store.getters.user
  69. },
  70. loading () {
  71. return this.$store.getters.loading
  72. }
  73. },
  74. async fetch ({ store, params }) {
  75. await store.dispatch('loadHeadline', params.slug)
  76. },
  77. methods: {
  78. async sendComment () {
  79. const comment = {
  80. id: uuidv4(),
  81. text: this.text,
  82. user: this.getCommentUserData(),
  83. publishedAt: Date.now(),
  84. likes: 0
  85. }
  86. await this.$store.dispatch('sendComment', comment)
  87. this.text = ''
  88. },
  89. async likeComment (commentId) {
  90. await this.$store.dispatch('likeComment', commentId)
  91. },
  92. getCommentUserData () {
  93. const commentUserData = { ...this.user }
  94. commentUserData.username = commentUserData.email.split('@')[0]
  95. return commentUserData
  96. }
  97. }
  98. }
  99. </script>