2020-06-29 17:32:51 +02:00
|
|
|
<script>
|
|
|
|
import { onMount } from "svelte"
|
2020-07-06 17:24:44 +02:00
|
|
|
import { fade } from "svelte/transition"
|
|
|
|
|
2020-06-29 17:32:51 +02:00
|
|
|
export let _bb
|
|
|
|
export let model
|
2020-07-06 11:17:23 +02:00
|
|
|
export let title
|
2020-07-06 17:24:44 +02:00
|
|
|
export let buttonText
|
2020-06-29 17:32:51 +02:00
|
|
|
|
|
|
|
const TYPE_MAP = {
|
|
|
|
string: "text",
|
|
|
|
boolean: "checkbox",
|
2020-06-29 20:55:27 +02:00
|
|
|
number: "number",
|
2020-06-29 17:32:51 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 21:16:03 +02:00
|
|
|
let record
|
2020-06-29 17:32:51 +02:00
|
|
|
let store = _bb.store
|
|
|
|
let schema = {}
|
|
|
|
let modelDef = {}
|
2020-07-06 17:24:44 +02:00
|
|
|
let saved = false
|
|
|
|
let saving = false
|
2020-07-07 21:16:03 +02:00
|
|
|
let recordId
|
|
|
|
let isNew = true
|
2020-07-06 17:24:44 +02:00
|
|
|
|
|
|
|
let inputElements = {}
|
|
|
|
|
2020-06-29 17:32:51 +02:00
|
|
|
$: if (model && model.length !== 0) {
|
|
|
|
fetchModel()
|
|
|
|
}
|
2020-07-07 21:16:03 +02:00
|
|
|
|
2020-06-29 17:32:51 +02:00
|
|
|
$: fields = Object.keys(schema)
|
2020-07-07 21:16:03 +02:00
|
|
|
|
|
|
|
$: Object.values(inputElements).length && setForm(record)
|
|
|
|
|
2020-06-29 17:32:51 +02:00
|
|
|
async function fetchModel() {
|
|
|
|
const FETCH_MODEL_URL = `/api/models/${model}`
|
|
|
|
const response = await _bb.api.get(FETCH_MODEL_URL)
|
|
|
|
modelDef = await response.json()
|
|
|
|
schema = modelDef.schema
|
|
|
|
}
|
2020-07-07 21:16:03 +02:00
|
|
|
|
2020-06-29 17:32:51 +02:00
|
|
|
async function save() {
|
2020-07-06 17:24:44 +02:00
|
|
|
// prevent double clicking firing multiple requests
|
|
|
|
if (saving) return
|
|
|
|
saving = true
|
2020-06-29 17:32:51 +02:00
|
|
|
const SAVE_RECORD_URL = `/api/${model}/records`
|
2020-07-07 21:16:03 +02:00
|
|
|
const response = await _bb.api.post(SAVE_RECORD_URL, record)
|
|
|
|
|
2020-06-29 17:32:51 +02:00
|
|
|
const json = await response.json()
|
2020-07-06 17:24:44 +02:00
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
store.update(state => {
|
|
|
|
state[model] = state[model] ? [...state[model], json] : [json]
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
|
2020-07-07 21:16:03 +02:00
|
|
|
// wipe form, if new record, otherwise update
|
|
|
|
// model to get new _rev
|
|
|
|
if (isNew) {
|
|
|
|
resetForm()
|
|
|
|
} else {
|
|
|
|
record = json
|
|
|
|
}
|
2020-07-06 17:24:44 +02:00
|
|
|
|
2020-07-06 18:10:55 +02:00
|
|
|
// set saved, and unset after 1 second
|
|
|
|
// i.e. make the success notifier appear, then disappear again after time
|
2020-07-06 17:24:44 +02:00
|
|
|
saved = true
|
|
|
|
setTimeout(() => {
|
|
|
|
saved = false
|
|
|
|
}, 1000)
|
|
|
|
}
|
|
|
|
saving = false
|
|
|
|
}
|
|
|
|
|
2020-07-06 18:10:55 +02:00
|
|
|
// we cannot use svelte bind on these inputs, as it does not allow
|
|
|
|
// bind, when the input type is dynamic
|
2020-07-06 17:24:44 +02:00
|
|
|
const resetForm = () => {
|
|
|
|
for (let el of Object.values(inputElements)) {
|
|
|
|
el.value = ""
|
|
|
|
if (el.checked) {
|
|
|
|
el.checked = false
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 21:16:03 +02:00
|
|
|
record = {
|
2020-07-06 17:24:44 +02:00
|
|
|
modelId: model
|
|
|
|
}
|
2020-06-29 17:32:51 +02:00
|
|
|
}
|
2020-07-06 17:24:44 +02:00
|
|
|
|
2020-07-07 21:16:03 +02:00
|
|
|
const setForm = rec => {
|
|
|
|
if (isNew || !rec) return
|
|
|
|
for (let fieldName in inputElements) {
|
|
|
|
if (typeof rec[fieldName] === "boolean") {
|
|
|
|
inputElements[fieldName].checked = rec[fieldName]
|
|
|
|
} else {
|
|
|
|
inputElements[fieldName].value = rec[fieldName]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:32:51 +02:00
|
|
|
const handleInput = field => event => {
|
|
|
|
let value
|
2020-07-07 21:16:03 +02:00
|
|
|
|
2020-06-29 17:32:51 +02:00
|
|
|
if (event.target.type === "checkbox") {
|
|
|
|
value = event.target.checked
|
2020-07-07 21:16:03 +02:00
|
|
|
record[field] = value
|
2020-06-29 17:32:51 +02:00
|
|
|
return
|
|
|
|
}
|
2020-07-07 21:16:03 +02:00
|
|
|
|
2020-06-29 17:32:51 +02:00
|
|
|
if (event.target.type === "number") {
|
|
|
|
value = parseInt(event.target.value)
|
2020-07-07 21:16:03 +02:00
|
|
|
record[field] = value
|
2020-06-29 17:32:51 +02:00
|
|
|
return
|
|
|
|
}
|
2020-07-07 21:16:03 +02:00
|
|
|
|
2020-06-29 17:32:51 +02:00
|
|
|
value = event.target.value
|
2020-07-07 21:16:03 +02:00
|
|
|
record[field] = value
|
2020-06-29 17:32:51 +02:00
|
|
|
}
|
2020-07-07 21:16:03 +02:00
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
const routeParams = _bb.routeParams()
|
|
|
|
recordId = Object.keys(routeParams).length > 0 && (routeParams.id || routeParams[0])
|
|
|
|
isNew = !recordId || recordId === "new"
|
|
|
|
|
|
|
|
if (isNew) {
|
|
|
|
record = { modelId: model }
|
|
|
|
} else {
|
|
|
|
const GET_RECORD_URL = `/api/${model}/records/${recordId}`
|
|
|
|
_bb.api.get(GET_RECORD_URL)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(rec => {
|
|
|
|
record = rec
|
|
|
|
setForm(rec)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
2020-06-29 17:32:51 +02:00
|
|
|
</script>
|
2020-06-29 20:55:27 +02:00
|
|
|
|
2020-06-29 17:32:51 +02:00
|
|
|
<form class="form" on:submit|preventDefault>
|
2020-07-06 11:17:23 +02:00
|
|
|
{#if title}
|
|
|
|
<h1>{title}</h1>
|
|
|
|
{/if}
|
2020-06-29 17:32:51 +02:00
|
|
|
<hr />
|
|
|
|
<div class="form-content">
|
|
|
|
{#each fields as field}
|
|
|
|
<div class="form-item">
|
|
|
|
<label class="form-label" for="form-stacked-text">{field}</label>
|
2020-06-29 20:55:27 +02:00
|
|
|
{#if schema[field].type === 'string' && schema[field].constraints.inclusion}
|
2020-07-06 17:24:44 +02:00
|
|
|
<select on:blur={handleInput(field)} bind:this={inputElements[field]}>
|
2020-06-29 20:54:30 +02:00
|
|
|
{#each schema[field].constraints.inclusion as opt}
|
2020-06-29 20:55:27 +02:00
|
|
|
<option>{opt}</option>
|
2020-06-29 20:54:30 +02:00
|
|
|
{/each}
|
|
|
|
</select>
|
|
|
|
{:else}
|
|
|
|
<input
|
2020-07-06 17:24:44 +02:00
|
|
|
bind:this={inputElements[field]}
|
2020-06-29 20:54:30 +02:00
|
|
|
class="input"
|
|
|
|
type={TYPE_MAP[schema[field].type]}
|
|
|
|
on:change={handleInput(field)} />
|
|
|
|
{/if}
|
2020-06-29 17:32:51 +02:00
|
|
|
</div>
|
|
|
|
<hr />
|
|
|
|
{/each}
|
|
|
|
<div class="button-block">
|
2020-07-06 17:24:44 +02:00
|
|
|
<button on:click={save} class:saved>
|
|
|
|
{#if saved}
|
|
|
|
<div in:fade>
|
|
|
|
<span class:saved style="margin-right: 5px">🎉</span>Success<span class:saved style="margin-left: 5px">🎉</span>
|
|
|
|
</div>
|
|
|
|
{:else}
|
|
|
|
<div>
|
|
|
|
{buttonText || "Submit Form"}
|
|
|
|
</div>
|
|
|
|
{/if}
|
2020-07-07 21:16:03 +02:00
|
|
|
</button>
|
2020-06-29 17:32:51 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
2020-06-29 20:55:27 +02:00
|
|
|
|
2020-06-29 17:32:51 +02:00
|
|
|
<style>
|
|
|
|
.form {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
justify-content: center;
|
|
|
|
margin: auto;
|
|
|
|
padding: 40px;
|
|
|
|
}
|
|
|
|
.form-content {
|
|
|
|
margin-bottom: 20px;
|
|
|
|
}
|
|
|
|
.input {
|
|
|
|
border-radius: 5px;
|
|
|
|
border: 1px solid #e6e6e6;
|
2020-06-29 20:54:30 +02:00
|
|
|
padding: 1em;
|
2020-06-29 17:32:51 +02:00
|
|
|
font-size: 16px;
|
|
|
|
}
|
2020-06-29 20:54:30 +02:00
|
|
|
|
2020-06-29 17:32:51 +02:00
|
|
|
.form-item {
|
|
|
|
display: grid;
|
|
|
|
grid-template-columns: 30% 1fr;
|
|
|
|
margin-bottom: 16px;
|
|
|
|
align-items: center;
|
|
|
|
gap: 1em;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr {
|
|
|
|
border: 1px solid #f5f5f5;
|
|
|
|
margin: 40px 0px;
|
|
|
|
}
|
|
|
|
hr:nth-last-child(2) {
|
|
|
|
border: 1px solid #f5f5f5;
|
|
|
|
margin: 40px 0px;
|
|
|
|
}
|
|
|
|
.button-block {
|
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
}
|
|
|
|
button {
|
|
|
|
font-size: 16px;
|
|
|
|
padding: 8px 16px;
|
|
|
|
box-sizing: border-box;
|
|
|
|
border-radius: 4px;
|
|
|
|
color: white;
|
2020-06-29 20:54:30 +02:00
|
|
|
background-color: black;
|
2020-06-29 17:32:51 +02:00
|
|
|
outline: none;
|
|
|
|
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-29 20:54:30 +02:00
|
|
|
|
2020-07-06 17:24:44 +02:00
|
|
|
button.saved {
|
|
|
|
background-color: green;
|
|
|
|
}
|
|
|
|
|
2020-06-29 20:55:27 +02: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);
|
|
|
|
}
|
2020-06-29 17:32:51 +02:00
|
|
|
|
2020-06-29 20:55:27 +02:00
|
|
|
input[type="checkbox"] {
|
2020-06-29 17:32:51 +02:00
|
|
|
transform: scale(2);
|
|
|
|
cursor: pointer;
|
2020-06-29 20:55:27 +02:00
|
|
|
}
|
2020-06-29 20:54:30 +02:00
|
|
|
|
2020-06-29 20:55:27 +02:00
|
|
|
select::-ms-expand {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
cursor: pointer;
|
|
|
|
display: inline-block;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
</style>
|