Skip to content

Commit 385b0a6

Browse files
committed
update project
1 parent 99f4db1 commit 385b0a6

File tree

5 files changed

+83
-22
lines changed

5 files changed

+83
-22
lines changed

app-pm2.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"apps": [
3+
{
4+
"name": "techtalk",
5+
"script": "npm",
6+
"args": "start"
7+
}
8+
]
9+
}

common/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ function pluralize(time, label) {
3131
}
3232
return time + label + 's ago'
3333
}
34+
35+
export function getRandomArrayElement(arr, n) {
36+
const shuffled = arr.sort(() => 0.5 - Math.random())
37+
38+
// Get sub-array of first n elements after shuffled
39+
return shuffled.slice(0, n)
40+
}

components/ListPostItem.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class="__title"
66
:to="{
77
name: 'p-slug',
8-
params: { post: post, slug: post.slug }
8+
params: { post: post, slug: post.slug, random_posts: random_posts }
99
}"
1010
v-html="post.title.rendered"
1111
></NuxtLink>
@@ -36,6 +36,10 @@ export default {
3636
post: {
3737
type: Object,
3838
default: () => {}
39+
},
40+
randomPosts: {
41+
type: Array,
42+
default: () => []
3943
}
4044
},
4145
computed: {

pages/blog.vue

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
<div>
33
<div class="list-news-wrap">
44
<ul class="list-news">
5-
<ListPostItem v-for="post in posts" :key="post.id" :post="post" />
5+
<ListPostItem
6+
v-for="post in posts"
7+
:key="post.id"
8+
:post="post"
9+
:random_posts="random_posts"
10+
/>
611
</ul>
712
<Pagination :current-page="currentPage" :total-pages="totalPages" />
813
</div>
@@ -12,6 +17,8 @@
1217
<script>
1318
import ListPostItem from '~/components/ListPostItem'
1419
import Pagination from '~/components/Pagination.vue'
20+
import { getRandomArrayElement } from '~/common/utils'
21+
1522
export default {
1623
name: 'Blog',
1724
components: { ListPostItem, Pagination },
@@ -20,6 +27,7 @@ export default {
2027
data() {
2128
return {
2229
posts: [],
30+
random_posts: [],
2331
currentPage: 1,
2432
totalPosts: 0,
2533
totalPages: 0,
@@ -29,15 +37,14 @@ export default {
2937
asyncData({ $axios, query, error }) {
3038
const page = +query.page || 1
3139
return $axios
32-
.get(
33-
`https://techtalk.vn/wp-json/wp/v2/posts?_embed&page=${page}&per_page=20`
34-
)
40+
.get(`//techtalk.vn/wp-json/wp/v2/posts?_embed&page=${page}&per_page=20`)
3541
.then((res) => {
3642
return {
3743
posts: res.data,
3844
totalPosts: +res.headers['x-wp-total'],
3945
totalPages: +res.headers['x-wp-totalpages'],
4046
currentPage: +page,
47+
random_posts: getRandomArrayElement(res.data, 10),
4148
title:
4249
'Result page ' +
4350
+page +
@@ -73,7 +80,7 @@ export default {
7380
}
7481
</script>
7582

76-
<style scoped lang="scss">
83+
<style lang="scss">
7784
.list-news {
7885
list-style-type: none;
7986
padding: 0;

pages/p/_slug.vue

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,28 @@
22
<div v-if="isValidPost()">
33
<h1 class="title" v-html="post.title.rendered"></h1>
44
<div class="news-content" v-html="post.content.rendered"></div>
5+
6+
<ul class="list-news">
7+
<ListPostItem
8+
v-for="p in random_posts"
9+
:key="p.id"
10+
:post="p"
11+
:random_posts="random_posts"
12+
/>
13+
</ul>
514
</div>
615
</template>
716

817
<script>
18+
import ListPostItem from '~/components/ListPostItem'
19+
920
export default {
1021
name: 'P',
22+
components: { ListPostItem },
1123
data() {
1224
return {
13-
post: {}
25+
post: {},
26+
random_posts: []
1427
}
1528
},
1629
head() {
@@ -58,27 +71,48 @@ export default {
5871
}
5972
: {}
6073
},
61-
asyncData({ $axios, params, error }) {
74+
async asyncData({ $axios, params, error }) {
6275
console.log('asyncData post single', params)
6376
6477
if (!params.post) {
6578
const slug = params.slug
66-
return $axios
67-
.get(`https://techtalk.vn/wp-json/wp/v2/posts?_embed&slug=${slug}`)
68-
.then((res) => {
69-
if (!res.data.length)
70-
error({ statusCode: 404, message: 'Post not found!' })
71-
return {
72-
post: res.data.length ? res.data[0] : []
73-
}
74-
})
75-
.catch(function(ex) {
76-
console.log('parsing failed', ex)
77-
error({ statusCode: 404, message: ex })
78-
})
79+
try {
80+
const res = await $axios.get(
81+
`//techtalk.vn/wp-json/wp/v2/posts?_embed&slug=${slug}`
82+
)
83+
const resRd = await $axios.get(
84+
`//techtalk.vn/wp-json/wp/v2/posts?_embed&categories=${
85+
res.data[0].categories[0]
86+
}`
87+
)
88+
89+
if (!res.data.length)
90+
error({ statusCode: 404, message: 'Post not found!' })
91+
return {
92+
post: res.data.length ? res.data[0] : [],
93+
random_posts: resRd.data.length ? resRd.data : []
94+
}
95+
} catch (ex) {
96+
console.log('parsing failed', ex)
97+
error({ statusCode: 404, message: ex })
98+
}
99+
// return $axios
100+
// .get(`//techtalk.vn/wp-json/wp/v2/posts?_embed&slug=${slug}`)
101+
// .then((res) => {
102+
// if (!res.data.length)
103+
// error({ statusCode: 404, message: 'Post not found!' })
104+
// return {
105+
// post: res.data.length ? res.data[0] : []
106+
// }
107+
// })
108+
// .catch(function(ex) {
109+
// console.log('parsing failed', ex)
110+
// error({ statusCode: 404, message: ex })
111+
// })
79112
} else {
80113
return {
81-
post: params.post
114+
post: params.post,
115+
random_posts: params.random_posts
82116
}
83117
}
84118
},

0 commit comments

Comments
 (0)