forked from tsai/budibase
3 changed files with 88 additions and 2 deletions
@ -0,0 +1,58 @@ |
|||||
|
<script> |
||||
|
import { onMount } from "svelte" |
||||
|
import api from "../../api" |
||||
|
import getTable from "./tableCache" |
||||
|
|
||||
|
export let columnName |
||||
|
export let row |
||||
|
|
||||
|
let linkedRows = [] |
||||
|
let displayColumn |
||||
|
|
||||
|
onMount(async () => { |
||||
|
linkedRows = await fetchLinkedRowsData(row, columnName) |
||||
|
if (linkedRows && linkedRows.length) { |
||||
|
const table = await getTable(linkedRows[0].tableId) |
||||
|
if (table && table.primaryDisplay) { |
||||
|
displayColumn = table.primaryDisplay |
||||
|
} |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
async function fetchLinkedRowsData(row, columnName) { |
||||
|
if (!row || !row._id) { |
||||
|
return [] |
||||
|
} |
||||
|
const QUERY_URL = `/api/${row.tableId}/${row._id}/enrich` |
||||
|
const response = await api.get(QUERY_URL) |
||||
|
const enrichedRow = await response.json() |
||||
|
return enrichedRow[columnName] |
||||
|
} |
||||
|
</script> |
||||
|
|
||||
|
<div class="container"> |
||||
|
{#if linkedRows && linkedRows.length && displayColumn} |
||||
|
{#each linkedRows as linkedRow} |
||||
|
<div class="linked-row">{linkedRow[displayColumn]}</div> |
||||
|
{/each} |
||||
|
{/if} |
||||
|
</div> |
||||
|
|
||||
|
<style> |
||||
|
.container { |
||||
|
display: flex; |
||||
|
flex-direction: row; |
||||
|
justify-content: flex-start; |
||||
|
align-items: center; |
||||
|
gap: var(--spacing-xs); |
||||
|
width: 100%; |
||||
|
} |
||||
|
.linked-row { |
||||
|
color: white; |
||||
|
background-color: var(--ink); |
||||
|
border-radius: var(--border-radius-l); |
||||
|
padding: var(--spacing-xs) var(--spacing-s) calc(var(--spacing-xs) + 1px) |
||||
|
var(--spacing-s); |
||||
|
line-height: 1; |
||||
|
} |
||||
|
</style> |
||||
@ -0,0 +1,21 @@ |
|||||
|
import api from "../../api" |
||||
|
|
||||
|
let cache = {} |
||||
|
|
||||
|
async function fetchTable(id) { |
||||
|
const FETCH_TABLE_URL = `/api/tables/${id}` |
||||
|
const response = await api.get(FETCH_TABLE_URL) |
||||
|
return await response.json() |
||||
|
} |
||||
|
|
||||
|
export default async function getTable(tableId) { |
||||
|
if (!tableId) { |
||||
|
return null |
||||
|
} |
||||
|
if (!cache[tableId]) { |
||||
|
console.log("cache miss for " + tableId) |
||||
|
cache[tableId] = fetchTable(tableId) |
||||
|
cache[tableId] = await cache[tableId] |
||||
|
} |
||||
|
return await cache[tableId] |
||||
|
} |
||||
Loading…
Reference in new issue