_slug.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 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. <!-- Back Button -->
  36. <md-button @click="$router.go(-1)" class="md-fixed md-fab-bottom-right md-fab md-primary">
  37. <md-icon>arrow_back</md-icon>
  38. </md-button>
  39. </div>
  40. </div>
  41. </template>
  42. <script>
  43. import uuidv4 from 'uuid/v4'
  44. export default {
  45. data: () => ({
  46. text: ''
  47. }),
  48. computed: {
  49. headline () {
  50. return this.$store.getters.headline
  51. },
  52. user () {
  53. return this.$store.getters.user
  54. },
  55. loading () {
  56. return this.$store.getters.loading
  57. }
  58. },
  59. async fetch ({ store, params }) {
  60. await store.dispatch('loadHeadline', params.slug)
  61. },
  62. methods: {
  63. async sendComment () {
  64. const comment = {
  65. id: uuidv4(),
  66. text: this.text,
  67. user: this.getCommentUserData(),
  68. publishedAt: Date.now(),
  69. likes: 0
  70. }
  71. await this.$store.dispatch('sendComment', comment)
  72. this.text = ''
  73. },
  74. getCommentUserData () {
  75. const commentUserData = { ...this.user }
  76. commentUserData.username = commentUserData.email.split('@')[0]
  77. return commentUserData
  78. }
  79. }
  80. }
  81. </script>