Update data fetch models to use constructor to determine feature flags

This commit is contained in:
Andrew Kingston 2021-12-17 10:49:12 +00:00
parent 78a3f807a3
commit d6120de81b
2 changed files with 22 additions and 10 deletions

View File

@ -13,9 +13,10 @@ import { fetchTableDefinition } from "api"
* For other types of datasource, this class is overridden and extended. * For other types of datasource, this class is overridden and extended.
*/ */
export default class TableFetch { export default class TableFetch {
SupportsSearch = false // Feature flags
SupportsSort = false supportsSearch = false
SupportsPagination = false supportsSort = false
supportsPagination = false
// Config // Config
options = { options = {
@ -51,13 +52,20 @@ export default class TableFetch {
/** /**
* Constructs a new DataFetch instance. * Constructs a new DataFetch instance.
* @param opts the fetch options * @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 = {
...this.options, ...this.options,
...opts, ...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" // Bind all functions to properly scope "this"
this.getData = this.getData.bind(this) this.getData = this.getData.bind(this)
this.getInitialData = this.getInitialData.bind(this) this.getInitialData = this.getInitialData.bind(this)
@ -158,17 +166,17 @@ export default class TableFetch {
let { rows, hasNextPage, cursor } = await this.getData() let { rows, hasNextPage, cursor } = await this.getData()
// If we don't support searching, do a client search // If we don't support searching, do a client search
if (!this.SupportsSearch) { if (!this.supportsSearch) {
rows = luceneQuery(rows, query) rows = luceneQuery(rows, query)
} }
// If we don't support sorting, do a client-side sort // If we don't support sorting, do a client-side sort
if (!this.SupportsSort) { if (!this.supportsSort) {
rows = luceneSort(rows, sortColumn, sortOrder, sortType) rows = luceneSort(rows, sortColumn, sortOrder, sortType)
} }
// If we don't support pagination, do a client-side limit // If we don't support pagination, do a client-side limit
if (!this.SupportsPagination) { if (!this.supportsPagination) {
rows = luceneLimit(rows, limit) rows = luceneLimit(rows, limit)
} }

View File

@ -3,9 +3,13 @@ import DataFetch from "./DataFetch.js"
import { searchTable } from "api" import { searchTable } from "api"
export default class TableFetch extends DataFetch { export default class TableFetch extends DataFetch {
SupportsSearch = true constructor(opts) {
SupportsSort = true super(opts, {
SupportsPagination = true supportsSearch: true,
supportsSort: true,
supportsPagination: true,
})
}
async getData() { async getData() {
const { datasource, limit, sortColumn, sortOrder, sortType, paginate } = const { datasource, limit, sortColumn, sortOrder, sortType, paginate } =