generated from antfu-collective/vitesse
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.vue
98 lines (84 loc) · 2.36 KB
/
index.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
<script setup lang="ts">
import { useQuery, useResult } from '@vue/apollo-composable'
import { gql } from 'graphql-tag'
import { PokemonResults, Pokemons } from '~/libs/models/pokemon'
import LoaderPokemonList from '~/components/loader/LoaderPokemonList.vue'
const limit = 15
const currentPage = ref(1)
const enabledFetch = ref(false)
const { result, variables, fetchMore, refetch, loading, error } = useQuery<PokemonResults>(gql`
query pokemons($offset: Int, $limit: Int) {
pokemons(limit: $limit, offset: $offset) {
count
next
previous
status
message
results {
url
name
image,
artwork,
dreamworld
}
}
}
`,
{
limit,
offset: 0,
}, () => ({ enabled: enabledFetch.value, notifyOnNetworkStatusChange: true }))
const count = computed(() => useResult(result, { results: [] } as Pokemons).value.count || 0)
const pokemons = computed(() => useResult(result, { results: [] } as Pokemons).value.results)
const router = useRouter()
const route = useRoute()
const fetchPokemons = () => {
enabledFetch.value = true
refetch()
}
const init = () => {
const page = route.query.page
if (typeof page === 'string')
currentPage.value = +page || 1
variables.value = {
limit,
offset: (currentPage.value - 1) * limit,
}
fetchPokemons()
}
watch([currentPage], ([curPage]) => {
router.push({ name: 'index', query: { page: curPage } })
fetchMore({
variables: {
offset: (curPage - 1) * limit,
},
updateQuery: (previousResult, { fetchMoreResult }) => {
if (!fetchMoreResult) return previousResult
return fetchMoreResult
},
})
})
init()
</script>
<template>
<div class="sm:hidden my-4 w-full text-center" :class="{'opacity-70 pointer-events-none': loading}">
<Pagination v-model:current-page="currentPage" :total-data="count" :page-size="limit" />
</div>
<LoaderPokemonList v-if="loading" :length="limit" />
<div v-else-if="error">
{{ error.message }}
</div>
<div v-else-if="pokemons.length > 0">
<PokemonList :pokemons="pokemons" />
</div>
<div v-else>
No data found
</div>
<div class="my-4 w-full text-center" :class="{'opacity-70 pointer-events-none': loading}">
<Pagination v-model:current-page="currentPage" :total-data="count" :page-size="limit" />
</div>
</template>
<route lang="yaml">
meta:
layout: home
</route>