/
MainBlock.vue
172 lines (165 loc) · 3.82 KB
/
MainBlock.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<template>
<div class="page-wrapper">
<template v-if="$fetchState.pending && !articles.length">
<div class="article-cards-wrapper">
<content-placeholders
v-for="p in 30"
:key="p"
rounded
class="article-card-block"
>
<content-placeholders-img />
<content-placeholders-text :lines="3" />
</content-placeholders>
</div>
</template>
<template v-else-if="$fetchState.error">
<inline-error-block :error="$fetchState.error" />
</template>
<template v-else>
<div class="article-cards-wrapper">
<article-card-block
v-for="(article, i) in articles"
:key="article.id"
v-observe-visibility="
i === articles.length - 1 ? lazyLoadArticles : false
"
:article="article"
class="article-card-block"
/>
</div>
</template>
<template v-if="$fetchState.pending && articles.length">
<div class="article-cards-wrapper">
<content-placeholders
v-for="p in 30"
:key="p"
rounded
class="article-card-block"
>
<content-placeholders-img />
<content-placeholders-text :lines="3" />
</content-placeholders>
</div>
</template>
<div v-if="noFurtherResult" class="no-more-results">
There are no more Recipes
</div>
</div>
</template>
<script>
import ArticleCardBlock from '@/components/blocks/ArticleCardBlock'
import InlineErrorBlock from '@/components/blocks/InlineErrorBlock'
export default {
components: {
ArticleCardBlock,
InlineErrorBlock
},
props: {
queryType: {
type: String,
default: 'index'
}
},
async fetch() {
let qry
switch (this.queryType) {
case 'index':
qry = this.$content()
break
case 'category':
qry = this.$content().where({
'categories.slug': { $contains: this.$route.params.slug }
})
break
case 'search':
qry = this.$content().search('title', this.$route.params.slug)
break
default:
qry = this.$content()
}
const recipes = await qry
.only([
'slug',
'featuredImage',
'title',
'serves',
'totalTime',
'updated',
'categories'
])
.sortBy('updated', 'desc')
.skip(this.skipCnt)
.limit(this.limitCnt)
.fetch()
if (recipes.length !== 0) {
this.articles.push(...recipes)
} else {
this.noFurtherResult = true
}
},
data() {
return {
lastVisible: null,
articles: [],
skipCnt: 0,
limitCnt: 10,
noFurtherResult: false
}
},
// activated() {
// // Call fetch again if last fetch more than 60 secs ago
// if (this.$fetchState.timestamp <= Date.now() - 60000) {
// this.$fetch()
// }
// },
methods: {
lazyLoadArticles(isVisible) {
if (isVisible) {
if (!this.noFurtherResult) {
this.skipCnt += this.limitCnt
this.$fetch()
}
// }
}
}
},
head() {
return {
title: 'CookingShooking'
}
}
}
</script>
<style lang="scss" scoped>
.page-wrapper {
max-width: $screen-xl;
margin: auto;
padding: 1rem;
min-height: 100vh;
}
.article-cards-wrapper {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
.article-card-block {
width: calc(100% - 2 * 1rem);
margin: 1rem;
margin-bottom: 1.5rem;
margin-top: 0.5rem;
@media (min-width: $screen-sm) {
width: calc(50% - 2 * 1rem);
}
@media (min-width: $screen-lg) {
width: calc(33.33333% - 2 * 1rem);
}
}
}
.no-more-results {
text-align: center;
color: grey;
margin: 1rem;
overflow: hidden;
box-shadow: -9px -9px 16px #f8fafe, 9px 9px 16px #ced2db;
}
</style>