From d6120de81b90c819ee70111b80adedcd24829ba8 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 17 Dec 2021 10:49:12 +0000 Subject: [PATCH] Update data fetch models to use constructor to determine feature flags --- packages/client/src/utils/fetch/DataFetch.js | 22 +++++++++++++------ packages/client/src/utils/fetch/TableFetch.js | 10 ++++++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/client/src/utils/fetch/DataFetch.js b/packages/client/src/utils/fetch/DataFetch.js index d33b2a3011..8b966a2678 100644 --- a/packages/client/src/utils/fetch/DataFetch.js +++ b/packages/client/src/utils/fetch/DataFetch.js @@ -13,9 +13,10 @@ import { fetchTableDefinition } from "api" * For other types of datasource, this class is overridden and extended. */ export default class TableFetch { - SupportsSearch = false - SupportsSort = false - SupportsPagination = false + // Feature flags + supportsSearch = false + supportsSort = false + supportsPagination = false // Config options = { @@ -51,13 +52,20 @@ export default class TableFetch { /** * Constructs a new DataFetch instance. * @param opts the fetch options + * @param flags the datasource feature flags */ - constructor(opts) { + constructor(opts, flags) { + // Merge options with their default values this.options = { ...this.options, ...opts, } + // Update feature flags + this.supportsSearch = flags?.supportsSearch || false + this.supportsSort = flags?.supportsSort || false + this.supportsPagination = flags?.supportsPagination || false + // Bind all functions to properly scope "this" this.getData = this.getData.bind(this) this.getInitialData = this.getInitialData.bind(this) @@ -158,17 +166,17 @@ export default class TableFetch { let { rows, hasNextPage, cursor } = await this.getData() // If we don't support searching, do a client search - if (!this.SupportsSearch) { + if (!this.supportsSearch) { rows = luceneQuery(rows, query) } // If we don't support sorting, do a client-side sort - if (!this.SupportsSort) { + if (!this.supportsSort) { rows = luceneSort(rows, sortColumn, sortOrder, sortType) } // If we don't support pagination, do a client-side limit - if (!this.SupportsPagination) { + if (!this.supportsPagination) { rows = luceneLimit(rows, limit) } diff --git a/packages/client/src/utils/fetch/TableFetch.js b/packages/client/src/utils/fetch/TableFetch.js index f64c40fd44..5217ec5152 100644 --- a/packages/client/src/utils/fetch/TableFetch.js +++ b/packages/client/src/utils/fetch/TableFetch.js @@ -3,9 +3,13 @@ import DataFetch from "./DataFetch.js" import { searchTable } from "api" export default class TableFetch extends DataFetch { - SupportsSearch = true - SupportsSort = true - SupportsPagination = true + constructor(opts) { + super(opts, { + supportsSearch: true, + supportsSort: true, + supportsPagination: true, + }) + } async getData() { const { datasource, limit, sortColumn, sortOrder, sortType, paginate } =