Refactor how extending data provider queries works to support multiple extensions per key

This commit is contained in:
Andrew Kingston 2021-11-18 18:41:37 +00:00
parent 8f981704d6
commit 33e5e198b7
2 changed files with 34 additions and 21 deletions

View File

@ -275,11 +275,10 @@
allRows = res.rows allRows = res.rows
} }
const addQueryExtension = (key, operator, field, value) => { const addQueryExtension = (key, extension) => {
if (!key || !operator || !field) { if (!key || !extension) {
return return
} }
const extension = { operator, field, value }
queryExtensions = { ...queryExtensions, [key]: extension } queryExtensions = { ...queryExtensions, [key]: extension }
} }
@ -295,11 +294,13 @@
const extendQuery = (defaultQuery, extensions) => { const extendQuery = (defaultQuery, extensions) => {
const extensionValues = Object.values(extensions || {}) const extensionValues = Object.values(extensions || {})
let extendedQuery = { ...defaultQuery } let extendedQuery = { ...defaultQuery }
extensionValues.forEach(({ operator, field, value }) => { extensionValues.forEach(extension => {
extendedQuery[operator] = { Object.entries(extension || {}).forEach(([operator, fields]) => {
...extendedQuery[operator], extendedQuery[operator] = {
[field]: value, ...extendedQuery[operator],
} ...fields,
}
})
}) })
if (JSON.stringify(query) !== JSON.stringify(extendedQuery)) { if (JSON.stringify(query) !== JSON.stringify(extendedQuery)) {

View File

@ -13,15 +13,6 @@
const component = getContext("component") const component = getContext("component")
const { styleable, ActionTypes, getAction } = getContext("sdk") const { styleable, ActionTypes, getAction } = getContext("sdk")
$: addExtension = getAction(
dataProvider?.id,
ActionTypes.AddDataProviderQueryExtension
)
$: removeExtension = getAction(
dataProvider?.id,
ActionTypes.RemoveDataProviderQueryExtension
)
const options = [ const options = [
"Last 1 day", "Last 1 day",
"Last 7 days", "Last 7 days",
@ -32,10 +23,24 @@
] ]
let value = options.includes(defaultValue) ? defaultValue : "Last 30 days" let value = options.includes(defaultValue) ? defaultValue : "Last 30 days"
$: queryExtension = getQueryExtension(value) $: dataProviderId = dataProvider?.id
$: addExtension?.($component.id, "range", field, queryExtension) $: addExtension = getAction(
dataProviderId,
ActionTypes.AddDataProviderQueryExtension
)
$: removeExtension = getAction(
dataProviderId,
ActionTypes.RemoveDataProviderQueryExtension
)
$: queryExtension = getQueryExtension(field, value)
$: addExtension?.($component.id, queryExtension)
const getQueryExtension = (field, value) => {
if (!field || !value) {
return null
}
console.log("getting extension for " + value)
const getQueryExtension = value => {
let low = dayjs.utc().subtract(1, "year") let low = dayjs.utc().subtract(1, "year")
let high = dayjs.utc().add(1, "day") let high = dayjs.utc().add(1, "day")
@ -51,7 +56,14 @@
low = dayjs.utc().subtract(6, "months") low = dayjs.utc().subtract(6, "months")
} }
return { low: low.format(), high: high.format() } return {
range: {
[field]: {
low: low.format(),
high: high.format(),
},
},
}
} }
onDestroy(() => { onDestroy(() => {