1
0
Fork 0
mirror of synced 2024-07-19 13:15:49 +12:00
budibase/packages/standard-components/src/RowDetail.svelte

71 lines
1.8 KiB
Svelte
Raw Normal View History

2020-07-02 00:19:14 +12:00
<script>
import { onMount } from "svelte"
export let _bb
export let table
2020-07-02 00:19:14 +12:00
let headers = []
let store = _bb.store
let target
async function fetchTable(id) {
const FETCH_TABLE_URL = `/api/tables/${id}`
const response = await _bb.api.get(FETCH_TABLE_URL)
return await response.json()
}
async function fetchFirstRow() {
const FETCH_ROWS_URL = `/api/views/all_${table}`
const response = await _bb.api.get(FETCH_ROWS_URL)
2020-07-02 00:19:14 +12:00
if (response.status === 200) {
const allRows = await response.json()
if (allRows.length > 0) return allRows[0]
return { tableId: table }
2020-07-02 00:19:14 +12:00
}
}
async function fetchData() {
const pathParts = window.location.pathname.split("/")
let row
2020-07-02 00:19:14 +12:00
// if srcdoc, then we assume this is the builder preview
2020-07-08 08:29:20 +12:00
if (pathParts.length === 0 || pathParts[0] === "srcdoc") {
if (table) row = await fetchFirstRow()
} else if (_bb.routeParams().id) {
const GET_ROW_URL = `/api/${table}/rows/${_bb.routeParams().id}`
const response = await _bb.api.get(GET_ROW_URL)
2020-07-02 00:19:14 +12:00
if (response.status === 200) {
row = await response.json()
} else {
throw new Error("Failed to fetch row.", response)
2020-07-02 00:19:14 +12:00
}
} else {
throw new Exception("Row ID was not supplied to RowDetail")
2020-07-02 00:19:14 +12:00
}
if (row) {
// Fetch table schema so we can check for linked rows
const tableObj = await fetchTable(row.tableId)
for (let key of Object.keys(tableObj.schema)) {
if (tableObj.schema[key].type === "link") {
row[key] = Array.isArray(row[key]) ? row[key].length : 0
}
}
row._table = tableObj
2020-10-09 10:06:44 +13:00
2020-07-02 00:19:14 +12:00
_bb.attachChildren(target, {
context: row,
2020-07-02 00:19:14 +12:00
})
} else {
_bb.attachChildren(target)
2020-07-02 00:19:14 +12:00
}
}
onMount(async () => {
await fetchData()
})
</script>
2020-07-08 08:29:20 +12:00
<section bind:this={target} />