2020-06-18 03:51:10 +12:00
|
|
|
<script>
|
2020-06-19 04:55:46 +12:00
|
|
|
import { onMount } from "svelte"
|
2020-06-18 03:51:10 +12:00
|
|
|
import { backendUiStore } from "builderStore"
|
|
|
|
import api from "builderStore/api"
|
|
|
|
|
|
|
|
export let modelId
|
2020-06-23 08:30:23 +12:00
|
|
|
export let linkName
|
|
|
|
export let linked = []
|
2020-06-18 03:51:10 +12:00
|
|
|
|
|
|
|
let records = []
|
2020-06-24 23:41:26 +12:00
|
|
|
let model = {}
|
2020-06-18 03:51:10 +12:00
|
|
|
|
2020-06-24 00:50:45 +12:00
|
|
|
let linkedRecords = new Set(linked)
|
|
|
|
|
|
|
|
$: linked = [...linkedRecords]
|
2020-06-24 23:41:26 +12:00
|
|
|
$: FIELDS_TO_HIDE = [$backendUiStore.selectedModel._id]
|
|
|
|
$: schema = $backendUiStore.selectedModel.schema
|
2020-06-24 00:50:45 +12:00
|
|
|
|
2020-06-18 03:51:10 +12:00
|
|
|
async function fetchRecords() {
|
2020-06-24 00:50:45 +12:00
|
|
|
const FETCH_RECORDS_URL = `/api/${modelId}/records`
|
2020-06-18 03:51:10 +12:00
|
|
|
const response = await api.get(FETCH_RECORDS_URL)
|
2020-06-24 23:41:26 +12:00
|
|
|
const modelResponse = await api.get(`/api/models/${modelId}`)
|
|
|
|
|
|
|
|
model = await modelResponse.json()
|
2020-06-18 03:51:10 +12:00
|
|
|
records = await response.json()
|
|
|
|
}
|
|
|
|
|
2020-06-24 00:50:45 +12:00
|
|
|
function linkRecord(id) {
|
|
|
|
if (linkedRecords.has(id)) {
|
2020-06-24 03:20:06 +12:00
|
|
|
linkedRecords.delete(id)
|
2020-06-24 00:50:45 +12:00
|
|
|
} else {
|
|
|
|
linkedRecords.add(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
linkedRecords = linkedRecords
|
2020-06-18 03:51:10 +12:00
|
|
|
}
|
2020-06-24 10:26:54 +12:00
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
fetchRecords()
|
|
|
|
})
|
2020-06-18 03:51:10 +12:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<section>
|
2020-06-24 00:50:45 +12:00
|
|
|
<header>
|
|
|
|
<h3>{linkName}</h3>
|
|
|
|
</header>
|
2020-06-18 03:51:10 +12:00
|
|
|
{#each records as record}
|
2020-06-24 00:50:45 +12:00
|
|
|
<div class="linked-record" on:click={() => linkRecord(record._id)}>
|
|
|
|
<div class="fields" class:selected={linkedRecords.has(record._id)}>
|
2020-06-24 23:41:26 +12:00
|
|
|
{#each Object.keys(model.schema).filter(key => !FIELDS_TO_HIDE.includes(key)) as key}
|
2020-06-18 03:51:10 +12:00
|
|
|
<div class="field">
|
2020-06-24 23:41:26 +12:00
|
|
|
<span>{model.schema[key].name}</span>
|
2020-06-18 03:51:10 +12:00
|
|
|
<p>{record[key]}</p>
|
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<style>
|
2020-06-24 00:50:45 +12:00
|
|
|
.fields.selected {
|
|
|
|
background: var(--light-grey);
|
|
|
|
}
|
|
|
|
|
2020-06-23 08:30:23 +12:00
|
|
|
h3 {
|
2020-06-24 00:50:45 +12:00
|
|
|
font-size: 18px;
|
|
|
|
font-weight: 600;
|
|
|
|
margin-bottom: 12px;
|
|
|
|
color: var(--ink);
|
2020-06-18 03:51:10 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
.fields {
|
2020-06-23 08:30:23 +12:00
|
|
|
padding: 15px;
|
2020-06-18 03:51:10 +12:00
|
|
|
display: grid;
|
|
|
|
grid-template-columns: 1fr 1fr 1fr;
|
|
|
|
grid-gap: 20px;
|
2020-06-24 00:50:45 +12:00
|
|
|
background: var(--white);
|
2020-06-18 03:51:10 +12:00
|
|
|
border: 1px solid var(--grey);
|
|
|
|
border-radius: 5px;
|
2020-06-24 00:50:45 +12:00
|
|
|
transition: 0.5s all;
|
|
|
|
margin-bottom: 8px;
|
2020-06-18 03:51:10 +12:00
|
|
|
}
|
|
|
|
|
2020-06-24 00:50:45 +12:00
|
|
|
.fields:hover {
|
2020-06-23 08:30:23 +12:00
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
|
2020-06-18 03:51:10 +12:00
|
|
|
.field span {
|
|
|
|
color: var(--ink-lighter);
|
|
|
|
font-size: 12px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.field p {
|
|
|
|
color: var(--ink);
|
|
|
|
font-size: 14px;
|
|
|
|
word-break: break-word;
|
2020-06-23 08:30:23 +12:00
|
|
|
font-weight: 500;
|
|
|
|
margin-top: 4px;
|
2020-06-18 03:51:10 +12:00
|
|
|
}
|
2020-06-19 04:55:46 +12:00
|
|
|
</style>
|