2020-05-08 09:15:09 +12:00
|
|
|
<script>
|
|
|
|
import { onMount } from "svelte"
|
|
|
|
|
|
|
|
export let _bb
|
|
|
|
export let _instanceId
|
|
|
|
export let model
|
|
|
|
export let layout = "list"
|
|
|
|
|
|
|
|
let headers = []
|
|
|
|
let store = _bb.store
|
|
|
|
|
|
|
|
async function fetchData() {
|
2020-06-03 08:31:29 +12:00
|
|
|
if (!model || !model.length) return
|
|
|
|
|
|
|
|
const FETCH_RECORDS_URL = `/api/${_instanceId}/views/all_${model}`
|
2020-05-08 09:15:09 +12:00
|
|
|
const response = await _bb.api.get(FETCH_RECORDS_URL)
|
|
|
|
if (response.status === 200) {
|
|
|
|
const json = await response.json()
|
|
|
|
|
|
|
|
store.update(state => {
|
2020-06-03 08:31:29 +12:00
|
|
|
state[model] = json
|
2020-05-08 09:15:09 +12:00
|
|
|
return state
|
2020-05-18 22:01:09 +12:00
|
|
|
})
|
2020-05-08 09:15:09 +12:00
|
|
|
} else {
|
|
|
|
throw new Error("Failed to fetch records.", response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 08:31:29 +12:00
|
|
|
$: data = $store[model] || []
|
|
|
|
$: if (model) fetchData()
|
2020-05-08 09:15:09 +12:00
|
|
|
|
|
|
|
onMount(async () => {
|
|
|
|
await fetchData()
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
2020-05-18 22:01:09 +12:00
|
|
|
<section class:grid={layout === 'grid'} class:list={layout === 'list'}>
|
2020-05-08 09:15:09 +12:00
|
|
|
{#each data as data}
|
|
|
|
<div class="data-card">
|
|
|
|
<ul>
|
|
|
|
{#each Object.keys(data) as key}
|
|
|
|
<li>
|
2020-05-18 22:01:09 +12:00
|
|
|
<span class="data-key">{key}:</span>
|
|
|
|
<span class="data-value">{data[key]}</span>
|
2020-05-08 09:15:09 +12:00
|
|
|
</li>
|
|
|
|
{/each}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.list {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
font-family: Roboto;
|
|
|
|
}
|
|
|
|
|
|
|
|
.grid {
|
|
|
|
display: grid;
|
|
|
|
grid-template-columns: 1fr 1fr 1fr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ul {
|
|
|
|
list-style-type: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
li {
|
|
|
|
margin: 5px 0 5px 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.data-card {
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
border-radius: 5px;
|
|
|
|
padding: 10px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.data-key {
|
|
|
|
font-weight: bold;
|
|
|
|
font-size: 20px;
|
|
|
|
text-transform: capitalize;
|
|
|
|
}
|
|
|
|
</style>
|