2020-07-01 14:19:14 +02:00
|
|
|
<script>
|
|
|
|
import { onMount } from "svelte"
|
|
|
|
|
|
|
|
export let _bb
|
|
|
|
export let model
|
|
|
|
|
|
|
|
let headers = []
|
|
|
|
let store = _bb.store
|
|
|
|
let target
|
|
|
|
|
2020-10-08 17:15:12 +02:00
|
|
|
async function fetchModel(id) {
|
|
|
|
const FETCH_MODEL_URL = `/api/models/${id}`
|
|
|
|
const response = await _bb.api.get(FETCH_MODEL_URL)
|
|
|
|
return await response.json()
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:19:14 +02:00
|
|
|
async function fetchFirstRecord() {
|
|
|
|
const FETCH_RECORDS_URL = `/api/views/all_${model}`
|
|
|
|
const response = await _bb.api.get(FETCH_RECORDS_URL)
|
|
|
|
if (response.status === 200) {
|
|
|
|
const allRecords = await response.json()
|
|
|
|
if (allRecords.length > 0) return allRecords[0]
|
2020-10-09 12:58:46 +02:00
|
|
|
return { modelId: model }
|
2020-07-01 14:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchData() {
|
|
|
|
const pathParts = window.location.pathname.split("/")
|
|
|
|
|
|
|
|
let record
|
|
|
|
// 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-07-01 14:19:14 +02:00
|
|
|
record = await fetchFirstRecord()
|
2020-10-07 23:30:51 +02:00
|
|
|
} else if (_bb.routeParams().id) {
|
|
|
|
const GET_RECORD_URL = `/api/${model}/records/${_bb.routeParams().id}`
|
2020-07-01 14:19:14 +02:00
|
|
|
const response = await _bb.api.get(GET_RECORD_URL)
|
|
|
|
if (response.status === 200) {
|
|
|
|
record = await response.json()
|
2020-10-07 23:30:51 +02:00
|
|
|
} else {
|
|
|
|
throw new Error("Failed to fetch record.", response)
|
2020-07-01 14:19:14 +02:00
|
|
|
}
|
2020-10-07 23:30:51 +02:00
|
|
|
} else {
|
2020-10-09 18:12:55 +02:00
|
|
|
throw new Exception("Record ID was not supplied to RowDetail")
|
2020-07-01 14:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (record) {
|
2020-10-08 17:15:12 +02:00
|
|
|
// Fetch model schema so we can check for linked records
|
|
|
|
const model = await fetchModel(record.modelId)
|
|
|
|
for (let key of Object.keys(model.schema)) {
|
|
|
|
if (model.schema[key].type === "link") {
|
|
|
|
record[key] = Array.isArray(record[key]) ? record[key].length : 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 23:06:44 +02:00
|
|
|
record._model = model
|
|
|
|
|
2020-07-01 14:19:14 +02:00
|
|
|
_bb.attachChildren(target, {
|
|
|
|
context: record,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onMount(async () => {
|
|
|
|
await fetchData()
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
2020-07-07 22:29:20 +02:00
|
|
|
<section bind:this={target} />
|