Add work on data context in components

This commit is contained in:
Andrew Kingston 2020-11-18 11:24:01 +00:00
parent aef49616a7
commit 2f0e4694e0
14 changed files with 73 additions and 203 deletions

View File

@ -15,102 +15,7 @@ import json from "@rollup/plugin-json"
import path from "path" import path from "path"
const production = !process.env.ROLLUP_WATCH const production = !process.env.ROLLUP_WATCH
const lodash_fp_exports = [
"flow",
"pipe",
"union",
"reduce",
"isUndefined",
"cloneDeep",
"split",
"some",
"map",
"filter",
"isEmpty",
"countBy",
"includes",
"last",
"find",
"constant",
"take",
"first",
"intersection",
"mapValues",
"isNull",
"has",
"isInteger",
"isNumber",
"isString",
"isBoolean",
"isDate",
"isArray",
"isObject",
"clone",
"values",
"keyBy",
"isNaN",
"keys",
"orderBy",
"concat",
"reverse",
"difference",
"merge",
"flatten",
"each",
"pull",
"join",
"defaultCase",
"uniqBy",
"every",
"uniqWith",
"isFunction",
"groupBy",
"differenceBy",
"intersectionBy",
"isEqual",
"max",
"sortBy",
"assign",
"uniq",
"trimChars",
"trimCharsStart",
"isObjectLike",
"flattenDeep",
"indexOf",
"isPlainObject",
"toNumber",
"takeRight",
"toPairs",
"remove",
"findIndex",
"compose",
"get",
"tap",
]
const lodash_exports = [
"flow",
"join",
"replace",
"trim",
"dropRight",
"takeRight",
"head",
"reduce",
"tail",
"startsWith",
"findIndex",
"merge",
"assign",
"each",
"find",
"orderBy",
"union",
]
const outputpath = "../server/builder" const outputpath = "../server/builder"
const coreExternal = [ const coreExternal = [
"lodash", "lodash",
"lodash/fp", "lodash/fp",
@ -224,13 +129,7 @@ export default {
) )
}, },
}), }),
commonjs({ commonjs(),
namedExports: {
"lodash/fp": lodash_fp_exports,
lodash: lodash_exports,
shortid: ["generate"],
},
}),
url({ url({
limit: 0, limit: 0,
include: ["**/*.woff2", "**/*.png"], include: ["**/*.woff2", "**/*.png"],

View File

@ -16,7 +16,6 @@
}, },
"devDependencies": { "devDependencies": {
"@budibase/standard-components": "^0.3.8", "@budibase/standard-components": "^0.3.8",
"@rollup/plugin-alias": "^3.1.1",
"@rollup/plugin-commonjs": "^16.0.0", "@rollup/plugin-commonjs": "^16.0.0",
"@rollup/plugin-node-resolve": "^10.0.0", "@rollup/plugin-node-resolve": "^10.0.0",
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",

View File

@ -1,34 +1,26 @@
import alias from "@rollup/plugin-alias"
import commonjs from "@rollup/plugin-commonjs" import commonjs from "@rollup/plugin-commonjs"
import resolve from "@rollup/plugin-node-resolve" import resolve from "@rollup/plugin-node-resolve"
import builtins from "rollup-plugin-node-builtins" import builtins from "rollup-plugin-node-builtins"
import svelte from "rollup-plugin-svelte" import svelte from "rollup-plugin-svelte"
import path from "path"
const production = !process.env.ROLLUP_WATCH const production = !process.env.ROLLUP_WATCH
const projectRootDir = path.resolve(__dirname)
export default { export default {
input: "src/index.js", input: "src/index.js",
output: [ output: [
{ {
file: "dist/budibase-client.js", sourcemap: true,
format: "iife",
name: "app",
file: `./dist/budibase-client.js`,
},
{
file: "dist/budibase-client.esm.mjs",
format: "esm", format: "esm",
sourcemap: "inline", sourcemap: "inline",
}, },
], ],
plugins: [ plugins: [
alias({
entries: [
{
find: "@budibase/component-sdk",
replacement: path.resolve(
projectRootDir,
"../component-sdk/dist/budibase-component-sdk"
),
},
],
}),
svelte({ svelte({
dev: !production, dev: !production,
}), }),

View File

@ -18,10 +18,10 @@
"rollup-plugin-node-builtins": "^2.1.2", "rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-globals": "^1.4.0", "rollup-plugin-node-globals": "^1.4.0",
"rollup-plugin-node-resolve": "^5.2.0", "rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-svelte": "^6.1.1" "rollup-plugin-svelte": "^6.1.1",
"svelte": "^3.29.0"
}, },
"dependencies": { "dependencies": {
"svelte": "^3.29.0",
"svelte-spa-router": "^3.0.5" "svelte-spa-router": "^3.0.5"
} }
} }

View File

@ -0,0 +1,17 @@
<script>
import { setContext, onMount } from "svelte"
import { ContextTypes, createDataProviderStore } from "../context"
export let row
setContext("foo", "bar2")
const dataProviderStore = createDataProviderStore()
setContext(ContextTypes.DataProvider, dataProviderStore)
onMount(async () => {
await dataProviderStore.actions.setRow(row)
})
</script>
<slot />

View File

@ -0,0 +1 @@
export { default as DataProvider } from "./DataProvider.svelte"

View File

@ -1 +1,5 @@
import { getContext } from "svelte"
export const DataProvider = "bb-data-provider" export const DataProvider = "bb-data-provider"
export const asd = () => getContext(DataProvider)

View File

@ -1,24 +1,24 @@
import { writable } from "svelte/store" import { writable } from "svelte/store"
import { fetchTableDefinition } from "../api"
export const createDataProviderContext = () => { export const createDataProviderStore = () => {
const store = writable({ const store = writable({
rows: [], row: {},
table: null, table: null,
}) })
const setRows = rows => { const setRow = async row => {
store.update(state => { let table
state.rows = rows if (row && row.tableId) {
return state table = await fetchTableDefinition(row.tableId)
}) }
}
const setTable = table => {
store.update(state => { store.update(state => {
state.row = row
state.table = table state.table = table
return state return state
}) })
} }
return { return {
subscribe: store.subscribe, subscribe: store.subscribe,
actions: { setRows, setTable }, actions: { setRow },
} }
} }

View File

@ -2,4 +2,5 @@ export * from "./api"
export * from "./store" export * from "./store"
export * from "./context" export * from "./context"
export * from "./utils" export * from "./utils"
export * from "./components"
export { link as linkable } from "svelte-spa-router" export { link as linkable } from "svelte-spa-router"

View File

@ -1,14 +1,11 @@
import alias from "@rollup/plugin-alias"
import commonjs from "@rollup/plugin-commonjs" import commonjs from "@rollup/plugin-commonjs"
import resolve from "@rollup/plugin-node-resolve" import resolve from "@rollup/plugin-node-resolve"
import replace from "@rollup/plugin-replace" import replace from "@rollup/plugin-replace"
import svelte from "rollup-plugin-svelte" import svelte from "rollup-plugin-svelte"
import postcss from "rollup-plugin-postcss" import postcss from "rollup-plugin-postcss"
import { terser } from "rollup-plugin-terser" import { terser } from "rollup-plugin-terser"
import path from "path"
const production = !process.env.ROLLUP_WATCH const production = !process.env.ROLLUP_WATCH
const projectRootDir = path.resolve(__dirname)
export default { export default {
input: "src/index.js", input: "src/index.js",
@ -21,17 +18,6 @@ export default {
}, },
], ],
plugins: [ plugins: [
alias({
entries: [
{
find: "@budibase/component-sdk",
replacement: path.resolve(
projectRootDir,
"../component-sdk/dist/budibase-component-sdk"
),
},
],
}),
production && terser(), production && terser(),
postcss(), postcss(),
svelte({ svelte({

View File

@ -15,8 +15,12 @@
let rowId let rowId
let errors = {} let errors = {}
console.log("RENDER FORM")
console.log(getContext("foo"))
const dataProviderStore = getContext(ContextTypes.DataProvider) const dataProviderStore = getContext(ContextTypes.DataProvider)
$: row = $dataProviderStore.rows[0]
$: row = $dataProviderStore.row
$: schema = $dataProviderStore.table && $dataProviderStore.table.schema $: schema = $dataProviderStore.table && $dataProviderStore.table.schema
$: fields = schema ? Object.keys(schema) : [] $: fields = schema ? Object.keys(schema) : []
</script> </script>

View File

@ -1,30 +1,28 @@
<script> <script>
import { onMount, setContext } from "svelte" import { onMount } from "svelte"
import { import {
fetchDatasource, fetchDatasource,
createDataProviderContext, styleable,
fetchTableDefinition, DataProvider,
ContextTypes,
} from "@budibase/component-sdk" } from "@budibase/component-sdk"
import { isEmpty } from "lodash/fp" import { isEmpty } from "lodash/fp"
export let datasource = [] export let datasource = []
export let styles
let target let rows = []
const dataProviderContext = createDataProviderContext()
setContext(ContextTypes.DataProvider, dataProviderContext)
onMount(async () => { onMount(async () => {
if (!isEmpty(datasource)) { if (!isEmpty(datasource)) {
const rows = await fetchDatasource(datasource) rows = await fetchDatasource(datasource)
dataProviderContext.actions.setRows(rows)
if (datasource.tableId) {
const tableDefinition = await fetchTableDefinition(datasource.tableId)
dataProviderContext.actions.setTable(tableDefinition)
}
} }
}) })
</script> </script>
<section bind:this={target} /> <div use:styleable={styles}>
{#each rows as row}
<DataProvider {row}>
<slot />
</DataProvider>
{/each}
</div>

View File

@ -1,32 +1,9 @@
<script> <script>
import { onMount } from "svelte" import { DataProvider } from "@budibase/component-sdk"
import { fetchTableDefinition } from "@budibase/component-sdk"
export let _bb
export let table export let table
let row = {}
$: {
row.tableId = table
}
let target
onMount(async () => {
if (table && typeof table === "string") {
const tableObj = await fetchTableDefinition(table)
row.tableId = table
row._table = tableObj
_bb.attachChildren(target, {
context: row,
})
} else {
_bb.attachChildren(target, {
context: {},
})
}
})
</script> </script>
<section bind:this={target} /> <DataProvider row={{ tableId: table }}>
<slot />
</DataProvider>

View File

@ -6,17 +6,15 @@
fetchRow, fetchRow,
screenStore, screenStore,
routeStore, routeStore,
createDataProviderContext, DataProvider,
ContextTypes,
} from "@budibase/component-sdk" } from "@budibase/component-sdk"
export let table export let table
let headers = [] let headers = []
let row
// Expose data provider context for this row setContext("foo", "bar")
const dataProviderContext = createDataProviderContext()
setContext(ContextTypes.DataProvider, dataProviderContext)
async function fetchFirstRow() { async function fetchFirstRow() {
const rows = await fetchTableData(table) const rows = await fetchTableData(table)
@ -30,9 +28,6 @@
const pathParts = window.location.pathname.split("/") const pathParts = window.location.pathname.split("/")
const routeParamId = $routeStore.routeParams.id const routeParamId = $routeStore.routeParams.id
console.log(routeParamId)
let row
let tableDefinition
// if srcdoc, then we assume this is the builder preview // if srcdoc, then we assume this is the builder preview
if ((pathParts.length === 0 || pathParts[0] === "srcdoc") && table) { if ((pathParts.length === 0 || pathParts[0] === "srcdoc") && table) {
@ -42,16 +37,13 @@
} else { } else {
throw new Error("Row ID was not supplied to RowDetail") throw new Error("Row ID was not supplied to RowDetail")
} }
if (row) {
tableDefinition = await fetchTableDefinition(row.tableId)
}
dataProviderContext.actions.setRows([row])
dataProviderContext.actions.setTable(tableDefinition)
} }
onMount(fetchData) onMount(fetchData)
</script> </script>
<slot /> {#if row}
<DataProvider {row}>
<slot />
</DataProvider>
{/if}