2020-07-01 14:19:14 +02:00
|
|
|
<script>
|
2020-11-18 22:06:12 +01:00
|
|
|
import { onMount, getContext } from "svelte"
|
|
|
|
|
2020-10-09 19:49:23 +02:00
|
|
|
export let table
|
2020-07-01 14:19:14 +02:00
|
|
|
|
2021-02-09 13:41:21 +01:00
|
|
|
const {
|
|
|
|
API,
|
|
|
|
screenStore,
|
|
|
|
routeStore,
|
|
|
|
Provider,
|
|
|
|
styleable,
|
|
|
|
ActionTypes,
|
|
|
|
} = getContext("sdk")
|
|
|
|
const component = getContext("component")
|
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
|
|
|
|
2021-02-09 13:41:21 +01:00
|
|
|
const fetchFirstRow = async tableId => {
|
|
|
|
const rows = await API.fetchTableData(tableId)
|
|
|
|
return Array.isArray(rows) && rows.length ? rows[0] : { tableId }
|
2020-07-01 14:19:14 +02:00
|
|
|
}
|
|
|
|
|
2021-02-09 13:41:21 +01:00
|
|
|
const fetchData = async (rowId, tableId) => {
|
|
|
|
if (!tableId) {
|
2020-10-09 13:24:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-12 13:24:45 +01:00
|
|
|
const pathParts = window.location.pathname.split("/")
|
|
|
|
|
2020-07-01 14:19:14 +02:00
|
|
|
// if srcdoc, then we assume this is the builder preview
|
2021-02-09 13:41:21 +01:00
|
|
|
if ((pathParts.length === 0 || pathParts[0] === "srcdoc") && tableId) {
|
|
|
|
row = await fetchFirstRow(tableId)
|
|
|
|
} else if (rowId) {
|
|
|
|
row = await API.fetchRow({ tableId, rowId })
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 13:41:21 +01:00
|
|
|
$: actions = [
|
|
|
|
{
|
|
|
|
type: ActionTypes.RefreshDatasource,
|
|
|
|
callback: () => fetchData($routeStore.routeParams.id, table),
|
|
|
|
metadata: { datasource: { type: "table", tableId: table } },
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
onMount(() => fetchData($routeStore.routeParams.id, table))
|
2020-07-01 14:19:14 +02:00
|
|
|
</script>
|
|
|
|
|
2020-11-18 12:24:01 +01:00
|
|
|
{#if row}
|
2021-02-09 13:41:21 +01:00
|
|
|
<Provider data={row} {actions}>
|
|
|
|
<div use:styleable={$component.styles}>
|
2020-12-02 14:49:24 +01:00
|
|
|
<slot />
|
2021-02-09 13:41:21 +01:00
|
|
|
</div>
|
|
|
|
</Provider>
|
2020-11-18 12:24:01 +01:00
|
|
|
{/if}
|