2025-01-02 13:33:24 +01:00
|
|
|
import { SortOrder, UIView, ViewV2, ViewV2Type } from "@budibase/types"
|
2025-01-02 16:27:43 +01:00
|
|
|
import DataFetch from "./DataFetch"
|
2023-08-08 14:13:27 +02:00
|
|
|
import { get } from "svelte/store"
|
2025-01-02 15:36:27 +01:00
|
|
|
import { helpers } from "@budibase/shared-core"
|
2023-07-19 10:16:12 +02:00
|
|
|
|
2025-01-02 13:33:24 +01:00
|
|
|
export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> {
|
2025-01-08 12:49:17 +01:00
|
|
|
async determineFeatureFlags() {
|
2023-08-01 16:34:02 +02:00
|
|
|
return {
|
|
|
|
supportsSearch: true,
|
|
|
|
supportsSort: true,
|
|
|
|
supportsPagination: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-08 14:27:13 +01:00
|
|
|
async getDefinition() {
|
|
|
|
const { datasource } = this.options
|
|
|
|
|
2023-07-19 10:16:12 +02:00
|
|
|
try {
|
2023-08-15 18:07:14 +02:00
|
|
|
const res = await this.API.viewV2.fetchDefinition(datasource.id)
|
|
|
|
return res?.data
|
2025-01-02 12:33:54 +01:00
|
|
|
} catch (error: any) {
|
2023-08-16 12:00:14 +02:00
|
|
|
this.store.update(state => ({
|
|
|
|
...state,
|
|
|
|
error,
|
|
|
|
}))
|
2023-07-19 10:16:12 +02:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-08 12:55:26 +01:00
|
|
|
getDefaultSortColumn() {
|
2023-08-21 12:56:58 +02:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2023-07-19 10:16:12 +02:00
|
|
|
async getData() {
|
2024-09-20 14:59:50 +02:00
|
|
|
const { datasource, limit, sortColumn, sortOrder, sortType, paginate } =
|
|
|
|
this.options
|
2023-10-13 20:06:53 +02:00
|
|
|
const { cursor, query, definition } = get(this.store)
|
|
|
|
|
2024-10-10 15:52:13 +02:00
|
|
|
// If this is a calculation view and we have no calculations, return nothing
|
|
|
|
if (
|
2025-01-02 12:33:54 +01:00
|
|
|
definition?.type === ViewV2Type.CALCULATION &&
|
2025-01-02 15:36:27 +01:00
|
|
|
!Object.values(definition.schema || {}).some(
|
|
|
|
helpers.views.isCalculationField
|
|
|
|
)
|
2024-10-10 15:52:13 +02:00
|
|
|
) {
|
|
|
|
return {
|
|
|
|
rows: [],
|
|
|
|
hasNextPage: false,
|
|
|
|
cursor: null,
|
|
|
|
error: null,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 15:13:30 +02:00
|
|
|
// If sort/filter params are not defined, update options to store the
|
2023-10-13 20:06:53 +02:00
|
|
|
// params built in to this view. This ensures that we can accurately
|
2023-10-20 15:13:30 +02:00
|
|
|
// compare old and new params and skip a redundant API call.
|
2025-01-02 12:33:54 +01:00
|
|
|
if (!sortColumn && definition?.sort?.field) {
|
2023-10-13 20:06:53 +02:00
|
|
|
this.options.sortColumn = definition.sort.field
|
2025-01-02 12:33:54 +01:00
|
|
|
this.options.sortOrder = definition.sort.order || SortOrder.ASCENDING
|
2023-10-13 20:06:53 +02:00
|
|
|
}
|
2024-09-09 17:36:31 +02:00
|
|
|
|
2023-07-19 10:16:12 +02:00
|
|
|
try {
|
2025-01-02 15:48:04 +01:00
|
|
|
const request = {
|
2025-01-08 13:05:27 +01:00
|
|
|
query,
|
2023-08-08 14:13:27 +02:00
|
|
|
paginate,
|
|
|
|
limit,
|
|
|
|
bookmark: cursor,
|
|
|
|
sort: sortColumn,
|
2025-01-02 15:36:27 +01:00
|
|
|
sortOrder: sortOrder,
|
2023-08-08 14:13:27 +02:00
|
|
|
sortType,
|
2025-01-02 15:48:04 +01:00
|
|
|
}
|
|
|
|
if (paginate) {
|
|
|
|
const res = await this.API.viewV2.fetch(datasource.id, {
|
|
|
|
...request,
|
|
|
|
paginate,
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
rows: res?.rows || [],
|
|
|
|
hasNextPage: res?.hasNextPage || false,
|
|
|
|
cursor: res?.bookmark || null,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const res = await this.API.viewV2.fetch(datasource.id, {
|
|
|
|
...request,
|
|
|
|
paginate,
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
rows: res?.rows || [],
|
|
|
|
hasNextPage: false,
|
|
|
|
cursor: null,
|
|
|
|
}
|
2023-08-08 14:13:27 +02:00
|
|
|
}
|
2023-07-19 10:16:12 +02:00
|
|
|
} catch (error) {
|
2023-08-08 14:13:27 +02:00
|
|
|
return {
|
|
|
|
rows: [],
|
|
|
|
hasNextPage: false,
|
2024-10-10 15:52:13 +02:00
|
|
|
cursor: null,
|
2023-08-08 14:13:27 +02:00
|
|
|
error,
|
|
|
|
}
|
2023-07-19 10:16:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|