2020-07-01 14:19:14 +02:00
|
|
|
<script>
|
|
|
|
import { onMount } from "svelte"
|
|
|
|
|
|
|
|
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 19:49:23 +02:00
|
|
|
async function fetchTable(id) {
|
|
|
|
const FETCH_TABLE_URL = `/api/tables/${id}`
|
|
|
|
const response = await _bb.api.get(FETCH_TABLE_URL)
|
2020-10-06 19:01:23 +02:00
|
|
|
return await response.json()
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
async function fetchFirstRow() {
|
|
|
|
const FETCH_ROWS_URL = `/api/views/all_${table}`
|
|
|
|
const response = await _bb.api.get(FETCH_ROWS_URL)
|
2020-07-01 14:19:14 +02:00
|
|
|
if (response.status === 200) {
|
2020-10-09 20:10:28 +02:00
|
|
|
const allRows = await response.json()
|
|
|
|
if (allRows.length > 0) return allRows[0]
|
2020-10-12 14:34:32 +02:00
|
|
|
return { tableId: table }
|
2020-07-01 14:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchData() {
|
|
|
|
const pathParts = window.location.pathname.split("/")
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
let row
|
2020-07-01 14:19:14 +02:00
|
|
|
// if srcdoc, then we assume this is the builder preview
|
2020-07-07 22:29:20 +02:00
|
|
|
if (pathParts.length === 0 || pathParts[0] === "srcdoc") {
|
2020-10-12 14:34:32 +02:00
|
|
|
if (table) row = await fetchFirstRow()
|
2020-10-07 23:30:51 +02:00
|
|
|
} else if (_bb.routeParams().id) {
|
2020-10-12 14:34:32 +02:00
|
|
|
const GET_ROW_URL = `/api/${table}/rows/${_bb.routeParams().id}`
|
2020-10-09 20:10:28 +02:00
|
|
|
const response = await _bb.api.get(GET_ROW_URL)
|
2020-07-01 14:19:14 +02:00
|
|
|
if (response.status === 200) {
|
2020-10-09 20:10:28 +02:00
|
|
|
row = await response.json()
|
2020-10-07 23:30:51 +02:00
|
|
|
} else {
|
2020-10-12 14:34:32 +02:00
|
|
|
throw new Error("Failed to fetch row.", response)
|
2020-07-01 14:19:14 +02:00
|
|
|
}
|
2020-10-07 23:30:51 +02:00
|
|
|
} else {
|
2020-10-12 14:34:32 +02:00
|
|
|
throw new Exception("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) {
|
|
|
|
// Fetch table schema so we can check for linked rows
|
2020-10-12 14:34:32 +02:00
|
|
|
const tableObj = await fetchTable(row.tableId)
|
|
|
|
for (let key of Object.keys(tableObj.schema)) {
|
|
|
|
if (tableObj.schema[key].type === "link") {
|
2020-10-09 20:10:28 +02:00
|
|
|
row[key] = Array.isArray(row[key]) ? row[key].length : 0
|
2020-10-06 19:01:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:34:32 +02:00
|
|
|
row._table = tableObj
|
2020-10-08 23:06:44 +02:00
|
|
|
|
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} />
|