1
0
Fork 0
mirror of synced 2024-07-19 13:15:49 +12:00
budibase/packages/standard-components/src/RowDetail.svelte
2020-11-23 11:29:24 +00:00

42 lines
989 B
Svelte

<script>
import { onMount, getContext } from "svelte"
const { API, screenStore, routeStore, DataProvider } = getContext("sdk")
export let table
let headers = []
let row
async function fetchFirstRow() {
const rows = await API.fetchTableData(table)
return Array.isArray(rows) && rows.length ? rows[0] : { tableId: table }
}
async function fetchData() {
if (!table) {
return
}
const pathParts = window.location.pathname.split("/")
const routeParamId = $routeStore.routeParams.id
// 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 API.fetchRow({ tableId: table, rowId: routeParamId })
} else {
throw new Error("Row ID was not supplied to RowDetail")
}
}
onMount(fetchData)
</script>
{#if row}
<DataProvider {row}>
<slot />
</DataProvider>
{/if}