-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHome.vue
203 lines (186 loc) · 4.81 KB
/
Home.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<template lang="pug">
main(v-if='articles.length', :class='$style.main')
ul.list-unstyled
li.border-b.my-4(
v-for='{ createdAt, id, number, title, labels: { nodes: labels } } of articles',
:key='id'
)
h5
router-link.heading-link(:to='`/article/${number}`') {{ $tt(title) }}
small.d-inline-flex.text-muted {{ createdAt | dateFormat }}
ul.list-unstyled.d-inline-flex
router-link.d-inline-flex.ml-2(
v-for='{ id, color, name } of labels',
tag='li',
:key='id',
:to='{ path: "/", query: { labels: name } }',
:style='{ backgroundColor: "#" + color }'
)
a.px-2.small(:style='{ color: $utils.invertColor(color) }') {{ name }}
nav(v-if='pageInfo.hasPreviousPage || pageInfo.hasNextPage')
ul.pagination.justify-content-end
router-link.page-item(
:to='prevRoute',
:class='{ disabled: !pageInfo.hasPreviousPage }',
tag='li'
)
a.page-link {{ $t('previous_page') }}
router-link.page-item(
:to='nextRoute',
:class='{ disabled: !pageInfo.hasNextPage }',
tag='li'
)
a.page-link {{ $t('next_page') }}
main.py-5.text-center.text-muted(v-else) {{ $t('no_content', [$route.query.labels ? $t('in_categories') : $route.query.search == null ? '' : $t('in_search')]) }}
</template>
<script lang="ts">
import { QueryOptions } from 'apollo-client'
import { Component, Vue } from 'vue-property-decorator'
import { Route } from 'vue-router'
import queries from 'queries.gql'
import {
AsyncDataFn,
Issue,
PageInfo,
Repository,
SearchResultItemConnection,
} from 'types'
import { getDefaultLabels } from 'utils'
interface Articles {
nodes: Issue[]
pageInfo: PageInfo
}
const getQueryOptions: AsyncDataFn<QueryOptions> = ({
apollo,
route,
store,
}) => {
const {
before,
after,
labels = getDefaultLabels({ apollo, store }).map(({ name }) => name),
search,
} = route.query
const { REPOSITORY } = store.getters
const searchText = search && (search as string).trim()
const variables = {
...REPOSITORY,
// eslint-disable-next-line no-magic-numbers
first: (!(before || after) || after) && 25,
// eslint-disable-next-line no-magic-numbers
last: before && 25,
before,
after,
labels,
search:
searchText &&
`repo:${REPOSITORY.owner}/${REPOSITORY.name} is:issue is:open ${searchText}`,
}
return {
query: search ? queries.search : queries.articles,
variables,
}
}
@Component({
async asyncData({ apollo, route, store, translate }) {
const { data } = await apollo.query<
| {
repository: Repository
}
| {
search: SearchResultItemConnection
}
>(getQueryOptions({ apollo, route, store }))
const articles =
'search' in data
? (data.search as Articles)
: (data.repository.issues as Articles)
for (const { title } of articles.nodes) translate(title)
},
title: (vm: Home) => vm.$t('home'),
translator: {
en: {
home: 'Home',
no_content: 'No content{ 0 }',
in_categories: ' in current categories',
in_search: ' under current search conditions',
previous_page: 'Previous',
next_page: 'Next',
},
zh: {
home: '首页',
no_content: '当前{ 0 }暂无内容',
in_categories: '分类下',
in_search: '搜索条件下',
previous_page: '上一页',
next_page: '下一页',
},
},
})
export default class Home extends Vue {
articles: Issue[] = null
pageInfo: PageInfo = null
get prevRoute() {
return {
path: '/',
query: {
before: this.pageInfo.startCursor,
search: this.$route.query.search,
},
}
}
get nextRoute() {
return {
path: '/',
query: {
after: this.pageInfo.endCursor,
search: this.$route.query.search,
},
}
}
setData() {
const data = this.$apollo.readQuery<
{
repository: Repository
} & {
search: SearchResultItemConnection
}
>(
getQueryOptions({
apollo: this.$apollo,
route: this.$route,
store: this.$store,
}),
)
const articles = data.search
? (data.search as Articles)
: (data.repository.issues as Articles)
this.articles = articles.nodes
this.pageInfo = articles.pageInfo
}
created() {
this.setData()
}
async beforeRouteUpdate(to: Route, _from: Route, next: () => void) {
await this.$apollo.query(
getQueryOptions({
apollo: this.$apollo,
route: to,
store: this.$store,
}),
)
next()
this.setData()
}
}
</script>
<style lang="scss" module>
@media (max-width: $grid-breakpoints-md) {
.main {
padding: {
top: 7px !important;
bottom: 7px !important;
}
}
}
</style>