Browse Source

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

pull/1620/head
Andrew Kingston 5 years ago
parent
commit
05a32bc009
  1. 2
      packages/client/src/sdk.js
  2. 19
      packages/client/src/utils/getAction.js
  3. 19
      packages/standard-components/src/DateRangePicker.svelte

2
packages/client/src/sdk.js

@ -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,
}

19
packages/client/src/utils/getAction.js

@ -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}`]
}

19
packages/standard-components/src/DateRangePicker.svelte

@ -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)
})

Loading…
Cancel
Save