Update linked record selector to handle errors and remove loading state flashing

This commit is contained in:
Andrew Kingston 2020-10-08 12:21:05 +01:00
parent fbb7bf7413
commit 735de553d2
1 changed files with 19 additions and 14 deletions

View File

@ -8,17 +8,24 @@
export let schema export let schema
export let linkedRecords = [] export let linkedRecords = []
let records = []
$: label = capitalise(schema.name) $: label = capitalise(schema.name)
$: linkedModelId = schema.modelId $: linkedModelId = schema.modelId
$: linkedModel = $backendUiStore.models.find( $: linkedModel = $backendUiStore.models.find(
model => model._id === linkedModelId model => model._id === linkedModelId
) )
$: promise = fetchRecords(linkedModelId) $: fetchRecords(linkedModelId)
async function fetchRecords(linkedModelId) { async function fetchRecords(linkedModelId) {
const FETCH_RECORDS_URL = `/api/${linkedModelId}/records` const FETCH_RECORDS_URL = `/api/${linkedModelId}/records`
try {
const response = await api.get(FETCH_RECORDS_URL) const response = await api.get(FETCH_RECORDS_URL)
return await response.json() records = await response.json()
} catch (error) {
console.log(error)
records = []
}
} }
function getPrettyName(record) { function getPrettyName(record) {
@ -34,7 +41,6 @@
table. table.
</Label> </Label>
{:else} {:else}
{#await promise then records}
<Multiselect <Multiselect
secondary secondary
bind:value={linkedRecords} bind:value={linkedRecords}
@ -44,5 +50,4 @@
<option value={record._id}>{getPrettyName(record)}</option> <option value={record._id}>{getPrettyName(record)}</option>
{/each} {/each}
</Multiselect> </Multiselect>
{/await}
{/if} {/if}