2020-05-06 21:33:30 +12:00
|
|
|
<script>
|
2020-05-07 21:53:34 +12:00
|
|
|
import { onMount } from "svelte"
|
2020-05-06 21:33:30 +12:00
|
|
|
|
|
|
|
export let _bb
|
2020-05-08 09:15:09 +12:00
|
|
|
export let model
|
2020-05-06 21:33:30 +12:00
|
|
|
|
2020-06-30 06:54:30 +12:00
|
|
|
const TYPE_MAP = {
|
|
|
|
string: "text",
|
|
|
|
boolean: "checkbox",
|
|
|
|
number: "number"
|
|
|
|
}
|
|
|
|
|
2020-05-06 21:33:30 +12:00
|
|
|
let username
|
|
|
|
let password
|
2020-05-08 09:15:09 +12:00
|
|
|
let newModel = {
|
2020-06-04 03:10:03 +12:00
|
|
|
modelId: model,
|
2020-05-08 09:15:09 +12:00
|
|
|
}
|
|
|
|
let store = _bb.store
|
2020-06-04 03:10:03 +12:00
|
|
|
let schema = {}
|
|
|
|
let modelDef = {}
|
2020-05-08 09:15:09 +12:00
|
|
|
|
2020-06-04 03:10:03 +12:00
|
|
|
$: if (model && model.length !== 0) {
|
|
|
|
fetchModel()
|
|
|
|
}
|
|
|
|
|
|
|
|
$: fields = Object.keys(schema)
|
|
|
|
|
|
|
|
async function fetchModel() {
|
2020-06-19 03:59:31 +12:00
|
|
|
const FETCH_MODEL_URL = `/api/models/${model}`
|
2020-06-04 03:10:03 +12:00
|
|
|
const response = await _bb.api.get(FETCH_MODEL_URL)
|
|
|
|
modelDef = await response.json()
|
|
|
|
schema = modelDef.schema
|
|
|
|
}
|
2020-05-08 09:15:09 +12:00
|
|
|
|
|
|
|
async function save() {
|
2020-06-19 03:59:31 +12:00
|
|
|
const SAVE_RECORD_URL = `/api/${model}/records`
|
2020-05-18 22:01:09 +12:00
|
|
|
const response = await _bb.api.post(SAVE_RECORD_URL, newModel)
|
|
|
|
const json = await response.json()
|
2020-05-08 09:15:09 +12:00
|
|
|
|
|
|
|
store.update(state => {
|
2020-06-21 17:01:29 +12:00
|
|
|
state[model] = state[model] ? [...state[model], json] : [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
|
|
|
}
|
|
|
|
|
|
|
|
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 21:33:30 +12:00
|
|
|
</script>
|
|
|
|
|
2020-06-04 09:49:55 +12:00
|
|
|
<form class="form" on:submit|preventDefault>
|
2020-06-06 02:08:03 +12:00
|
|
|
<h1>{modelDef.name} Form</h1>
|
2020-06-30 06:54:30 +12:00
|
|
|
<hr />
|
2020-06-04 09:49:55 +12:00
|
|
|
<div class="form-content">
|
2020-05-08 09:15:09 +12:00
|
|
|
{#each fields as field}
|
2020-06-04 09:49:55 +12:00
|
|
|
<div class="form-item">
|
2020-05-08 09:15:09 +12:00
|
|
|
<label class="form-label" for="form-stacked-text">{field}</label>
|
2020-06-30 06:54:30 +12:00
|
|
|
{#if schema[field].type === "string" && schema[field].constraints.inclusion}
|
|
|
|
<select on:blur={handleInput(field)}>
|
|
|
|
{#each schema[field].constraints.inclusion as opt}
|
|
|
|
<option>{opt}</option>
|
|
|
|
{/each}
|
|
|
|
</select>
|
|
|
|
{:else}
|
|
|
|
<input
|
|
|
|
class="input"
|
|
|
|
type={TYPE_MAP[schema[field].type]}
|
|
|
|
on:change={handleInput(field)} />
|
|
|
|
{/if}
|
2020-05-08 09:15:09 +12:00
|
|
|
</div>
|
2020-06-04 09:49:55 +12:00
|
|
|
<hr />
|
2020-05-08 09:15:09 +12:00
|
|
|
{/each}
|
2020-06-04 09:49:55 +12:00
|
|
|
<div class="button-block">
|
|
|
|
<button on:click={save}>Submit Form</button>
|
|
|
|
</div>
|
2020-05-06 21:33:30 +12:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<style>
|
2020-06-04 09:49:55 +12:00
|
|
|
.form {
|
2020-06-06 02:08:03 +12:00
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2020-06-04 09:49:55 +12:00
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.form-content {
|
|
|
|
margin-bottom: 20px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.input {
|
|
|
|
width: 600px;
|
|
|
|
height: 40px;
|
|
|
|
border-radius: 5px;
|
|
|
|
border: 1px solid #e6e6e6;
|
|
|
|
padding: 6px 12px 6px 12px;
|
|
|
|
font-size: 16px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.form-item {
|
|
|
|
display: flex;
|
2020-06-06 02:08:03 +12:00
|
|
|
flex-direction: column;
|
2020-06-04 09:49:55 +12:00
|
|
|
margin-bottom: 16px;
|
|
|
|
}
|
|
|
|
|
2020-05-08 09:15:09 +12:00
|
|
|
.form-label {
|
|
|
|
font-weight: bold;
|
2020-06-06 02:08:03 +12:00
|
|
|
margin-bottom: 12px;
|
2020-06-04 09:49:55 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
hr {
|
2020-06-23 19:19:16 +12:00
|
|
|
border: 1px solid var(--grey-1);
|
2020-06-04 09:49:55 +12:00
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
|
2020-06-30 06:54:30 +12:00
|
|
|
|
|
|
|
button:hover {
|
|
|
|
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
|
|
|
|
}
|
|
|
|
|
|
|
|
input[type=checkbox] {
|
|
|
|
width: 32px;
|
|
|
|
height: 32px;
|
|
|
|
padding: 0;
|
|
|
|
margin: 0;
|
|
|
|
vertical-align: bottom;
|
|
|
|
position: relative;
|
|
|
|
top: -1px;
|
|
|
|
*overflow: hidden;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
select::-ms-expand {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
display: inline-block;
|
|
|
|
cursor: pointer;
|
|
|
|
align-items: baseline;
|
|
|
|
box-sizing: border-box;
|
|
|
|
padding: 1em 1em;
|
|
|
|
border: 1px solid #eaeaea;
|
|
|
|
border-radius: 5px;
|
|
|
|
font: inherit;
|
|
|
|
line-height: inherit;
|
|
|
|
-webkit-appearance: none;
|
|
|
|
-moz-appearance: none;
|
|
|
|
-ms-appearance: none;
|
|
|
|
appearance: none;
|
|
|
|
background-repeat: no-repeat;
|
|
|
|
background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
|
|
|
|
background-position: right 17px top 1.5em, right 10px top 1.5em;
|
|
|
|
background-size: 7px 7px, 7px 7px;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-06 21:33:30 +12:00
|
|
|
</style>
|