diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 73f4e8bd11..ef05f7b8ef 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -16,22 +16,23 @@ import { APIClient } from "../api/types" const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils -interface DataFetchStore { +interface DataFetchStore { rows: Row[] info: null schema: TableSchema | null loading: boolean loaded: boolean - query: SearchFilters | null + query: TQuery pageNumber: number cursor: null cursors: any[] resetKey: number error: null - definition?: T | null + definition?: TDefinition | null } -interface DataFetchDerivedStore extends DataFetchStore { +interface DataFetchDerivedStore + extends DataFetchStore { hasNextPage: boolean hasPrevPage: boolean supportsSearch: boolean @@ -39,6 +40,13 @@ interface DataFetchDerivedStore extends DataFetchStore { supportsPagination: boolean } +interface DataFetchParams { + API: APIClient + datasource: TDatasource + query: TQuery + options?: {} +} + /** * Parent class which handles the implementation of fetching data from an * internal table or datasource plus. @@ -46,7 +54,8 @@ interface DataFetchDerivedStore extends DataFetchStore { */ export default abstract class DataFetch< TDatasource extends {}, - TDefinition extends {} + TDefinition extends {}, + TQuery extends {} = SearchFilters > { API: APIClient features: { @@ -59,7 +68,7 @@ export default abstract class DataFetch< limit: number // Search config filter: UISearchFilter | LegacyFilter[] | null - query: SearchFilters | null + query: TQuery // Sorting config sortColumn: string | null sortOrder: SortOrder @@ -71,14 +80,14 @@ export default abstract class DataFetch< clientSideSorting: boolean clientSideLimiting: boolean } - store: Writable> - derivedStore: Readable> + store: Writable> + derivedStore: Readable> /** * Constructs a new DataFetch instance. * @param opts the fetch options */ - constructor(opts: { API: APIClient; datasource: TDatasource; options?: {} }) { + constructor(opts: DataFetchParams) { // Feature flags this.features = { supportsSearch: false, @@ -93,7 +102,7 @@ export default abstract class DataFetch< // Search config filter: null, - query: null, + query: opts.query, // Sorting config sortColumn: null, @@ -116,7 +125,7 @@ export default abstract class DataFetch< schema: null, loading: false, loaded: false, - query: null, + query: opts.query, pageNumber: 0, cursor: null, cursors: [], @@ -247,7 +256,7 @@ export default abstract class DataFetch< // Build the query let query = this.options.query if (!query) { - query = buildQuery(filter ?? undefined) + query = buildQuery(filter ?? undefined) as TQuery } // Update store @@ -485,7 +494,7 @@ export default abstract class DataFetch< * @param state the current store state * @return {boolean} whether there is a next page of data or not */ - hasNextPage(state: DataFetchStore): boolean { + hasNextPage(state: DataFetchStore): boolean { return state.cursors[state.pageNumber + 1] != null } diff --git a/packages/frontend-core/src/fetch/GroupUserFetch.js b/packages/frontend-core/src/fetch/GroupUserFetch.ts similarity index 67% rename from packages/frontend-core/src/fetch/GroupUserFetch.js rename to packages/frontend-core/src/fetch/GroupUserFetch.ts index e40b565728..77b5e1de43 100644 --- a/packages/frontend-core/src/fetch/GroupUserFetch.js +++ b/packages/frontend-core/src/fetch/GroupUserFetch.ts @@ -1,9 +1,23 @@ import { get } from "svelte/store" import DataFetch from "./DataFetch" import { TableNames } from "../constants" +import { APIClient } from "../api/types" -export default class GroupUserFetch extends DataFetch { - constructor(opts) { +interface GroupUserQuery { + groupId: string + emailSearch: string +} + +export default class GroupUserFetch extends DataFetch< + any, + any, + GroupUserQuery +> { + constructor(opts: { + API: APIClient + datasource: any + query: GroupUserQuery + }) { super({ ...opts, datasource: { @@ -12,6 +26,10 @@ export default class GroupUserFetch extends DataFetch { }) } + getSchema(_datasource: any, definition: any) { + return definition?.schema + } + determineFeatureFlags() { return { supportsSearch: true, @@ -28,11 +46,12 @@ export default class GroupUserFetch extends DataFetch { async getData() { const { query, cursor } = get(this.store) + try { const res = await this.API.getGroupUsers({ id: query.groupId, emailSearch: query.emailSearch, - bookmark: cursor, + bookmark: cursor ?? undefined, }) return { diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index 0fbda7a414..ef471aa8e4 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -7,7 +7,7 @@ import NestedProviderFetch from "./NestedProviderFetch.js" import FieldFetch from "./FieldFetch" import JSONArrayFetch from "./JSONArrayFetch.js" import UserFetch from "./UserFetch.js" -import GroupUserFetch from "./GroupUserFetch.js" +import GroupUserFetch from "./GroupUserFetch" import CustomFetch from "./CustomFetch.js" import QueryArrayFetch from "./QueryArrayFetch.js" import { Table, UIDatasource, UIFetchAPI } from "@budibase/types"