2020-11-25 10:50:51 +01:00
|
|
|
import { enrichDataBindings } from "./enrichDataBinding"
|
|
|
|
import { enrichButtonActions } from "./buttonActions"
|
|
|
|
|
2021-01-21 11:41:30 +01:00
|
|
|
/**
|
|
|
|
* Deeply compares 2 props using JSON.stringify.
|
|
|
|
* Does not consider functions, as currently only button actions have a function
|
|
|
|
* prop and it's cheaper to just always re-render buttons than it is to deeply
|
|
|
|
* compare them.
|
|
|
|
*/
|
|
|
|
export const propsAreSame = (a, b) => {
|
|
|
|
if (a === b) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if (typeof a === "function" || typeof b === "function") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return JSON.stringify(a) === JSON.stringify(b)
|
|
|
|
}
|
|
|
|
|
2020-11-25 10:50:51 +01:00
|
|
|
/**
|
|
|
|
* Enriches component props.
|
|
|
|
* Data bindings are enriched, and button actions are enriched.
|
|
|
|
*/
|
2021-01-28 15:29:35 +01:00
|
|
|
export const enrichProps = async (props, dataContexts, dataBindings, user) => {
|
2020-11-25 10:50:51 +01:00
|
|
|
// Exclude all private props that start with an underscore
|
|
|
|
let validProps = {}
|
|
|
|
Object.entries(props)
|
|
|
|
.filter(([name]) => !name.startsWith("_"))
|
|
|
|
.forEach(([key, value]) => {
|
|
|
|
validProps[key] = value
|
|
|
|
})
|
|
|
|
|
|
|
|
// Create context of all bindings and data contexts
|
|
|
|
// Duplicate the closest context as "data" which the builder requires
|
|
|
|
const context = {
|
|
|
|
...dataContexts,
|
|
|
|
...dataBindings,
|
2021-01-28 15:29:35 +01:00
|
|
|
user,
|
2020-11-25 10:50:51 +01:00
|
|
|
data: dataContexts[dataContexts.closestComponentId],
|
|
|
|
data_draft: dataContexts[`${dataContexts.closestComponentId}_draft`],
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enrich all data bindings in top level props
|
2021-01-20 14:32:15 +01:00
|
|
|
let enrichedProps = await enrichDataBindings(validProps, context)
|
2020-11-25 10:50:51 +01:00
|
|
|
|
|
|
|
// Enrich button actions if they exist
|
|
|
|
if (props._component.endsWith("/button") && enrichedProps.onClick) {
|
|
|
|
enrichedProps.onClick = enrichButtonActions(enrichedProps.onClick, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
return enrichedProps
|
|
|
|
}
|