2020-05-06 11:33:30 +02:00
|
|
|
<script>
|
2020-05-07 11:53:34 +02:00
|
|
|
import { onMount } from "svelte"
|
2020-07-06 17:24:44 +02:00
|
|
|
import { fade } from "svelte/transition"
|
2020-10-06 17:46:23 +02:00
|
|
|
import {
|
|
|
|
Label,
|
|
|
|
DatePicker,
|
|
|
|
Input,
|
|
|
|
Select,
|
|
|
|
Button,
|
|
|
|
Toggle,
|
|
|
|
} from "@budibase/bbui"
|
2020-09-23 17:15:09 +02:00
|
|
|
import Dropzone from "./attachments/Dropzone.svelte"
|
2020-10-06 17:46:23 +02:00
|
|
|
import LinkedRecordSelector from "./LinkedRecordSelector.svelte"
|
2020-09-10 14:04:45 +02:00
|
|
|
import debounce from "lodash.debounce"
|
2020-10-06 17:46:23 +02:00
|
|
|
import ErrorsBox from "./ErrorsBox.svelte"
|
|
|
|
import { capitalise } from "./helpers"
|
2020-05-06 11:33:30 +02:00
|
|
|
|
|
|
|
export let _bb
|
2020-05-07 23:15:09 +02:00
|
|
|
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-05-06 11:33:30 +02:00
|
|
|
|
2020-06-29 20:54:30 +02:00
|
|
|
const TYPE_MAP = {
|
|
|
|
string: "text",
|
|
|
|
boolean: "checkbox",
|
2020-06-29 20:55:27 +02:00
|
|
|
number: "number",
|
2020-06-29 20:54:30 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 14:04:45 +02:00
|
|
|
const DEFAULTS_FOR_TYPE = {
|
|
|
|
string: "",
|
|
|
|
boolean: false,
|
|
|
|
number: null,
|
|
|
|
link: [],
|
|
|
|
}
|
|
|
|
|
2020-07-07 21:16:03 +02:00
|
|
|
let record
|
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-07-06 17:24:44 +02:00
|
|
|
let saved = false
|
2020-07-07 21:16:03 +02:00
|
|
|
let recordId
|
|
|
|
let isNew = true
|
2020-09-10 14:04:45 +02:00
|
|
|
let errors = {}
|
2020-05-07 23:15:09 +02:00
|
|
|
|
2020-10-06 17:46:23 +02:00
|
|
|
$: fields = schema ? Object.keys(schema) : []
|
2020-06-03 17:10:03 +02:00
|
|
|
$: if (model && model.length !== 0) {
|
|
|
|
fetchModel()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchModel() {
|
2020-06-18 17:59:31 +02:00
|
|
|
const FETCH_MODEL_URL = `/api/models/${model}`
|
2020-06-03 17:10:03 +02:00
|
|
|
const response = await _bb.api.get(FETCH_MODEL_URL)
|
|
|
|
modelDef = await response.json()
|
|
|
|
schema = modelDef.schema
|
2020-09-10 14:04:45 +02:00
|
|
|
record = {
|
|
|
|
modelId: model,
|
|
|
|
}
|
2020-06-03 17:10:03 +02:00
|
|
|
}
|
2020-05-07 23:15:09 +02:00
|
|
|
|
2020-09-10 14:04:45 +02:00
|
|
|
const save = debounce(async () => {
|
|
|
|
for (let field of fields) {
|
|
|
|
// Assign defaults to empty fields to prevent validation issues
|
2020-09-23 17:15:09 +02:00
|
|
|
if (!(field in record)) {
|
2020-09-10 14:04:45 +02:00
|
|
|
record[field] = DEFAULTS_FOR_TYPE[schema[field].type]
|
2020-09-23 17:15:09 +02:00
|
|
|
}
|
2020-09-10 14:04:45 +02:00
|
|
|
}
|
|
|
|
|
2020-06-18 17:59:31 +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-05-18 12:01:09 +02:00
|
|
|
const json = await response.json()
|
2020-05-07 23:15:09 +02:00
|
|
|
|
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-08 17:31:26 +02:00
|
|
|
|
2020-09-10 14:04:45 +02:00
|
|
|
errors = {}
|
|
|
|
|
2020-07-07 21:16:03 +02:00
|
|
|
// wipe form, if new record, otherwise update
|
2020-07-08 17:31:26 +02:00
|
|
|
// model to get new _rev
|
2020-09-10 14:04:45 +02:00
|
|
|
record = isNew ? { modelId: model } : 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
|
2020-10-06 17:46:23 +02:00
|
|
|
}, 3000)
|
2020-07-06 17:24:44 +02:00
|
|
|
}
|
2020-05-07 23:15:09 +02:00
|
|
|
|
2020-09-10 14:04:45 +02:00
|
|
|
if (response.status === 400) {
|
2020-10-06 17:46:23 +02:00
|
|
|
errors = Object.keys(json.errors)
|
|
|
|
.map(k => ({ dataPath: k, message: json.errors[k] }))
|
|
|
|
.flat()
|
2020-07-07 21:16:03 +02:00
|
|
|
}
|
2020-09-10 14:04:45 +02:00
|
|
|
})
|
2020-07-07 21:16:03 +02:00
|
|
|
|
2020-09-10 14:04:45 +02:00
|
|
|
onMount(async () => {
|
2020-07-07 21:16:03 +02:00
|
|
|
const routeParams = _bb.routeParams()
|
2020-07-08 17:31:26 +02:00
|
|
|
recordId =
|
|
|
|
Object.keys(routeParams).length > 0 && (routeParams.id || routeParams[0])
|
2020-07-07 21:16:03 +02:00
|
|
|
isNew = !recordId || recordId === "new"
|
|
|
|
|
|
|
|
if (isNew) {
|
2020-09-10 14:04:45 +02:00
|
|
|
record = { modelId: model }
|
|
|
|
return
|
2020-07-08 17:31:26 +02:00
|
|
|
}
|
2020-09-10 14:04:45 +02:00
|
|
|
|
|
|
|
const GET_RECORD_URL = `/api/${model}/records/${recordId}`
|
|
|
|
const response = await _bb.api.get(GET_RECORD_URL)
|
2020-10-06 17:46:23 +02:00
|
|
|
record = await response.json()
|
2020-07-08 17:31:26 +02:00
|
|
|
})
|
2020-05-06 11:33:30 +02:00
|
|
|
</script>
|
|
|
|
|
2020-06-03 23:49:55 +02:00
|
|
|
<form class="form" on:submit|preventDefault>
|
2020-07-06 11:17:23 +02:00
|
|
|
{#if title}
|
|
|
|
<h1>{title}</h1>
|
|
|
|
{/if}
|
2020-06-03 23:49:55 +02:00
|
|
|
<div class="form-content">
|
2020-10-06 17:46:23 +02:00
|
|
|
<ErrorsBox {errors} />
|
2020-05-07 23:15:09 +02:00
|
|
|
{#each fields as field}
|
2020-10-06 17:46:23 +02:00
|
|
|
{#if schema[field].type === 'options'}
|
|
|
|
<Select
|
|
|
|
secondary
|
|
|
|
label={capitalise(schema[field].name)}
|
|
|
|
bind:value={record[field]}>
|
|
|
|
<option value="">Choose an option</option>
|
|
|
|
{#each schema[field].constraints.inclusion as opt}
|
|
|
|
<option>{opt}</option>
|
|
|
|
{/each}
|
|
|
|
</Select>
|
|
|
|
{:else if schema[field].type === 'datetime'}
|
|
|
|
<DatePicker
|
|
|
|
label={capitalise(schema[field].name)}
|
|
|
|
bind:value={record[field]} />
|
|
|
|
{:else if schema[field].type === 'boolean'}
|
|
|
|
<Toggle
|
|
|
|
text={capitalise(schema[field].name)}
|
|
|
|
bind:checked={record[field]} />
|
|
|
|
{:else if schema[field].type === 'number'}
|
|
|
|
<Input
|
|
|
|
label={capitalise(schema[field].name)}
|
|
|
|
type="number"
|
|
|
|
bind:value={record[field]} />
|
|
|
|
{:else if schema[field].type === 'string'}
|
|
|
|
<Input
|
|
|
|
label={capitalise(schema[field].name)}
|
|
|
|
bind:value={record[field]} />
|
|
|
|
{:else if schema[field].type === 'attachment'}
|
|
|
|
<div>
|
|
|
|
<Label extraSmall grey>{schema[field].name}</Label>
|
2020-09-23 17:15:09 +02:00
|
|
|
<Dropzone bind:files={record[field]} />
|
2020-10-06 17:46:23 +02:00
|
|
|
</div>
|
|
|
|
{:else if schema[field].type === 'link'}
|
|
|
|
<LinkedRecordSelector
|
|
|
|
secondary
|
|
|
|
bind:linkedRecords={record[field]}
|
|
|
|
schema={schema[field]} />
|
|
|
|
{/if}
|
2020-05-07 23:15:09 +02:00
|
|
|
{/each}
|
2020-10-06 17:46:23 +02:00
|
|
|
<div class="buttons">
|
|
|
|
<Button primary on:click={save} green={saved}>
|
|
|
|
{#if saved}Success{:else}{buttonText || 'Submit Form'}{/if}
|
|
|
|
</Button>
|
2020-06-03 23:49:55 +02:00
|
|
|
</div>
|
2020-05-06 11:33:30 +02:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<style>
|
2020-06-03 23:49:55 +02:00
|
|
|
.form {
|
2020-06-05 16:08:03 +02:00
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2020-06-03 23:49:55 +02:00
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.form-content {
|
2020-10-06 17:46:23 +02:00
|
|
|
margin-bottom: var(--spacing-xl);
|
|
|
|
display: grid;
|
|
|
|
gap: var(--spacing-xl);
|
2020-07-12 20:19:12 +02:00
|
|
|
width: 100%;
|
2020-06-03 23:49:55 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 17:46:23 +02:00
|
|
|
.buttons {
|
2020-06-03 23:49:55 +02:00
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
}
|
2020-05-06 11:33:30 +02:00
|
|
|
</style>
|