2020-10-06 17:46:23 +02:00
|
|
|
<script>
|
|
|
|
import { onMount } from "svelte"
|
|
|
|
import { Select, Label, Multiselect } from "@budibase/bbui"
|
|
|
|
import api from "./api"
|
|
|
|
import { capitalise } from "./helpers"
|
|
|
|
|
|
|
|
export let schema = {}
|
2020-10-09 20:10:28 +02:00
|
|
|
export let linkedRows = []
|
2020-10-06 17:46:23 +02:00
|
|
|
export let showLabel = true
|
|
|
|
export let secondary
|
|
|
|
|
2020-10-09 19:49:23 +02:00
|
|
|
let linkedTable
|
2020-10-06 17:46:23 +02:00
|
|
|
|
|
|
|
$: label = capitalise(schema.name)
|
2020-10-09 19:49:23 +02:00
|
|
|
$: linkedTableId = schema.tableId
|
2020-10-09 20:10:28 +02:00
|
|
|
$: rowsPromise = fetchRows(linkedTableId)
|
2020-10-09 19:49:23 +02:00
|
|
|
$: fetchTable(linkedTableId)
|
2020-10-06 17:46:23 +02:00
|
|
|
|
2020-10-09 19:49:23 +02:00
|
|
|
async function fetchTable() {
|
|
|
|
if (linkedTableId == null) {
|
2020-10-06 17:46:23 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-09 19:49:23 +02:00
|
|
|
const FETCH_TABLE_URL = `/api/tables/${linkedTableId}`
|
|
|
|
const response = await api.get(FETCH_TABLE_URL)
|
|
|
|
linkedTable = await response.json()
|
2020-10-06 17:46:23 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
async function fetchRows(linkedTableId) {
|
2020-10-09 19:49:23 +02:00
|
|
|
if (linkedTableId == null) {
|
2020-10-06 17:46:23 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-09 20:10:28 +02:00
|
|
|
const FETCH_ROWS_URL = `/api/${linkedTableId}/rows`
|
|
|
|
const response = await api.get(FETCH_ROWS_URL)
|
2020-10-06 17:46:23 +02:00
|
|
|
return await response.json()
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
function getPrettyName(row) {
|
|
|
|
return row[(linkedTable && linkedTable.primaryDisplay) || "_id"]
|
2020-10-06 17:46:23 +02:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2020-10-09 19:49:23 +02:00
|
|
|
{#if linkedTable != null}
|
|
|
|
{#if linkedTable.primaryDisplay == null}
|
2020-10-06 17:46:23 +02:00
|
|
|
{#if showLabel}
|
|
|
|
<Label extraSmall grey>{label}</Label>
|
|
|
|
{/if}
|
|
|
|
<Label small black>
|
|
|
|
Please choose a primary display column for the
|
2020-10-09 19:49:23 +02:00
|
|
|
<b>{linkedTable.name}</b>
|
2020-10-06 17:46:23 +02:00
|
|
|
table.
|
|
|
|
</Label>
|
|
|
|
{:else}
|
2020-10-09 20:10:28 +02:00
|
|
|
{#await rowsPromise then rows}
|
2020-10-06 17:46:23 +02:00
|
|
|
<Multiselect
|
|
|
|
{secondary}
|
2020-10-09 20:10:28 +02:00
|
|
|
bind:value={linkedRows}
|
2020-10-06 17:46:23 +02:00
|
|
|
label={showLabel ? label : null}
|
|
|
|
placeholder="Choose some options">
|
2020-10-09 20:10:28 +02:00
|
|
|
{#each rows as row}
|
|
|
|
<option value={row._id}>{getPrettyName(row)}</option>
|
2020-10-06 17:46:23 +02:00
|
|
|
{/each}
|
|
|
|
</Multiselect>
|
|
|
|
{/await}
|
|
|
|
{/if}
|
|
|
|
{/if}
|