2022-07-01 02:39:26 +12:00
|
|
|
import { writable } from "svelte/store"
|
|
|
|
|
|
|
|
function defaultValue() {
|
|
|
|
return {
|
|
|
|
nextPage: null,
|
|
|
|
page: undefined,
|
|
|
|
hasPrevPage: false,
|
|
|
|
hasNextPage: false,
|
2022-07-01 04:02:50 +12:00
|
|
|
loading: false,
|
2022-07-01 02:39:26 +12:00
|
|
|
pageNumber: 1,
|
|
|
|
pages: [],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createPaginationStore() {
|
|
|
|
const { subscribe, set, update } = writable(defaultValue())
|
|
|
|
|
|
|
|
function prevPage() {
|
|
|
|
update(state => {
|
|
|
|
state.pageNumber--
|
|
|
|
state.nextPage = state.pages.pop()
|
|
|
|
state.page = state.pages[state.pages.length - 1]
|
|
|
|
state.hasPrevPage = state.pageNumber > 1
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function nextPage() {
|
|
|
|
update(state => {
|
|
|
|
state.pageNumber++
|
|
|
|
state.page = state.nextPage
|
|
|
|
state.pages.push(state.page)
|
|
|
|
state.hasPrevPage = state.pageNumber > 1
|
|
|
|
return state
|
|
|
|
})
|
2022-06-30 06:11:00 +12:00
|
|
|
}
|
|
|
|
|
2022-07-01 02:39:26 +12:00
|
|
|
function fetched(hasNextPage, nextPage) {
|
|
|
|
update(state => {
|
|
|
|
state.hasNextPage = hasNextPage
|
|
|
|
state.nextPage = nextPage
|
2022-07-01 04:02:50 +12:00
|
|
|
state.loading = false
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function loading(loading = true) {
|
|
|
|
update(state => {
|
|
|
|
state.loading = loading
|
2022-07-01 02:39:26 +12:00
|
|
|
return state
|
|
|
|
})
|
2022-06-30 06:11:00 +12:00
|
|
|
}
|
|
|
|
|
2022-07-01 02:39:26 +12:00
|
|
|
function reset() {
|
|
|
|
set(defaultValue())
|
2022-06-30 06:11:00 +12:00
|
|
|
}
|
|
|
|
|
2022-07-01 02:39:26 +12:00
|
|
|
return {
|
|
|
|
subscribe,
|
|
|
|
prevPage,
|
|
|
|
nextPage,
|
|
|
|
fetched,
|
2022-07-01 04:02:50 +12:00
|
|
|
loading,
|
2022-07-01 02:39:26 +12:00
|
|
|
reset,
|
2022-06-30 06:11:00 +12:00
|
|
|
}
|
|
|
|
}
|