index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <div class="md-layout md-aligment-center" style="margin: 4em 0">
  3. <!-- TOP Navigation-->
  4. <md-toolbar class="fixed-toolbar" elevation="1">
  5. <md-button @click="showLeftSidepanel = true" class="md-icon-button">
  6. <md-icon>menu</md-icon>
  7. </md-button>
  8. <nuxt-link class="md-primary md-title" to="/">
  9. NuxtNews
  10. </nuxt-link>
  11. <div class="md-toolbar-section-end">
  12. <md-button @click="$router.push('/login')">Login</md-button>
  13. <md-button @click="$router.push('/register')">Register</md-button>
  14. <md-button class="md-accent" @click="showRightSidepanel = true">Categorises</md-button>
  15. </div>
  16. </md-toolbar>
  17. <md-drawer md-fixed :md-active.sync="showLeftSidepanel">
  18. <md-toolbar :md-elevation="1">
  19. <span class="md-title">Personal Feed</span>
  20. </md-toolbar>
  21. <md-progress-bar v-if="loading" md-mode="indeterminate" />
  22. <md-field>
  23. <label for="country">Country</label>
  24. <md-select @input="changeCountry" :value="country" name="country" id="country">
  25. <md-option value="us">United States</md-option>
  26. <md-option value="ca">Canada</md-option>
  27. <md-option value="kr">Koread</md-option>
  28. <md-option value="ru">Russia</md-option>
  29. <md-option value="fr">France</md-option>
  30. </md-select>
  31. </md-field>
  32. </md-drawer>
  33. <md-drawer class="md-right" md-fixed :md-active.sync="showRightSidepanel">
  34. <md-toolbar :md-elevation="1">
  35. <span class="md-title">News Categories</span>
  36. </md-toolbar>
  37. <md-progress-bar v-if="loading" md-mode="indeterminate" />
  38. <md-list>
  39. <md-subheader class="md-primary">Categories</md-subheader>
  40. <md-list-item v-for="(newsCategory, i) in newsCategories" :key="i" @click="loadCategory(newsCategory.path)">
  41. <md-icon :class="newsCategory.path === category ? 'md-primary' : ''">
  42. {{ newsCategory.icon }}
  43. </md-icon>
  44. <span class="md-list-item-text">{{ newsCategory.name }}</span>
  45. </md-list-item>
  46. </md-list>
  47. </md-drawer>
  48. <!-- App Content -->
  49. <div class="md-layout-item md-size-95">
  50. <md-content class="md-layout md-gutter" style="background: #007998; padding: 1em;">
  51. <ul v-for="headline in headlines" :key="headline.id" class="md-layout-item md-large-size-25 md-medium-size-33 md-small-size-50 md-xsmall-size-100">
  52. <md-card style="margin-top: 1em;" md-width-hover>
  53. <md-ripple>
  54. <md-card-media md-ratio="16:9">
  55. <img :src="headline.urlToImage" :alt="headlines.title">
  56. </md-card-media>
  57. <md-card-header>
  58. <div class="md-title">
  59. <a :href="headline.url" target="_blank">{{ headline.title }}</a>
  60. </div>
  61. <div>
  62. {{ headline.source.name }}
  63. <md-icon class="small-icon">
  64. book
  65. </md-icon>
  66. </div>
  67. <div v-if="headline.author" class="md-subhead">
  68. {{ headline.author }}
  69. <md-icon class="small-icon">
  70. face
  71. </md-icon>
  72. </div>
  73. <div class="md-subhead">
  74. {{ headline.publishedAt }}
  75. <md-icon class="small-icon">
  76. alarm
  77. </md-icon>
  78. </div>
  79. </md-card-header>
  80. <md-card-content>
  81. {{ headline.description }}
  82. </md-card-content>
  83. <md-card-actions>
  84. <md-button class="md-icon-button">
  85. <md-icon>bookmark</md-icon>
  86. </md-button>
  87. <md-button class="md-icon-button">
  88. <md-icon>message</md-icon>
  89. </md-button>
  90. </md-card-actions>
  91. </md-ripple>
  92. </md-card>
  93. </ul>
  94. </md-content>
  95. </div>
  96. </div>
  97. </template>
  98. <script>
  99. export default {
  100. // async asyncData ({ app }) {
  101. // const topHeadlines = await app.$axios.$get('/api/top-headlines?country=us')
  102. // return { headlines: topHeadlines.articles }
  103. // }
  104. data: () => ({
  105. showRightSidepanel: false,
  106. showLeftSidepanel: false,
  107. newsCategories: [
  108. { name: 'Top Headlines', path: '', icon: 'today' },
  109. { name: 'Technology', path: 'technology', icon: 'keyboard' },
  110. { name: 'Business', path: 'business', icon: 'business_center' },
  111. { name: 'Entertainment', path: 'entertainment', icon: 'weekend' },
  112. { name: 'Health', path: 'health', icon: 'fastfood' },
  113. { name: 'Scinence', path: 'science', icon: 'fingerprint' },
  114. { name: 'Sports', path: 'sports', icon: 'golf_course' }
  115. ]
  116. }),
  117. computed: {
  118. headlines () {
  119. return this.$store.getters.headlines
  120. },
  121. category () {
  122. return this.$store.getters.category
  123. },
  124. loading () {
  125. return this.$store.getters.loading
  126. },
  127. country () {
  128. return this.$store.getters.country
  129. }
  130. },
  131. watch: {
  132. async country () {
  133. await this.$store.dispatch('loadHeadLines', `/api/top-headlines?country=${this.country}&category=${this.category}`)
  134. }
  135. },
  136. async fetch ({ store }) {
  137. await store.dispatch('loadHeadLines', `/api/top-headlines?country=${store.state.country}&category=${store.state.category}`)
  138. },
  139. methods: {
  140. async loadCategory (category) {
  141. this.$store.commit('setCategory', category)
  142. await this.$store.dispatch('loadHeadLines', `/api/top-headlines?country=${this.country}&category=${this.category}`)
  143. },
  144. changeCountry (country) {
  145. this.$store.commit('setCountry', country)
  146. }
  147. }
  148. }
  149. </script>
  150. <style scoped>
  151. .small-icon {
  152. font-size: 18px !important;
  153. }
  154. .fixed-toolbar {
  155. position: fixed;
  156. top: 0;
  157. z-index: 5;
  158. }
  159. </style>