index.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <div>
  3. <div class="main-content">
  4. <div class="container">
  5. <div class="columns is-mobile">
  6. <!-- posts -->
  7. <div class="column is-8">
  8. <div
  9. v-for="blog in publishedBlogs"
  10. :key="blog._id"
  11. class="section">
  12. <div class="post">
  13. <div @click="$router.push(`/blogs/${blog.slug}`)" class="post-header clickable">
  14. <h4 class="title is-4">{{blog.title}}</h4>
  15. <h5 class="subtitle is-5">{{blog.subtitle}}</h5>
  16. </div>
  17. <div class="post-content">
  18. by {{blog.author.name}}, {{blog.createdAt | formatDate}}
  19. </div>
  20. </div>
  21. </div>
  22. <!-- end of blog -->
  23. <!-- pagination -->
  24. <div class="section">
  25. <no-ssr placeholder="Loading...">
  26. <paginate
  27. v-model="currentPage"
  28. :page-count="pagination.pageCount"
  29. :click-handler="fetchBlogs"
  30. :prev-text="'Prev'"
  31. :next-text="'Next'"
  32. :container-class="'paginationContainer'">
  33. </paginate>
  34. </no-ssr>
  35. </div>
  36. <!-- end of pagination -->
  37. </div>
  38. <!-- side bar -->
  39. <div class="column is-4 is-narrow">
  40. <!-- featured -->
  41. <div class="section">
  42. <div class="sidebar">
  43. <div class="sidebar-header">
  44. <h4 class="title is-4">Featured Posts</h4>
  45. </div>
  46. <div class="sidebar-list">
  47. <!-- Featured Blogs -->
  48. <p
  49. v-for="fBlog in featuredBlogs"
  50. :key="fBlog._id">
  51. <nuxt-link :to="`/blogs/${fBlog.slug}`">
  52. {{fBlog.title}}
  53. </nuxt-link>
  54. </p>
  55. <!-- Featured Blogs -->
  56. </div>
  57. </div>
  58. </div>
  59. </div>
  60. <!-- end of side bar -->
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. </template>
  66. <script>
  67. import { mapState } from 'vuex';
  68. export default {
  69. head: {
  70. title: 'Amazing tech blogs | Nuxtjs'
  71. },
  72. computed: {
  73. ...mapState({
  74. publishedBlogs: state => state.blog.items.all,
  75. featuredBlogs: state => state.blog.items.featured,
  76. pagination: state => state.blog.pagination
  77. }),
  78. currentPage: {
  79. get() {
  80. return this.$store.state.blog.pagination.pageNum
  81. },
  82. set(value) {
  83. this.$store.commit('blog/setPage', value)
  84. }
  85. }
  86. },
  87. async fetch({store, query}) {
  88. // Try to get values from query
  89. const filter = {}
  90. const {pageNum, pageSize} = query
  91. if (pageNum && pageSize) {
  92. filter.pageNum = parseInt(pageNum, 10)
  93. filter.pageSize = parseInt(pageSize, 10)
  94. store.commit('blog/setPage', filter.pageNum)
  95. } else {
  96. // filter.pageNum = 1
  97. // filter.pageSize = 6
  98. // TODO: Maybe getters ?
  99. filter.pageNum = store.state.blog.pagination.pageNum
  100. filter.pageSize = store.state.blog.pagination.pageSize
  101. }
  102. await store.dispatch('blog/fetchBlogs', filter)
  103. await store.dispatch('blog/fetchFeaturedBlogs', {'filter[featured]': true})
  104. },
  105. methods: {
  106. setQueryPaginationParams() {
  107. const { pageSize, pageNum } = this.pagination
  108. this.$router.push({query: {pageNum, pageSize}})
  109. },
  110. async fetchBlogs() {
  111. const filter = {}
  112. filter.pageSize = this.pagination.pageSize
  113. filter.pageNum = this.pagination.pageNum
  114. // Here store the query params!
  115. const result = await this.$store.dispatch('blog/fetchBlogs', filter)
  116. if(result.isAxiosError !== true){
  117. this.setQueryPaginationParams()
  118. }
  119. },
  120. }
  121. }
  122. </script>
  123. <style scoped>
  124. .post-content {
  125. font-style: italic;
  126. }
  127. .pagination-content {
  128. display: flex;
  129. justify-content: flex-end;
  130. }
  131. .clickable {
  132. cursor: pointer;
  133. }
  134. #root {
  135. flex: 1 0 auto;
  136. }
  137. *:focus {
  138. outline: none;
  139. }
  140. a {
  141. transition: all .35s;
  142. color: #000;
  143. }
  144. .button:focus {
  145. border-color: #d74436;
  146. box-shadow: 0 0 0 0;
  147. }
  148. .input, .textarea, .input[type] {
  149. font-size: 1.1rem;
  150. }
  151. .input:focus, .textarea:focus, .input[type]:focus {
  152. border: 2px solid #d74436;
  153. }
  154. /* this is used when inline-styled content
  155. overlaps text backgrounds in a really ugly way */
  156. .buffer {
  157. padding-bottom: 1.1rem;
  158. }
  159. /* navigation */
  160. .nav {
  161. background-color: #0d0c0d;
  162. }
  163. .nav-left {
  164. padding-left: 2rem;
  165. }
  166. .nav-right, .nav-center {
  167. padding-right: 2rem;
  168. }
  169. a.nav-item.is-tab {
  170. font-weight: 700;
  171. font-size: 13px;
  172. text-transform: uppercase;
  173. color: #fff;
  174. padding: 0.4rem;
  175. }
  176. a.nav-item:hover {
  177. color: #d74436;
  178. }
  179. a.nav-item.is-tab:hover {
  180. border-bottom: 4px solid #d74436;
  181. }
  182. /* main content */
  183. .main-content {
  184. padding: 4rem 0 2rem 0;
  185. min-height: 800px
  186. }
  187. .main-content .container {
  188. padding: 0 2rem 2rem 2rem;
  189. }
  190. /* section */
  191. .section {
  192. padding: 0 0 2rem 0;
  193. }
  194. .section-header {
  195. padding-bottom: 3rem;
  196. }
  197. .section-header .title {
  198. text-transform: uppercase;
  199. color: #4a4a4a;
  200. font-size: 1.3rem;
  201. }
  202. .section-header a {
  203. color: #d74436;
  204. font-weight: 700;
  205. }
  206. .section-header a:hover {
  207. color: #e50076;
  208. }
  209. /* sidebar */
  210. .sidebar-header {
  211. border-color: #d74436;
  212. padding-bottom: 1rem;
  213. border-bottom: 4px solid #d74436;
  214. }
  215. .sidebar-header .title, .sidebar-header-single .title {
  216. font-weight: 700;
  217. text-transform: uppercase;
  218. font-size: 1.3rem;
  219. }
  220. .sidebar-list p, .sidebar-list-single p {
  221. font-size: 1.1rem;
  222. font-weight: 300;
  223. padding-bottom: 0.8rem;
  224. }
  225. .sidebar-list a {
  226. color: #4a4a4a;
  227. }
  228. .sidebar-list, .post-content, .sidebar-list-single {
  229. padding-top: 1.4rem;
  230. }
  231. .sidebar-list-nav {
  232. padding-top: 1rem;
  233. }
  234. .sidebar-list-nav .is-tab {
  235. padding-right: 1rem;
  236. }
  237. .sidebar-footer-single {
  238. padding-top: 2rem;
  239. }
  240. .sidebar-footer-single a {
  241. color: #000;
  242. font-weight: 700;
  243. text-transform: uppercase;
  244. font-size: 1.1rem;
  245. border-right: 4px solid #d74436;
  246. padding-right: 1rem;
  247. }
  248. .sidebar-footer-single a:hover {
  249. color: #363636;
  250. }
  251. /* post */
  252. .post-header, .sidebar-header-single {
  253. border-color: #d74436;
  254. padding-left: 1rem;
  255. border-left: 4px solid #d74436;
  256. }
  257. .post-header .title {
  258. font-weight: 700;
  259. font-size: 1.8rem;
  260. color: rgba(0,0,0,.84)!important;
  261. fill: rgba(0,0,0,.84)!important;
  262. }
  263. .post-header .subtitle, .sidebar-header-single .subtitle {
  264. font-size: 1.1rem;
  265. }
  266. .post-content p, .post-single-content p {
  267. margin-bottom: 0.8rem;
  268. }
  269. .post-content, .post-single-content {
  270. font-size: 1.1rem;
  271. font-weight: 300;
  272. }
  273. /* override */
  274. .post-single-content form p:nth-child(even) {
  275. border-right: 0;
  276. }
  277. .post-single-content form label {
  278. text-transform: uppercase;
  279. color: #4a4a4a;
  280. padding-bottom: 0.2rem;
  281. }
  282. .post-single-content form .input[type] {
  283. padding-top: 0.2rem;
  284. }
  285. .post-single-content p:nth-child(even) {
  286. border-right: 4px solid #d74436;
  287. padding-right: 1rem;
  288. }
  289. .post-content a {
  290. color: #d74436;
  291. }
  292. .card-content-form form {
  293. padding-top: 1.5rem;
  294. }
  295. .post-footer {
  296. padding: 1.5rem 0 0 0;
  297. }
  298. /* pagination */
  299. .pagination-content {
  300. border-right: 4px solid #d74436;
  301. padding-right: 1rem;
  302. }
  303. .pagination-link.is-current {
  304. background-color: #d74436;
  305. border-color: #d74436;
  306. }
  307. </style>