1
0
Fork 0
mirror of synced 2024-09-19 18:59:06 +12:00
budibase/packages/standard-components/src/RowDetail.svelte

55 lines
1.2 KiB
Svelte
Raw Normal View History

2020-07-02 00:19:14 +12:00
<script>
import { onMount } from "svelte"
import {
fetchTableDefinition,
fetchTableData,
fetchRow,
} from "@budibase/component-sdk"
2020-07-02 00:19:14 +12:00
export let _bb
export let table
2020-07-02 00:19:14 +12:00
let headers = []
let store = _bb.store
let target
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 = _bb.routeParams().id
let row
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
}
if (row) {
row._table = await fetchTableDefinition(row.tableId)
2020-07-02 00:19:14 +12:00
_bb.attachChildren(target, {
context: row,
2020-07-02 00:19:14 +12:00
})
} else {
_bb.attachChildren(target)
2020-07-02 00:19:14 +12:00
}
}
onMount(async () => {
await fetchData()
})
</script>
2020-07-08 08:29:20 +12:00
<section bind:this={target} />