2020-07-01 14:19:14 +02:00
|
|
|
<script>
|
2020-11-17 13:08:24 +01:00
|
|
|
import { onMount, setContext } from "svelte"
|
2020-11-12 13:24:45 +01:00
|
|
|
import {
|
|
|
|
fetchTableDefinition,
|
|
|
|
fetchTableData,
|
|
|
|
fetchRow,
|
2020-11-17 13:08:24 +01:00
|
|
|
screenStore,
|
|
|
|
routeStore,
|
|
|
|
createDataProviderContext,
|
|
|
|
ContextTypes,
|
2020-11-13 16:42:32 +01:00
|
|
|
} from "@budibase/component-sdk"
|
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-17 13:08:24 +01:00
|
|
|
|
|
|
|
// Expose data provider context for this row
|
|
|
|
const dataProviderContext = createDataProviderContext()
|
|
|
|
setContext(ContextTypes.DataProvider, dataProviderContext)
|
2020-07-01 14:19:14 +02:00
|
|
|
|
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("/")
|
2020-11-17 13:08:24 +01:00
|
|
|
const routeParamId = $routeStore.routeParams.id
|
|
|
|
console.log(routeParamId)
|
2020-10-09 20:10:28 +02:00
|
|
|
let row
|
2020-11-17 13:08:24 +01:00
|
|
|
let tableDefinition
|
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-17 13:08:24 +01:00
|
|
|
tableDefinition = await fetchTableDefinition(row.tableId)
|
2020-07-01 14:19:14 +02:00
|
|
|
}
|
2020-11-17 13:08:24 +01:00
|
|
|
|
|
|
|
dataProviderContext.actions.setRows([row])
|
|
|
|
dataProviderContext.actions.setTable(tableDefinition)
|
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-17 13:08:24 +01:00
|
|
|
<slot />
|