2020-07-01 14:19:14 +02:00
|
|
|
<script>
|
2020-11-18 22:06:12 +01:00
|
|
|
import { onMount, getContext } from "svelte"
|
|
|
|
|
2020-12-02 14:49:24 +01:00
|
|
|
const { API, screenStore, routeStore, DataProvider, styleable } = getContext(
|
|
|
|
"sdk"
|
|
|
|
)
|
|
|
|
const component = getContext("component")
|
2020-07-01 14:19:14 +02:00
|
|
|
|
2020-10-09 19:49:23 +02:00
|
|
|
export let table
|
2020-07-01 14:19:14 +02:00
|
|
|
|
|
|
|
let headers = []
|
2020-11-18 12:24:01 +01:00
|
|
|
let row
|
2020-11-17 13:08:24 +01:00
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
async function fetchFirstRow() {
|
2020-11-18 22:06:12 +01:00
|
|
|
const rows = await API.fetchTableData(table)
|
2020-11-12 13:24:45 +01:00
|
|
|
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("/")
|
2020-11-17 13:08:24 +01:00
|
|
|
const routeParamId = $routeStore.routeParams.id
|
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) {
|
2020-11-18 22:06:12 +01:00
|
|
|
row = await API.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-11-17 13:08:24 +01:00
|
|
|
onMount(fetchData)
|
2020-07-01 14:19:14 +02:00
|
|
|
</script>
|
|
|
|
|
2020-11-18 12:24:01 +01:00
|
|
|
{#if row}
|
2020-12-02 14:49:24 +01:00
|
|
|
<div use:styleable={$component.styles}>
|
|
|
|
<DataProvider {row}>
|
|
|
|
<slot />
|
|
|
|
</DataProvider>
|
|
|
|
</div>
|
2020-11-18 12:24:01 +01:00
|
|
|
{/if}
|