index.vue 7.5 KB

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