Simplify data contexts

This commit is contained in:
Andrew Kingston 2020-11-24 11:28:31 +00:00
parent def6054b26
commit 199c3409c9
9 changed files with 21 additions and 24 deletions

View File

@ -20,8 +20,8 @@ export const fetchDatasource = async (datasource, dataContext) => {
rows = await fetchViewData(datasource)
} else if (type === "link") {
rows = await fetchRelationshipData({
rowId: dataContext?.data?._id,
tableId: dataContext?.data?.tableId,
rowId: dataContext?._id,
tableId: dataContext?.tableId,
fieldName,
})
}

View File

@ -26,10 +26,10 @@
}
// Enriches data bindings to real values based on data context
const enrichDataBindings = (data, bindings, props) => {
const enrichDataBindings = (dataContexts, dataBindings, props) => {
const state = {
...data,
...bindings,
...dataContexts,
...dataBindings,
}
let enrichedProps = {}
Object.entries(props).forEach(([key, value]) => {
@ -49,9 +49,9 @@
$: componentProps = extractValidProps(definition)
$: children = definition._children
$: id = definition._id
$: dataStore = getContext("data")
$: dataContext = getContext("data")
$: enrichedProps = enrichDataBindings(
$dataStore,
$dataContext,
$bindingStore,
componentProps
)
@ -64,7 +64,7 @@
$: componentStore.set({
id,
styles: { ...definition._styles, id },
dataContext: $dataStore.data,
dataContext: $dataContext.data,
})
</script>

View File

@ -6,7 +6,6 @@
import { capitalise } from "./helpers"
const { styleable, screenStore, API } = getContext("sdk")
const dataContextStore = getContext("data")
const component = getContext("component")
export let wide = false
@ -15,11 +14,11 @@
let schema
let fields = []
$: getContextDetails($dataContextStore)
$: getContextDetails($component.dataContext)
const getContextDetails = async dataContext => {
row = dataContext?.data
if (row) {
if (dataContext) {
row = dataContext
const tableDefinition = await API.fetchTableDefinition(row.tableId)
schema = tableDefinition.schema
fields = Object.keys(schema)

View File

@ -3,7 +3,6 @@
import { isEmpty } from "lodash/fp"
const { API, styleable, DataProvider } = getContext("sdk")
const dataContextStore = getContext("data")
const component = getContext("component")
export let datasource = []
@ -12,7 +11,7 @@
onMount(async () => {
if (!isEmpty(datasource)) {
rows = await API.fetchDatasource(datasource, $dataContextStore)
rows = await API.fetchDatasource(datasource, $component.dataContext)
}
})
</script>

View File

@ -5,7 +5,7 @@
import { isEmpty } from "lodash/fp"
const { API } = getContext("sdk")
const dataContext = getContext("data")
const component = getContext("component")
export let title
export let datasource
@ -35,7 +35,7 @@
// Fetch, filter and sort data
const schema = (await API.fetchTableDefinition(datasource.tableId)).schema
const result = await API.fetchDatasource(datasource, $dataContext)
const result = await API.fetchDatasource(datasource, $component.dataContext)
const reducer = row => (valid, column) => valid && row[column] != null
const hasAllColumns = row => allCols.reduce(reducer(row), true)
const data = result

View File

@ -5,7 +5,7 @@
import { isEmpty } from "lodash/fp"
const { API } = getContext("sdk")
const dataContext = getContext("data")
const component = getContext("component")
export let title
export let datasource
@ -33,7 +33,7 @@
// Fetch, filter and sort data
const schema = (await API.fetchTableDefinition(datasource.tableId)).schema
const result = await API.fetchDatasource(datasource, $dataContext)
const result = await API.fetchDatasource(datasource, $component.dataContext)
const reducer = row => (valid, column) => valid && row[column] != null
const hasAllColumns = row => allCols.reduce(reducer(row), true)
const data = result

View File

@ -5,7 +5,7 @@
import { isEmpty } from "lodash/fp"
const { API } = getContext("sdk")
const dataContext = getContext("data")
const component = getContext("component")
// Common props
export let title
@ -41,7 +41,7 @@
// Fetch, filter and sort data
const schema = (await API.fetchTableDefinition(datasource.tableId)).schema
const result = await API.fetchDatasource(datasource, $dataContext)
const result = await API.fetchDatasource(datasource, $component.dataContext)
const reducer = row => (valid, column) => valid && row[column] != null
const hasAllColumns = row => allCols.reduce(reducer(row), true)
const data = result

View File

@ -5,7 +5,7 @@
import { isEmpty } from "lodash/fp"
const { API } = getContext("sdk")
const dataContext = getContext("data")
const component = getContext("component")
export let title
export let datasource
@ -31,7 +31,7 @@
// Fetch, filter and sort data
const schema = (await API.fetchTableDefinition(datasource.tableId)).schema
const result = await API.fetchDatasource(datasource, $dataContext)
const result = await API.fetchDatasource(datasource, $component.dataContext)
const data = result
.filter(row => row[labelColumn] != null && row[valueColumn] != null)
.slice(0, 20)

View File

@ -15,7 +15,6 @@
// These maps need to be set up to handle whatever types that are used in the tables.
const setters = new Map([["number", number]])
const SDK = getContext("sdk")
const dataContext = getContext("data")
const component = getContext("component")
const { API, styleable } = SDK
@ -59,7 +58,7 @@
onMount(async () => {
if (!isEmpty(datasource)) {
data = await API.fetchDatasource(datasource, $dataContext)
data = await API.fetchDatasource(datasource, $component.dataContext)
let schema
// Get schema for datasource