2020-07-01 14:19:14 +02:00
|
|
|
<script>
|
|
|
|
import { onMount } from "svelte"
|
2020-11-12 13:24:45 +01:00
|
|
|
import {
|
|
|
|
fetchTableDefinition,
|
|
|
|
fetchTableData,
|
|
|
|
fetchRow,
|
2020-11-13 16:42:32 +01:00
|
|
|
} from "@budibase/component-sdk"
|
2020-07-01 14:19:14 +02:00
|
|
|
|
|
|
|
export let _bb
|
2020-10-09 19:49:23 +02:00
|
|
|
export let table
|
2020-07-01 14:19:14 +02:00
|
|
|
|
|
|
|
let headers = []
|
|
|
|
let store = _bb.store
|
|
|
|
let target
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
async function fetchFirstRow() {
|
2020-11-12 13:24:45 +01:00
|
|
|
const rows = await fetchTableData(table)
|
|
|
|
return Array.isArray(rows) && rows.length ? rows[0] : { tableId: table }
|
2020-07-01 14:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchData() {
|
2020-10-13 10:58:08 +02:00
|
|
|
if (!table) {
|
2020-10-09 13:24:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-12 13:24:45 +01:00
|
|
|
const pathParts = window.location.pathname.split("/")
|
|
|
|
const routeParamId = _bb.routeParams().id
|
2020-10-09 20:10:28 +02:00
|
|
|
let row
|
2020-11-12 13:24:45 +01:00
|
|
|
|
2020-07-01 14:19:14 +02:00
|
|
|
// if srcdoc, then we assume this is the builder preview
|
2020-11-12 13:24:45 +01:00
|
|
|
if ((pathParts.length === 0 || pathParts[0] === "srcdoc") && table) {
|
|
|
|
row = await fetchFirstRow()
|
|
|
|
} else if (routeParamId) {
|
|
|
|
row = await fetchRow({ tableId: table, rowId: routeParamId })
|
2020-10-07 23:30:51 +02:00
|
|
|
} else {
|
2020-11-12 13:24:45 +01:00
|
|
|
throw new Error("Row ID was not supplied to RowDetail")
|
2020-07-01 14:19:14 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
if (row) {
|
2020-11-12 13:24:45 +01:00
|
|
|
row._table = await fetchTableDefinition(row.tableId)
|
2020-07-01 14:19:14 +02:00
|
|
|
_bb.attachChildren(target, {
|
2020-10-09 20:10:28 +02:00
|
|
|
context: row,
|
2020-07-01 14:19:14 +02:00
|
|
|
})
|
|
|
|
} else {
|
2020-10-12 11:51:20 +02:00
|
|
|
_bb.attachChildren(target)
|
2020-07-01 14:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onMount(async () => {
|
|
|
|
await fetchData()
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
2020-07-07 22:29:20 +02:00
|
|
|
<section bind:this={target} />
|