1
0
Fork 0
mirror of synced 2024-10-01 01:28:51 +13:00
budibase/packages/standard-components/src/RowDetail.svelte

50 lines
1.1 KiB
Svelte
Raw Normal View History

2020-07-02 00:19:14 +12:00
<script>
import { onMount, setContext } from "svelte"
import {
fetchTableDefinition,
fetchTableData,
fetchRow,
screenStore,
routeStore,
2020-11-19 00:24:01 +13:00
DataProvider,
} from "@budibase/component-sdk"
2020-07-02 00:19:14 +12:00
export let table
2020-07-02 00:19:14 +12:00
let headers = []
2020-11-19 00:24:01 +13:00
let row
2020-11-19 00:24:01 +13:00
setContext("foo", "bar")
2020-07-02 00:19:14 +12:00
async function fetchFirstRow() {
const rows = await fetchTableData(table)
return Array.isArray(rows) && rows.length ? rows[0] : { tableId: table }
2020-07-02 00:19:14 +12:00
}
async function fetchData() {
if (!table) {
2020-10-10 00:24:18 +13:00
return
}
const pathParts = window.location.pathname.split("/")
const routeParamId = $routeStore.routeParams.id
2020-07-02 00:19:14 +12:00
// if srcdoc, then we assume this is the builder preview
if ((pathParts.length === 0 || pathParts[0] === "srcdoc") && table) {
row = await fetchFirstRow()
} else if (routeParamId) {
row = await fetchRow({ tableId: table, rowId: routeParamId })
} else {
throw new Error("Row ID was not supplied to RowDetail")
2020-07-02 00:19:14 +12:00
}
}
onMount(fetchData)
2020-07-02 00:19:14 +12:00
</script>
2020-11-19 00:24:01 +13:00
{#if row}
<DataProvider {row}>
<slot />
</DataProvider>
{/if}