2020-05-06 11:33:30 +02:00
|
|
|
<script>
|
2020-05-07 11:53:34 +02:00
|
|
|
import { onMount } from "svelte"
|
2020-05-06 11:33:30 +02:00
|
|
|
|
|
|
|
export let _bb
|
|
|
|
export let _instanceId
|
2020-05-07 23:15:09 +02:00
|
|
|
export let model
|
2020-05-06 11:33:30 +02:00
|
|
|
|
|
|
|
let username
|
|
|
|
let password
|
2020-05-07 23:15:09 +02:00
|
|
|
let newModel = {
|
2020-06-03 17:10:03 +02:00
|
|
|
modelId: model,
|
2020-05-07 23:15:09 +02:00
|
|
|
}
|
|
|
|
let store = _bb.store
|
2020-06-03 17:10:03 +02:00
|
|
|
let schema = {}
|
|
|
|
let modelDef = {}
|
2020-05-07 23:15:09 +02:00
|
|
|
|
2020-06-03 17:10:03 +02:00
|
|
|
$: if (model && model.length !== 0) {
|
|
|
|
fetchModel()
|
|
|
|
}
|
|
|
|
|
|
|
|
$: fields = Object.keys(schema)
|
|
|
|
|
|
|
|
async function fetchModel() {
|
|
|
|
const FETCH_MODEL_URL = `/api/${_instanceId}/models/${model}`
|
|
|
|
const response = await _bb.api.get(FETCH_MODEL_URL)
|
|
|
|
modelDef = await response.json()
|
|
|
|
schema = modelDef.schema
|
|
|
|
}
|
2020-05-07 23:15:09 +02:00
|
|
|
|
|
|
|
async function save() {
|
2020-06-03 17:10:03 +02:00
|
|
|
const SAVE_RECORD_URL = `/api/${_instanceId}/${model}/records`
|
2020-05-18 12:01:09 +02:00
|
|
|
const response = await _bb.api.post(SAVE_RECORD_URL, newModel)
|
|
|
|
const json = await response.json()
|
2020-05-07 23:15:09 +02:00
|
|
|
|
|
|
|
store.update(state => {
|
2020-06-03 17:10:03 +02:00
|
|
|
state[model._id] = [...state[model], json]
|
2020-05-07 23:15:09 +02:00
|
|
|
return state
|
2020-05-18 12:01:09 +02:00
|
|
|
})
|
2020-05-07 23:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const handleInput = field => event => {
|
|
|
|
let value
|
|
|
|
|
|
|
|
if (event.target.type === "checkbox") {
|
|
|
|
value = event.target.checked
|
|
|
|
newModel[field] = value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.target.type === "number") {
|
|
|
|
value = parseInt(event.target.value)
|
|
|
|
newModel[field] = value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
value = event.target.value
|
|
|
|
newModel[field] = value
|
|
|
|
}
|
2020-05-06 11:33:30 +02:00
|
|
|
</script>
|
|
|
|
|
2020-06-03 23:49:55 +02:00
|
|
|
<form class="form" on:submit|preventDefault>
|
|
|
|
<div class="form-content">
|
2020-05-07 23:15:09 +02:00
|
|
|
{#each fields as field}
|
2020-06-03 23:49:55 +02:00
|
|
|
<div class="form-item">
|
2020-05-07 23:15:09 +02:00
|
|
|
<label class="form-label" for="form-stacked-text">{field}</label>
|
2020-05-18 12:01:09 +02:00
|
|
|
<input
|
2020-06-03 23:49:55 +02:00
|
|
|
class="input"
|
|
|
|
placeholder={field}
|
2020-06-03 17:10:03 +02:00
|
|
|
type={schema[field].type === 'string' ? 'text' : schema[field].type}
|
2020-05-18 12:01:09 +02:00
|
|
|
on:change={handleInput(field)} />
|
2020-05-07 23:15:09 +02:00
|
|
|
</div>
|
2020-06-03 23:49:55 +02:00
|
|
|
<hr />
|
2020-05-07 23:15:09 +02:00
|
|
|
{/each}
|
2020-06-03 23:49:55 +02:00
|
|
|
<div class="button-block">
|
|
|
|
<button on:click={save}>Submit Form</button>
|
|
|
|
</div>
|
2020-05-06 11:33:30 +02:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<style>
|
2020-06-03 23:49:55 +02:00
|
|
|
.form {
|
|
|
|
align-items: center;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.form-content {
|
|
|
|
margin-bottom: 20px;
|
|
|
|
justify-content: space-between;
|
|
|
|
align-items: baseline;
|
|
|
|
}
|
|
|
|
|
|
|
|
.input {
|
|
|
|
width: 600px;
|
|
|
|
height: 40px;
|
|
|
|
border-radius: 5px;
|
|
|
|
border: 1px solid #e6e6e6;
|
|
|
|
padding: 6px 12px 6px 12px;
|
|
|
|
font-size: 16px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.form-item {
|
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
margin-bottom: 16px;
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:15:09 +02:00
|
|
|
.form-label {
|
|
|
|
font-weight: bold;
|
2020-06-03 23:49:55 +02:00
|
|
|
margin-bottom: 8px;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr {
|
|
|
|
border: 1px solid #fafafa;
|
|
|
|
margin: 20px 0px;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr:nth-last-child(2) {
|
|
|
|
border: 1px solid #fff;
|
|
|
|
margin: 20px 0px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.button-block {
|
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
}
|
|
|
|
|
|
|
|
button {
|
|
|
|
font-family: Inter;
|
|
|
|
font-size: 16px;
|
|
|
|
padding: 0.4em;
|
|
|
|
box-sizing: border-box;
|
|
|
|
border-radius: 4px;
|
|
|
|
color: white;
|
|
|
|
background-color: #393c44;
|
|
|
|
outline: none;
|
|
|
|
width: 300px;
|
|
|
|
height: 40px;
|
|
|
|
cursor: pointer;
|
|
|
|
transition: all 0.2s ease 0s;
|
|
|
|
overflow: hidden;
|
|
|
|
outline: none;
|
|
|
|
user-select: none;
|
|
|
|
white-space: nowrap;
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
button:hover {
|
|
|
|
background-color: white;
|
|
|
|
border-color: #393c44;
|
|
|
|
color: #393c44;
|
2020-05-07 23:15:09 +02:00
|
|
|
}
|
2020-05-06 11:33:30 +02:00
|
|
|
</style>
|