Skip to content

Commit a0fad04

Browse files
committed
Refactor pagination
1 parent 70b539b commit a0fad04

File tree

3 files changed

+72
-63
lines changed

3 files changed

+72
-63
lines changed

frontend/components/containers/annotation/BottomNavigator.vue

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,23 @@
2424
<v-icon>mdi-book-open-outline</v-icon>
2525
</v-btn> -->
2626

27-
<v-btn @click="nextPage">
27+
<v-btn @click="nextPage(total)">
2828
<span>Next</span>
2929
<v-icon>mdi-chevron-right</v-icon>
3030
</v-btn>
3131
</v-bottom-navigation>
3232
</template>
3333

3434
<script>
35-
import { mapState, mapActions, mapMutations } from 'vuex'
35+
import { mapState, mapActions, mapMutations, mapGetters } from 'vuex'
3636
3737
export default {
38-
data() {
39-
return {
40-
page: 1,
41-
limit: 10
42-
}
43-
},
44-
4538
computed: {
4639
...mapState('documents', ['items', 'total']),
47-
48-
offset() {
49-
return Math.floor((this.page - 1) / this.limit) * this.limit
50-
},
51-
52-
current() {
53-
return (this.page - 1) % this.limit
54-
}
40+
...mapGetters('pagination', ['current', 'limit', 'offset', 'page'])
5541
},
5642
5743
watch: {
58-
page() {
59-
const checkpoint = {}
60-
checkpoint[this.$route.params.id] = this.page
61-
localStorage.setItem('checkpoint', JSON.stringify(checkpoint))
62-
},
6344
offset() {
6445
this.updateSearchOptions({
6546
limit: this.limit,
@@ -75,22 +56,18 @@ export default {
7556
},
7657
7758
created() {
78-
const checkpoint = JSON.parse(localStorage.getItem('checkpoint'))
79-
this.page = checkpoint[this.$route.params.id] ? checkpoint[this.$route.params.id] : 1
59+
this.initPage({
60+
projectId: this.$route.params.id
61+
})
8062
this.getDocumentList({
8163
projectId: this.$route.params.id
8264
})
8365
},
8466
8567
methods: {
8668
...mapActions('documents', ['getDocumentList']),
87-
...mapMutations('documents', ['setCurrent', 'updateSearchOptions']),
88-
prevPage() {
89-
this.page = Math.max(this.page - 1, 1)
90-
},
91-
nextPage() {
92-
this.page = Math.min(this.page + 1, this.total)
93-
}
69+
...mapActions('pagination', ['prevPage', 'nextPage', 'initPage']),
70+
...mapMutations('documents', ['setCurrent', 'updateSearchOptions'])
9471
}
9572
}
9673
</script>

frontend/components/containers/annotation/Paginator.vue

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
fab
3030
small
3131
v-on="on"
32-
@shortkey="nextPage"
33-
@click="nextPage"
32+
@shortkey="nextPage(total)"
33+
@click="nextPage(total)"
3434
>
3535
<v-icon>mdi-chevron-right</v-icon>
3636
</v-btn>
@@ -42,35 +42,16 @@
4242

4343
<script>
4444
import Vue from 'vue'
45-
import { mapState, mapActions, mapMutations } from 'vuex'
45+
import { mapState, mapActions, mapMutations, mapGetters } from 'vuex'
4646
Vue.use(require('vue-shortkey'))
4747
4848
export default {
49-
data() {
50-
return {
51-
page: 1,
52-
limit: 10
53-
}
54-
},
55-
5649
computed: {
5750
...mapState('documents', ['items', 'total']),
58-
59-
offset() {
60-
return Math.floor((this.page - 1) / this.limit) * this.limit
61-
},
62-
63-
current() {
64-
return (this.page - 1) % this.limit
65-
}
51+
...mapGetters('pagination', ['current', 'limit', 'offset', 'page'])
6652
},
6753
6854
watch: {
69-
page() {
70-
const checkpoint = {}
71-
checkpoint[this.$route.params.id] = this.page
72-
localStorage.setItem('checkpoint', JSON.stringify(checkpoint))
73-
},
7455
offset() {
7556
this.updateSearchOptions({
7657
limit: this.limit,
@@ -86,22 +67,18 @@ export default {
8667
},
8768
8869
created() {
89-
const checkpoint = JSON.parse(localStorage.getItem('checkpoint'))
90-
this.page = checkpoint[this.$route.params.id] ? checkpoint[this.$route.params.id] : 1
70+
this.initPage({
71+
projectId: this.$route.params.id
72+
})
9173
this.getDocumentList({
9274
projectId: this.$route.params.id
9375
})
9476
},
9577
9678
methods: {
9779
...mapActions('documents', ['getDocumentList']),
98-
...mapMutations('documents', ['setCurrent', 'updateSearchOptions']),
99-
prevPage() {
100-
this.page = Math.max(this.page - 1, 1)
101-
},
102-
nextPage() {
103-
this.page = Math.min(this.page + 1, this.total)
104-
}
80+
...mapActions('pagination', ['prevPage', 'nextPage', 'initPage']),
81+
...mapMutations('documents', ['setCurrent', 'updateSearchOptions'])
10582
}
10683
}
10784
</script>

frontend/store/pagination.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export const state = () => ({
2+
limit: 10,
3+
page: 1,
4+
projectId: null
5+
})
6+
7+
export const getters = {
8+
offset(state) {
9+
return Math.floor((state.page - 1) / state.limit) * state.limit
10+
},
11+
current(state) {
12+
return (state.page - 1) % state.limit
13+
},
14+
page(state) {
15+
return state.page
16+
},
17+
limit(state) {
18+
return state.limit
19+
}
20+
}
21+
22+
export const mutations = {
23+
updatePage(state, page) {
24+
state.page = page
25+
},
26+
savePage(state) {
27+
const checkpoint = {}
28+
checkpoint[state.projectId] = state.page
29+
localStorage.setItem('checkpoint', JSON.stringify(checkpoint))
30+
},
31+
loadPage(state) {
32+
const checkpoint = JSON.parse(localStorage.getItem('checkpoint'))
33+
state.page = checkpoint[state.projectId] ? checkpoint[state.projectId] : 1
34+
},
35+
setProjectId(state, projectId) {
36+
state.projectId = projectId
37+
}
38+
}
39+
40+
export const actions = {
41+
prevPage({ commit, state }) {
42+
const page = Math.max(state.page - 1, 1)
43+
commit('updatePage', page)
44+
commit('savePage')
45+
},
46+
nextPage({ commit, state }, total) {
47+
const page = Math.min(state.page + 1, total)
48+
commit('updatePage', page)
49+
commit('savePage')
50+
},
51+
initPage({ commit }, payload) {
52+
commit('setProjectId', payload.projectId)
53+
commit('loadPage')
54+
}
55+
}

0 commit comments

Comments
 (0)