index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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="showSearchDialog=true" class="md-primary">
  29. Search
  30. </md-button>
  31. <md-button @click="showRightSidepanel = true" class="md-accent">
  32. Categorises
  33. </md-button>
  34. </div>
  35. </md-toolbar>
  36. <!-- Search Dialog -->
  37. <md-dialog :md-active.sync="showSearchDialog">
  38. <md-dialog-title>Search Headlines</md-dialog-title>
  39. <div class="md-layout" style="padding: 1em">
  40. <md-field>
  41. <label for="">Search Term(s)</label>
  42. <md-input v-model="query" placeholder="Use quotes for exact matches, AND / OR / NOW fro multiple terms" maxlength="30" />
  43. </md-field>
  44. <md-datepicker v-model="fromDate">
  45. <label for="">Select starting date(opt)</label>
  46. </md-datepicker>
  47. <md-datepicker v-model="toDate">
  48. <label for="">Select ending date(opt)</label>
  49. </md-datepicker>
  50. <md-field>
  51. <label for="">Sort search results by criteria(optional)</label>
  52. <md-select id="sortBy" v-model="sortBy" name="sortBy" md-dense>
  53. <md-option value="publishedAt">
  54. Newest (default)
  55. </md-option>
  56. <md-option value="relevany">
  57. Relevant
  58. </md-option>
  59. <md-option value="popularity">
  60. Popular
  61. </md-option>
  62. </md-select>
  63. </md-field>
  64. </div>
  65. <md-dialog-actions>
  66. <md-button @click="showSearchDialog=false" class="md-accent">
  67. Cancel
  68. </md-button>
  69. <md-button @click="searchHeadlines" class="md-primary">
  70. Search
  71. </md-button>
  72. </md-dialog-actions>
  73. </md-dialog>
  74. <md-drawer :md-active.sync="showLeftSidepanel" md-fixed>
  75. <md-toolbar :md-elevation="1">
  76. <span class="md-title">Personal Feed</span>
  77. </md-toolbar>
  78. <md-progress-bar v-if="loading" md-mode="indeterminate" />
  79. <md-field>
  80. <label for="country">Country</label>
  81. <md-select id="country" @input="changeCountry" :value="country" name="country">
  82. <md-option value="us">
  83. United States
  84. </md-option>
  85. <md-option value="ca">
  86. Canada
  87. </md-option>
  88. <md-option value="kr">
  89. Koread
  90. </md-option>
  91. <md-option value="ru">
  92. Russia
  93. </md-option>
  94. <md-option value="fr">
  95. France
  96. </md-option>
  97. </md-select>
  98. </md-field>
  99. <!-- Default Markup (if Feed Empty) -->
  100. <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">
  101. <md-button to="/login" class="md-primary md-raised">
  102. Login
  103. </md-button>
  104. </md-empty-state>
  105. <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" />
  106. <!-- Feed Content -->
  107. <md-list v-else v-for="headline in feed" :key="headline.id" class="md-triple-line">
  108. <md-list-item>
  109. <md-avatar><img :src="headline.utlToImage" :alt="headline.title"></md-avatar>
  110. <div class="md-list-item-text">
  111. <span><a :href="headline.url" target="_blank">{{ headline.title }}</a></span>
  112. <span>{{ headline.source.name }}</span>
  113. <span @click="saveHeadline(headline)">View comments</span>
  114. </div>
  115. <md-button @click="removeHeadlineFromFeed(headline)" class="md-icon-button md-list-action">
  116. <md-icon class="md-accent">
  117. delete
  118. </md-icon>
  119. </md-button>
  120. </md-list-item>
  121. <md-divider class="md-inset" />
  122. </md-list>
  123. </md-drawer>
  124. <!-- News categories (Right Drawer) -->
  125. <md-drawer :md-active.sync="showRightSidepanel" class="md-right" md-fixed>
  126. <md-toolbar :md-elevation="1">
  127. <span class="md-title">News Categories</span>
  128. </md-toolbar>
  129. <md-progress-bar v-if="loading" md-mode="indeterminate" />
  130. <md-list>
  131. <md-subheader class="md-primary">
  132. Categories
  133. </md-subheader>
  134. <md-list-item v-for="(newsCategory, i) in newsCategories" :key="i" @click="loadCategory(newsCategory.path)">
  135. <md-icon :class="newsCategory.path === category ? 'md-primary' : ''">
  136. {{ newsCategory.icon }}
  137. </md-icon>
  138. <span class="md-list-item-text">{{ newsCategory.name }}</span>
  139. </md-list-item>
  140. </md-list>
  141. </md-drawer>
  142. <!-- App Content -->
  143. <div class="md-layout-item md-size-95">
  144. <md-content class="md-layout md-gutter" style="background: #007998; padding: 1em;">
  145. <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">
  146. <md-card style="margin-top: 1em;" md-width-hover>
  147. <md-ripple>
  148. <md-card-media md-ratio="16:9">
  149. <img :src="headline.urlToImage" :alt="headlines.title">
  150. </md-card-media>
  151. <md-card-header>
  152. <div class="md-title">
  153. <a :href="headline.url" target="_blank">{{ headline.title }}</a>
  154. </div>
  155. <div @click="loadSource(headline.source.id)">
  156. {{ headline.source.name }}
  157. <md-icon class="small-icon">
  158. book
  159. </md-icon>
  160. </div>
  161. <div v-if="headline.author" class="md-subhead">
  162. {{ headline.author }}
  163. <md-icon class="small-icon">
  164. face
  165. </md-icon>
  166. </div>
  167. <div class="md-subhead">
  168. {{ headline.publishedAt | publishedTimeToNow }}
  169. <md-icon class="small-icon">
  170. alarm
  171. </md-icon>
  172. </div>
  173. </md-card-header>
  174. <md-card-content>
  175. {{ headline.description }}
  176. </md-card-content>
  177. <md-card-actions>
  178. <md-button @click="addHeadlineToFeed(headline)" :class="isInFeed(headline.title)" class="md-icon-button">
  179. <md-icon>bookmark</md-icon>
  180. </md-button>
  181. <md-button @click="saveHeadline(headline)" class="md-icon-button">
  182. <md-icon>message</md-icon>
  183. </md-button>
  184. </md-card-actions>
  185. </md-ripple>
  186. </md-card>
  187. </ul>
  188. </md-content>
  189. </div>
  190. </div>
  191. </template>
  192. <script>
  193. export default {
  194. // async asyncData ({ app }) {
  195. // const topHeadlines = await app.$axios.$get('/api/top-headlines?country=us')
  196. // return { headlines: topHeadlines.articles }
  197. // }
  198. data: () => ({
  199. showRightSidepanel: false,
  200. showLeftSidepanel: false,
  201. showSearchDialog: false,
  202. newsCategories: [
  203. { name: 'Top Headlines', path: '', icon: 'today' },
  204. { name: 'Technology', path: 'technology', icon: 'keyboard' },
  205. { name: 'Business', path: 'business', icon: 'business_center' },
  206. { name: 'Entertainment', path: 'entertainment', icon: 'weekend' },
  207. { name: 'Health', path: 'health', icon: 'fastfood' },
  208. { name: 'Scinence', path: 'science', icon: 'fingerprint' },
  209. { name: 'Sports', path: 'sports', icon: 'golf_course' }
  210. ],
  211. query: '',
  212. fromDate: '',
  213. toDate: '',
  214. sortBy: ''
  215. }),
  216. computed: {
  217. headlines () {
  218. return this.$store.getters.headlines
  219. },
  220. category () {
  221. return this.$store.getters.category
  222. },
  223. loading () {
  224. return this.$store.getters.loading
  225. },
  226. country () {
  227. return this.$store.getters.country
  228. },
  229. user () {
  230. return this.$store.getters.user
  231. },
  232. feed () {
  233. return this.$store.getters.feed
  234. },
  235. source () {
  236. return this.$store.getters.source
  237. },
  238. isAuthenticated () {
  239. return this.$store.getters.isAuthenticated
  240. }
  241. },
  242. watch: {
  243. async country () {
  244. await this.$store.dispatch('loadHeadLines', `/api/top-headlines?country=${this.country}&category=${this.category}`)
  245. }
  246. },
  247. async fetch ({ store }) {
  248. await store.dispatch('loadHeadLines', `/api/top-headlines?country=${store.state.country}&category=${store.state.category}`)
  249. await store.dispatch('loadUserFeed')
  250. },
  251. methods: {
  252. async loadCategory (category) {
  253. this.$store.commit('setCategory', category)
  254. await this.$store.dispatch('loadHeadLines', `/api/top-headlines?country=${this.country}&category=${this.category}`)
  255. },
  256. changeCountry (country) {
  257. this.$store.commit('setCountry', country)
  258. },
  259. logoutUser () {
  260. this.$store.dispatch('logoutUser')
  261. },
  262. async loadSource (sourceId) {
  263. if (sourceId) {
  264. this.$store.commit('setSource', sourceId)
  265. await this.$store.dispatch('loadHeadLines', `/api/top-headlines?sources=${this.source}`)
  266. }
  267. },
  268. async removeHeadlineFromFeed (headline) {
  269. await this.$store.dispatch('removeHeadlineFromFeed', headline)
  270. },
  271. async saveHeadline (headline) {
  272. await this.$store.dispatch('saveHeadline', headline).then(() => {
  273. this.$router.push(`/headlines/${headline.slug}`)
  274. })
  275. },
  276. async searchHeadlines () {
  277. await this.$store.dispatch('loadHeadLines', `/api/top-headlines?q=${this.query}&from=${this.dateToISOString(this.fromDate)}&to=${this.dateToISOString(this.toDate)}&sortBy=${this.sortBy}`)
  278. this.showSearchDialog = false
  279. },
  280. async addHeadlineToFeed (headline) {
  281. if (this.user) {
  282. await this.$store.dispatch('addHeadlineToFeed', headline)
  283. }
  284. },
  285. isInFeed (title) {
  286. const inFeed = this.feed.findIndex(headline => headline.title === title) > -1
  287. return inFeed ? 'md-primary' : ''
  288. },
  289. dateToISOString (date) {
  290. if (date) {
  291. return new Date(date).toISOString()
  292. }
  293. }
  294. }
  295. }
  296. </script>
  297. <style scoped>
  298. .small-icon {
  299. font-size: 18px !important;
  300. }
  301. .fixed-toolbar {
  302. position: fixed;
  303. top: 0;
  304. z-index: 5;
  305. }
  306. </style>