Add button action to refresh a datasource

This commit is contained in:
Andrew Kingston 2021-02-05 12:54:36 +00:00
parent af74e6a592
commit 6bdde675a2
7 changed files with 93 additions and 22 deletions

View File

@ -0,0 +1,37 @@
<script>
import { Select, Label } from "@budibase/bbui"
import { currentAsset, store } from "builderStore"
import { getDataProviderComponents } from "builderStore/dataBinding"
export let parameters
$: dataProviders = getDataProviderComponents(
$currentAsset.props,
$store.selectedComponentId
)
</script>
<div class="root">
<Label size="m" color="dark">Form</Label>
<Select secondary bind:value={parameters.componentId}>
<option value="" />
{#if dataProviders}
{#each dataProviders as component}
<option value={component._id}>{component._instanceName}</option>
{/each}
{/if}
</Select>
</div>
<style>
.root {
display: flex;
flex-direction: row;
align-items: baseline;
}
.root :global(> div) {
flex: 1;
margin-left: var(--spacing-l);
}
</style>

View File

@ -4,6 +4,7 @@ import DeleteRow from "./DeleteRow.svelte"
import ExecuteQuery from "./ExecuteQuery.svelte" import ExecuteQuery from "./ExecuteQuery.svelte"
import TriggerAutomation from "./TriggerAutomation.svelte" import TriggerAutomation from "./TriggerAutomation.svelte"
import ValidateForm from "./ValidateForm.svelte" import ValidateForm from "./ValidateForm.svelte"
import RefreshDatasource from "./RefreshDatasource.svelte"
// defines what actions are available, when adding a new one // defines what actions are available, when adding a new one
// the component is the setup panel for the action // the component is the setup panel for the action
@ -35,4 +36,8 @@ export default [
name: "Validate Form", name: "Validate Form",
component: ValidateForm, component: ValidateForm,
}, },
{
name: "Refresh Datasource",
component: RefreshDatasource,
},
] ]

View File

@ -14,7 +14,7 @@
$: providerKey = key || $component.id $: providerKey = key || $component.id
// Add data context // Add data context
$: newContext.actions.provideData(providerKey, data) $: data !== undefined && newContext.actions.provideData(providerKey, data)
// Add actions context // Add actions context
$: { $: {

View File

@ -4,4 +4,5 @@ export const TableNames = {
export const ActionTypes = { export const ActionTypes = {
ValidateForm: "ValidateForm", ValidateForm: "ValidateForm",
RefreshDatasource: "RefreshDatasource",
} }

View File

@ -50,14 +50,29 @@ const queryExecutionHandler = async action => {
}) })
} }
const validateFormHandler = async (action, context) => { const executeActionHandler = async (context, componentId, actionType) => {
const { componentId } = action.parameters const fn = context[`${componentId}_${actionType}`]
const fn = context[`${componentId}_${ActionTypes.ValidateForm}`]
if (fn) { if (fn) {
return await fn() return await fn()
} }
} }
const validateFormHandler = async (action, context) => {
return await executeActionHandler(
context,
action.parameters.componentId,
ActionTypes.ValidateForm
)
}
const refreshDatasourceHandler = async (action, context) => {
return await executeActionHandler(
context,
action.parameters.componentId,
ActionTypes.RefreshDatasource
)
}
const handlerMap = { const handlerMap = {
["Save Row"]: saveRowHandler, ["Save Row"]: saveRowHandler,
["Delete Row"]: deleteRowHandler, ["Delete Row"]: deleteRowHandler,
@ -65,6 +80,7 @@ const handlerMap = {
["Execute Query"]: queryExecutionHandler, ["Execute Query"]: queryExecutionHandler,
["Trigger Automation"]: triggerAutomationHandler, ["Trigger Automation"]: triggerAutomationHandler,
["Validate Form"]: validateFormHandler, ["Validate Form"]: validateFormHandler,
["Refresh Datasource"]: refreshDatasourceHandler,
} }
/** /**

View File

@ -106,6 +106,7 @@
"styleable": true, "styleable": true,
"hasChildren": true, "hasChildren": true,
"dataProvider": true, "dataProvider": true,
"actions": ["RefreshDatasource"],
"settings": [ "settings": [
{ {
"type": "datasource", "type": "datasource",

View File

@ -2,7 +2,9 @@
import { getContext } from "svelte" import { getContext } from "svelte"
import { isEmpty } from "lodash/fp" import { isEmpty } from "lodash/fp"
const { API, styleable, Provider, builderStore } = getContext("sdk") const { API, styleable, Provider, builderStore, ActionTypes } = getContext(
"sdk"
)
const component = getContext("component") const component = getContext("component")
export let datasource = [] export let datasource = []
@ -18,25 +20,34 @@
} }
loaded = true loaded = true
} }
$: actions = [
{
type: ActionTypes.RefreshDatasource,
callback: () => fetchData(datasource),
},
]
</script> </script>
{#if rows.length > 0} <Provider {actions}>
<div use:styleable={$component.styles}> {#if rows.length > 0}
{#if $component.children === 0 && $builderStore.inBuilder} <div use:styleable={$component.styles}>
<p>Add some components too</p> {#if $component.children === 0 && $builderStore.inBuilder}
{:else} <p>Add some components too</p>
{#each rows as row} {:else}
<Provider data={row}> {#each rows as row}
<slot /> <Provider data={row}>
</Provider> <slot />
{/each} </Provider>
{/if} {/each}
</div> {/if}
{:else if loaded && $builderStore.inBuilder} </div>
<div use:styleable={$component.styles}> {:else if loaded && $builderStore.inBuilder}
<p>Feed me some data</p> <div use:styleable={$component.styles}>
</div> <p>Feed me some data</p>
{/if} </div>
{/if}
</Provider>
<style> <style>
p { p {