Add client SDK function to get a component action and clean up date range picker

This commit is contained in:
Andrew Kingston 2021-06-03 10:10:25 +01:00
parent 7f1b612e75
commit 280a09afd7
3 changed files with 30 additions and 10 deletions

View File

@ -9,6 +9,7 @@ import {
import { styleable } from "./utils/styleable" import { styleable } from "./utils/styleable"
import transition from "./utils/transition" import transition from "./utils/transition"
import { linkable } from "./utils/linkable" import { linkable } from "./utils/linkable"
import { getAction } from "./utils/getAction"
import Provider from "./components/Provider.svelte" import Provider from "./components/Provider.svelte"
import { ActionTypes } from "./constants" import { ActionTypes } from "./constants"
@ -22,6 +23,7 @@ export default {
styleable, styleable,
transition, transition,
linkable, linkable,
getAction,
Provider, Provider,
ActionTypes, ActionTypes,
} }

View File

@ -0,0 +1,19 @@
import { get } from "svelte/store"
import { getContext } from "svelte"
/**
* Gets a component action.
* @param id The component ID that provides the action
* @param type The action type to get
* @returns {null|*} The action function
*/
export const getAction = (id, type) => {
if (!id || !type) {
return null
}
const context = getContext("context")
if (!context) {
return null
}
return get(context)?.[`${id}_${type}`]
}

View File

@ -13,7 +13,9 @@
const dataContext = getContext("context") const dataContext = getContext("context")
const component = getContext("component") const component = getContext("component")
const { styleable, builderStore, ActionTypes } = getContext("sdk") const { styleable, builderStore, ActionTypes, getAction } = getContext("sdk")
const setQuery = getAction(dataProvider?.id, ActionTypes.SetDataProviderQuery)
const options = [ const options = [
"Last 1 day", "Last 1 day",
"Last 7 days", "Last 7 days",
@ -26,10 +28,11 @@
const updateDateRange = option => { const updateDateRange = option => {
const query = dataProvider?.state?.query const query = dataProvider?.state?.query
if (!query) { if (!query || !setQuery) {
return return
} }
value = option
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")
@ -45,7 +48,8 @@
low = dayjs.utc().subtract(6, "months") low = dayjs.utc().subtract(6, "months")
} }
const newQuery = { // Update data provider query with the new filter
setQuery({
...query, ...query,
range: { range: {
...query.range, ...query.range,
@ -54,15 +58,10 @@
low: low.format(), low: low.format(),
}, },
}, },
} })
const setQuery =
$dataContext[`${dataProvider.id}_${ActionTypes.SetDataProviderQuery}`]
if (setQuery) {
setQuery(newQuery)
}
} }
// Update the range on mount to the initial value
onMount(() => { onMount(() => {
updateDateRange(value) updateDateRange(value)
}) })