2020-05-06 21:33:30 +12:00
|
|
|
<script>
|
2020-05-07 21:53:34 +12:00
|
|
|
import { onMount } from "svelte"
|
2020-07-07 03:24:44 +12:00
|
|
|
import { fade } from "svelte/transition"
|
2020-09-11 00:04:45 +12:00
|
|
|
import { Label, DatePicker } from "@budibase/bbui"
|
2020-09-24 03:15:09 +12:00
|
|
|
import Dropzone from "./attachments/Dropzone.svelte"
|
2020-09-11 00:04:45 +12:00
|
|
|
import debounce from "lodash.debounce"
|
2020-05-06 21:33:30 +12:00
|
|
|
|
|
|
|
export let _bb
|
2020-05-08 09:15:09 +12:00
|
|
|
export let model
|
2020-07-06 21:17:23 +12:00
|
|
|
export let title
|
2020-07-07 03:24:44 +12:00
|
|
|
export let buttonText
|
2020-05-06 21:33:30 +12:00
|
|
|
|
2020-06-30 06:54:30 +12:00
|
|
|
const TYPE_MAP = {
|
|
|
|
string: "text",
|
|
|
|
boolean: "checkbox",
|
2020-06-30 06:55:27 +12:00
|
|
|
number: "number",
|
2020-06-30 06:54:30 +12:00
|
|
|
}
|
|
|
|
|
2020-09-11 00:04:45 +12:00
|
|
|
const DEFAULTS_FOR_TYPE = {
|
|
|
|
string: "",
|
|
|
|
boolean: false,
|
|
|
|
number: null,
|
|
|
|
link: [],
|
|
|
|
}
|
|
|
|
|
2020-07-08 07:16:03 +12:00
|
|
|
let record
|
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-07-07 03:24:44 +12:00
|
|
|
let saved = false
|
2020-07-08 07:16:03 +12:00
|
|
|
let recordId
|
|
|
|
let isNew = true
|
2020-09-11 00:04:45 +12:00
|
|
|
let errors = {}
|
2020-05-08 09:15:09 +12:00
|
|
|
|
2020-06-04 03:10:03 +12:00
|
|
|
$: if (model && model.length !== 0) {
|
|
|
|
fetchModel()
|
|
|
|
}
|
|
|
|
|
2020-09-11 00:04:45 +12:00
|
|
|
$: fields = schema ? Object.keys(schema) : []
|
2020-06-04 03:10:03 +12:00
|
|
|
|
2020-09-11 00:04:45 +12:00
|
|
|
$: errorMessages = Object.entries(errors).map(
|
|
|
|
([field, message]) => `${field} ${message}`
|
|
|
|
)
|
2020-07-08 07:16:03 +12:00
|
|
|
|
2020-06-04 03:10:03 +12:00
|
|
|
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-09-11 00:04:45 +12:00
|
|
|
record = {
|
|
|
|
modelId: model,
|
|
|
|
}
|
2020-06-04 03:10:03 +12:00
|
|
|
}
|
2020-05-08 09:15:09 +12:00
|
|
|
|
2020-09-11 00:04:45 +12:00
|
|
|
const save = debounce(async () => {
|
|
|
|
for (let field of fields) {
|
|
|
|
// Assign defaults to empty fields to prevent validation issues
|
2020-09-24 03:15:09 +12:00
|
|
|
if (!(field in record)) {
|
2020-09-11 00:04:45 +12:00
|
|
|
record[field] = DEFAULTS_FOR_TYPE[schema[field].type]
|
2020-09-24 03:15:09 +12:00
|
|
|
}
|
2020-09-11 00:04:45 +12:00
|
|
|
}
|
|
|
|
|
2020-06-19 03:59:31 +12:00
|
|
|
const SAVE_RECORD_URL = `/api/${model}/records`
|
2020-07-08 07:16:03 +12:00
|
|
|
const response = await _bb.api.post(SAVE_RECORD_URL, record)
|
|
|
|
|
2020-05-18 22:01:09 +12:00
|
|
|
const json = await response.json()
|
2020-05-08 09:15:09 +12:00
|
|
|
|
2020-07-07 03:24:44 +12:00
|
|
|
if (response.status === 200) {
|
|
|
|
store.update(state => {
|
|
|
|
state[model] = state[model] ? [...state[model], json] : [json]
|
|
|
|
return state
|
|
|
|
})
|
2020-07-09 03:31:26 +12:00
|
|
|
|
2020-09-11 00:04:45 +12:00
|
|
|
errors = {}
|
|
|
|
|
2020-07-08 07:16:03 +12:00
|
|
|
// wipe form, if new record, otherwise update
|
2020-07-09 03:31:26 +12:00
|
|
|
// model to get new _rev
|
2020-09-11 00:04:45 +12:00
|
|
|
record = isNew ? { modelId: model } : json
|
2020-07-07 03:24:44 +12:00
|
|
|
|
2020-07-07 04:10:55 +12:00
|
|
|
// set saved, and unset after 1 second
|
|
|
|
// i.e. make the success notifier appear, then disappear again after time
|
2020-07-07 03:24:44 +12:00
|
|
|
saved = true
|
|
|
|
setTimeout(() => {
|
|
|
|
saved = false
|
|
|
|
}, 1000)
|
|
|
|
}
|
2020-05-08 09:15:09 +12:00
|
|
|
|
2020-09-11 00:04:45 +12:00
|
|
|
if (response.status === 400) {
|
|
|
|
errors = json.errors
|
2020-07-08 07:16:03 +12:00
|
|
|
}
|
2020-09-11 00:04:45 +12:00
|
|
|
})
|
2020-07-08 07:16:03 +12:00
|
|
|
|
2020-09-11 00:04:45 +12:00
|
|
|
onMount(async () => {
|
2020-07-08 07:16:03 +12:00
|
|
|
const routeParams = _bb.routeParams()
|
2020-07-09 03:31:26 +12:00
|
|
|
recordId =
|
|
|
|
Object.keys(routeParams).length > 0 && (routeParams.id || routeParams[0])
|
2020-07-08 07:16:03 +12:00
|
|
|
isNew = !recordId || recordId === "new"
|
|
|
|
|
|
|
|
if (isNew) {
|
2020-09-11 00:04:45 +12:00
|
|
|
record = { modelId: model }
|
|
|
|
return
|
2020-07-09 03:31:26 +12:00
|
|
|
}
|
2020-09-11 00:04:45 +12:00
|
|
|
|
|
|
|
const GET_RECORD_URL = `/api/${model}/records/${recordId}`
|
|
|
|
const response = await _bb.api.get(GET_RECORD_URL)
|
|
|
|
const json = await response.json()
|
|
|
|
record = json
|
2020-07-09 03:31:26 +12:00
|
|
|
})
|
2020-05-06 21:33:30 +12:00
|
|
|
</script>
|
|
|
|
|
2020-06-04 09:49:55 +12:00
|
|
|
<form class="form" on:submit|preventDefault>
|
2020-07-06 21:17:23 +12:00
|
|
|
{#if title}
|
|
|
|
<h1>{title}</h1>
|
|
|
|
{/if}
|
2020-09-11 00:04:45 +12:00
|
|
|
{#each errorMessages as error}
|
|
|
|
<p class="error">{error}</p>
|
|
|
|
{/each}
|
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-09-02 08:28:38 +12:00
|
|
|
<Label small forAttr={'form-stacked-text'}>{field}</Label>
|
2020-06-30 06:55:27 +12:00
|
|
|
{#if schema[field].type === 'string' && schema[field].constraints.inclusion}
|
2020-09-11 00:04:45 +12:00
|
|
|
<select bind:value={record[field]}>
|
2020-06-30 06:54:30 +12:00
|
|
|
{#each schema[field].constraints.inclusion as opt}
|
2020-06-30 06:55:27 +12:00
|
|
|
<option>{opt}</option>
|
2020-06-30 06:54:30 +12:00
|
|
|
{/each}
|
|
|
|
</select>
|
2020-09-11 00:04:45 +12:00
|
|
|
{:else if schema[field].type === 'datetime'}
|
|
|
|
<DatePicker bind:value={record[field]} />
|
|
|
|
{:else if schema[field].type === 'boolean'}
|
2020-09-11 00:10:57 +12:00
|
|
|
<input class="input" type="checkbox" bind:checked={record[field]} />
|
2020-09-11 00:04:45 +12:00
|
|
|
{:else if schema[field].type === 'number'}
|
|
|
|
<input class="input" type="number" bind:value={record[field]} />
|
|
|
|
{:else if schema[field].type === 'string'}
|
|
|
|
<input class="input" type="text" bind:value={record[field]} />
|
2020-09-24 03:15:09 +12:00
|
|
|
{:else if schema[field].type === 'attachment'}
|
|
|
|
<Dropzone bind:files={record[field]} />
|
2020-06-30 06:54:30 +12:00
|
|
|
{/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">
|
2020-07-07 03:24:44 +12:00
|
|
|
<button on:click={save} class:saved>
|
|
|
|
{#if saved}
|
|
|
|
<div in:fade>
|
2020-07-13 06:19:12 +12:00
|
|
|
<span class:saved>Success</span>
|
2020-07-07 03:24:44 +12:00
|
|
|
</div>
|
|
|
|
{:else}
|
2020-07-09 03:31:26 +12:00
|
|
|
<div>{buttonText || 'Submit Form'}</div>
|
2020-07-07 03:24:44 +12:00
|
|
|
{/if}
|
2020-07-09 03:31:26 +12:00
|
|
|
</button>
|
2020-06-04 09:49:55 +12:00
|
|
|
</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;
|
2020-07-13 06:19:12 +12:00
|
|
|
width: 100%;
|
2020-06-04 09:49:55 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
.input {
|
|
|
|
border-radius: 5px;
|
|
|
|
border: 1px solid #e6e6e6;
|
2020-07-13 06:19:12 +12:00
|
|
|
padding: 1rem;
|
2020-06-04 09:49:55 +12:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-07 03:24:44 +12:00
|
|
|
button.saved {
|
2020-07-13 06:19:12 +12:00
|
|
|
background-color: #84c991;
|
|
|
|
border: none;
|
2020-07-09 03:31:26 +12:00
|
|
|
}
|
2020-07-07 03:24:44 +12:00
|
|
|
|
2020-06-30 06:55:27 +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);
|
|
|
|
}
|
2020-06-30 06:54:30 +12:00
|
|
|
|
2020-06-30 06:55:27 +12:00
|
|
|
input[type="checkbox"] {
|
|
|
|
width: 32px;
|
|
|
|
height: 32px;
|
|
|
|
padding: 0;
|
|
|
|
margin: 0;
|
|
|
|
vertical-align: bottom;
|
|
|
|
position: relative;
|
|
|
|
top: -1px;
|
|
|
|
*overflow: hidden;
|
|
|
|
}
|
2020-06-30 06:54:30 +12:00
|
|
|
|
2020-06-30 06:55:27 +12:00
|
|
|
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-09-11 00:04:45 +12:00
|
|
|
|
|
|
|
.error {
|
|
|
|
color: red;
|
|
|
|
font-weight: 500;
|
|
|
|
}
|
2020-05-06 21:33:30 +12:00
|
|
|
</style>
|