Change how client provides the current user so that the whole app is actually wrapped in a provider which provides the current user

This commit is contained in:
Andrew Kingston 2021-02-05 11:44:33 +00:00
parent 0dd243d8b6
commit 5846e2ef7c
5 changed files with 28 additions and 26 deletions

View File

@ -3,6 +3,7 @@
import { setContext, onMount } from "svelte" import { setContext, onMount } from "svelte"
import Component from "./Component.svelte" import Component from "./Component.svelte"
import NotificationDisplay from "./NotificationDisplay.svelte" import NotificationDisplay from "./NotificationDisplay.svelte"
import Provider from "./Provider.svelte"
import SDK from "../sdk" import SDK from "../sdk"
import { import {
createContextStore, createContextStore,
@ -27,6 +28,8 @@
</script> </script>
{#if loaded && $screenStore.activeLayout} {#if loaded && $screenStore.activeLayout}
<Provider key="user" data={$authStore}>
<Component definition={$screenStore.activeLayout.props} /> <Component definition={$screenStore.activeLayout.props} />
<NotificationDisplay /> <NotificationDisplay />
</Provider>
{/if} {/if}

View File

@ -4,7 +4,7 @@
import * as ComponentLibrary from "@budibase/standard-components" import * as ComponentLibrary from "@budibase/standard-components"
import Router from "./Router.svelte" import Router from "./Router.svelte"
import { enrichProps, propsAreSame } from "../utils/componentProps" import { enrichProps, propsAreSame } from "../utils/componentProps"
import { authStore, builderStore } from "../store" import { builderStore } from "../store"
import { hashString } from "../utils/hash" import { hashString } from "../utils/hash"
export let definition = {} export let definition = {}
@ -32,7 +32,7 @@
$: constructor = getComponentConstructor(definition._component) $: constructor = getComponentConstructor(definition._component)
$: children = definition._children || [] $: children = definition._children || []
$: id = definition._id $: id = definition._id
$: updateComponentProps(definition, $context, $authStore) $: updateComponentProps(definition, $context)
$: styles = definition._styles $: styles = definition._styles
// Update component context // Update component context
@ -53,13 +53,13 @@
} }
// Enriches any string component props using handlebars // Enriches any string component props using handlebars
const updateComponentProps = async (definition, context, user) => { const updateComponentProps = async (definition, context) => {
// Record the timestamp so we can reference it after enrichment // Record the timestamp so we can reference it after enrichment
latestUpdateTime = Date.now() latestUpdateTime = Date.now()
const enrichmentTime = latestUpdateTime const enrichmentTime = latestUpdateTime
// Enrich props with context // Enrich props with context
const enrichedProps = await enrichProps(definition, context, user) const enrichedProps = await enrichProps(definition, context)
// Abandon this update if a newer update has started // Abandon this update if a newer update has started
if (enrichmentTime !== latestUpdateTime) { if (enrichmentTime !== latestUpdateTime) {

View File

@ -4,24 +4,22 @@
export let data export let data
export let actions export let actions
export let key
// Clone and create new data context for this component tree // Clone and create new data context for this component tree
const context = getContext("context") const context = getContext("context")
const component = getContext("component") const component = getContext("component")
const newContext = createContextStore($context) const newContext = createContextStore($context)
setContext("context", newContext) setContext("context", newContext)
$: providerKey = key || $component.id
// Add data context // Add data context
$: { $: newContext.actions.provideData(providerKey, data)
if (data !== undefined) {
newContext.actions.provideData($component.id, data)
}
}
// Add actions context // Add actions context
$: { $: {
actions?.forEach(({ type, callback }) => { actions?.forEach(({ type, callback }) => {
newContext.actions.provideAction($component.id, type, callback) newContext.actions.provideAction(providerKey, type, callback)
}) })
} }
</script> </script>

View File

@ -4,26 +4,28 @@ export const createContextStore = existingContext => {
const store = writable({ ...existingContext }) const store = writable({ ...existingContext })
// Adds a data context layer to the tree // Adds a data context layer to the tree
const provideData = (componentId, data) => { const provideData = (key, data) => {
if (!key) {
return
}
store.update(state => { store.update(state => {
if (componentId) { state[key] = data
state[componentId] = data
// Keep track of the closest component ID so we can later hydrate a "data" prop. // Keep track of the closest component ID so we can later hydrate a "data" prop.
// This is only required for legacy bindings that used "data" rather than a // This is only required for legacy bindings that used "data" rather than a
// component ID. // component ID.
state.closestComponentId = componentId state.closestComponentId = key
}
return state return state
}) })
} }
// Adds an action context layer to the tree // Adds an action context layer to the tree
const provideAction = (componentId, actionType, callback) => { const provideAction = (key, actionType, callback) => {
store.update(state => { if (!key || !actionType) {
if (actionType && componentId) { return
state[`${componentId}_${actionType}`] = callback
} }
store.update(state => {
state[`${key}_${actionType}`] = callback
return state return state
}) })
} }

View File

@ -21,7 +21,7 @@ export const propsAreSame = (a, b) => {
* Enriches component props. * Enriches component props.
* Data bindings are enriched, and button actions are enriched. * Data bindings are enriched, and button actions are enriched.
*/ */
export const enrichProps = async (props, context, user) => { export const enrichProps = async (props, context) => {
// Exclude all private props that start with an underscore // Exclude all private props that start with an underscore
let validProps = {} let validProps = {}
Object.entries(props) Object.entries(props)
@ -34,7 +34,6 @@ export const enrichProps = async (props, context, user) => {
// Duplicate the closest context as "data" which the builder requires // Duplicate the closest context as "data" which the builder requires
const totalContext = { const totalContext = {
...context, ...context,
user,
// This is only required for legacy bindings that used "data" rather than a // This is only required for legacy bindings that used "data" rather than a
// component ID. // component ID.