index.vue 8.6 KB

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