Skip to content

Commit

Permalink
feat: the ability to fetch everything with limit 0/Infinity
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Dec 3, 2023
1 parent 6b9e9db commit b41a73f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
27 changes: 19 additions & 8 deletions packages/ui/src/components/MagnetarTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ function clearState(): void {
fetchMore()
}
const hasFetchLimit = computed<boolean>(
() => props.pagination.limit <= 0 || props.pagination.limit === Infinity
)
const minH = ref(26)
const minW = ref(26)
const tableEl = ref(null)
Expand All @@ -167,7 +170,7 @@ const { height, width } = useElementSize(tableEl)
*/
async function setMinTableHeight() {
await nextTick()
if (collectionInstance.value.data.size >= props.pagination.limit) {
if (!hasFetchLimit.value || collectionInstance.value.data.size >= props.pagination.limit) {
if (minH.value === 26 && height.value > 26) minH.value = height.value
if (minW.value === 26 && width.value > 26) minW.value = width.value
}
Expand All @@ -182,12 +185,10 @@ defineExpose({ activeCollection })
/** never throws */
async function fetchMore() {
fetchState.value = 'fetching'
const collection = activeCollection.value
let collection = activeCollection.value
if (hasFetchLimit.value) collection = collection.limit(props.pagination.limit)
try {
await collection
.limit(props.pagination.limit)
.startAfter(collection.fetched.cursor)
.fetch({ force: true })
await collection.startAfter(collection.fetched.cursor).fetch({ force: true })
await collection.fetchCount()
fetchState.value = collection.fetched.reachedEnd ? 'end' : 'ok'
// set new state
Expand Down Expand Up @@ -257,11 +258,21 @@ async function setOrderBy(
const showingFiltersCode = ref(false)
const pageIndex = ref(0)
const pageCountFetched = computed(() => Math.ceil(allData.value.length / props.pagination.limit))
const pageCount = computed(() => Math.ceil(collectionInstance.value.count / props.pagination.limit))
const pageCountFetched = computed(() =>
!hasFetchLimit.value
? allData.value.length
? 1
: 0
: Math.ceil(allData.value.length / props.pagination.limit)
)
const pageCount = computed(() =>
!hasFetchLimit.value ? 1 : Math.ceil(collectionInstance.value.count / props.pagination.limit)
)
const allData = computed(() => [...collectionInstance.value.data.values()])
const rows = computed(() => {
if (!hasFetchLimit.value) return allData.value
const { pagination } = props
if (pagination.kind === 'previous-next') {
return allData.value.slice(
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export type MUIColumn<T extends Record<string, any>, Label = string> = {
}

export type MUIPagination = {
/** Infinity, 0, or any other negative number means it will fetch without limit on one page */
limit: number
/** @default 'fetch-more' */
kind?: 'fetch-more' | 'previous-next'
Expand Down

0 comments on commit b41a73f

Please sign in to comment.