index.vue 6.5 KB

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