96 lines
1.9 KiB
Svelte
96 lines
1.9 KiB
Svelte
<script>
|
|
import { onMount } from "svelte"
|
|
import { backendUiStore } from "builderStore"
|
|
import api from "builderStore/api"
|
|
|
|
export let modelId
|
|
export let linkName
|
|
export let linked = []
|
|
|
|
let records = []
|
|
|
|
let linkedRecords = new Set(linked)
|
|
|
|
$: linked = [...linkedRecords]
|
|
$: FIELDS_TO_HIDE = ["modelId", "type", "_id", "_rev", $backendUiStore.selectedModel.name]
|
|
|
|
async function fetchRecords() {
|
|
const FETCH_RECORDS_URL = `/api/${modelId}/records`
|
|
const response = await api.get(FETCH_RECORDS_URL)
|
|
records = await response.json()
|
|
}
|
|
|
|
onMount(() => {
|
|
fetchRecords()
|
|
})
|
|
|
|
function linkRecord(id) {
|
|
if (linkedRecords.has(id)) {
|
|
linkedRecords.delete(id);
|
|
} else {
|
|
linkedRecords.add(id)
|
|
}
|
|
|
|
linkedRecords = linkedRecords
|
|
}
|
|
</script>
|
|
|
|
<section>
|
|
<header>
|
|
<h3>{linkName}</h3>
|
|
</header>
|
|
{#each records as record}
|
|
<div class="linked-record" on:click={() => linkRecord(record._id)}>
|
|
<div class="fields" class:selected={linkedRecords.has(record._id)}>
|
|
{#each Object.keys(record).filter(key => !FIELDS_TO_HIDE.includes(key)) as key}
|
|
<div class="field">
|
|
<span>{key}</span>
|
|
<p>{record[key]}</p>
|
|
</div>
|
|
{/each}
|
|
</div>
|
|
</div>
|
|
{/each}
|
|
</section>
|
|
|
|
<style>
|
|
.fields.selected {
|
|
background: var(--light-grey);
|
|
}
|
|
|
|
h3 {
|
|
font-size: 18px;
|
|
font-weight: 600;
|
|
margin-bottom: 12px;
|
|
color: var(--ink);
|
|
}
|
|
|
|
.fields {
|
|
padding: 15px;
|
|
display: grid;
|
|
grid-template-columns: 1fr 1fr 1fr;
|
|
grid-gap: 20px;
|
|
background: var(--white);
|
|
border: 1px solid var(--grey);
|
|
border-radius: 5px;
|
|
transition: 0.5s all;
|
|
margin-bottom: 8px;
|
|
}
|
|
|
|
.fields:hover {
|
|
cursor: pointer;
|
|
}
|
|
|
|
.field span {
|
|
color: var(--ink-lighter);
|
|
font-size: 12px;
|
|
}
|
|
|
|
.field p {
|
|
color: var(--ink);
|
|
font-size: 14px;
|
|
word-break: break-word;
|
|
font-weight: 500;
|
|
margin-top: 4px;
|
|
}
|
|
</style>
|