index.vue 6.3 KB

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