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 transition from "./utils/transition"
import { linkable } from "./utils/linkable"
import { getAction } from "./utils/getAction"
import Provider from "./components/Provider.svelte"
import { ActionTypes } from "./constants"
@ -22,6 +23,7 @@ export default {
styleable,
transition,
linkable,
getAction,
Provider,
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 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 = [
"Last 1 day",
"Last 7 days",
@ -26,10 +28,11 @@
const updateDateRange = option => {
const query = dataProvider?.state?.query
if (!query) {
if (!query || !setQuery) {
return
}
value = option
let low = dayjs.utc().subtract(1, "year")
let high = dayjs.utc().add(1, "day")
@ -45,7 +48,8 @@
low = dayjs.utc().subtract(6, "months")
}
const newQuery = {
// Update data provider query with the new filter
setQuery({
...query,
range: {
...query.range,
@ -54,15 +58,10 @@
low: low.format(),
},
},
})
}
const setQuery =
$dataContext[`${dataProvider.id}_${ActionTypes.SetDataProviderQuery}`]
if (setQuery) {
setQuery(newQuery)
}
}
// Update the range on mount to the initial value
onMount(() => {
updateDateRange(value)
})