2020-10-08 11:42:53 +02:00
|
|
|
<script>
|
|
|
|
import { onMount } from "svelte"
|
|
|
|
import { fade } from "svelte/transition"
|
|
|
|
import {
|
|
|
|
Label,
|
|
|
|
DatePicker,
|
|
|
|
Input,
|
|
|
|
Select,
|
|
|
|
Button,
|
|
|
|
Toggle,
|
|
|
|
} from "@budibase/bbui"
|
|
|
|
import Dropzone from "./attachments/Dropzone.svelte"
|
2020-10-09 20:10:28 +02:00
|
|
|
import LinkedRowSelector from "./LinkedRowSelector.svelte"
|
2020-10-08 11:42:53 +02:00
|
|
|
import debounce from "lodash.debounce"
|
|
|
|
import ErrorsBox from "./ErrorsBox.svelte"
|
|
|
|
import { capitalise } from "./helpers"
|
|
|
|
|
|
|
|
export let _bb
|
2020-10-09 19:49:23 +02:00
|
|
|
export let table
|
2020-10-08 11:42:53 +02:00
|
|
|
export let title
|
|
|
|
export let buttonText
|
|
|
|
export let wide = false
|
|
|
|
|
|
|
|
const TYPE_MAP = {
|
|
|
|
string: "text",
|
|
|
|
boolean: "checkbox",
|
|
|
|
number: "number",
|
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULTS_FOR_TYPE = {
|
|
|
|
string: "",
|
|
|
|
boolean: false,
|
|
|
|
number: null,
|
|
|
|
link: [],
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
let row
|
2020-10-08 11:42:53 +02:00
|
|
|
let store = _bb.store
|
|
|
|
let schema = {}
|
2020-10-09 19:49:23 +02:00
|
|
|
let tableDef = {}
|
2020-10-08 11:42:53 +02:00
|
|
|
let saved = false
|
2020-10-09 20:10:28 +02:00
|
|
|
let rowId
|
2020-10-08 11:42:53 +02:00
|
|
|
let isNew = true
|
|
|
|
let errors = {}
|
|
|
|
|
|
|
|
$: fields = schema ? Object.keys(schema) : []
|
2020-10-09 19:49:23 +02:00
|
|
|
$: if (table && table.length !== 0) {
|
|
|
|
fetchTable()
|
2020-10-08 11:42:53 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:49:23 +02:00
|
|
|
async function fetchTable() {
|
|
|
|
const FETCH_TABLE_URL = `/api/tables/${table}`
|
|
|
|
const response = await _bb.api.get(FETCH_TABLE_URL)
|
|
|
|
tableDef = await response.json()
|
|
|
|
schema = tableDef.schema
|
2020-10-09 20:10:28 +02:00
|
|
|
row = {
|
2020-10-09 19:49:23 +02:00
|
|
|
tableId: table,
|
2020-10-08 11:42:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const save = debounce(async () => {
|
|
|
|
for (let field of fields) {
|
|
|
|
// Assign defaults to empty fields to prevent validation issues
|
2020-10-09 20:10:28 +02:00
|
|
|
if (!(field in row)) {
|
|
|
|
row[field] = DEFAULTS_FOR_TYPE[schema[field].type]
|
2020-10-08 11:42:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
const SAVE_ROW_URL = `/api/${table}/rows`
|
|
|
|
const response = await _bb.api.post(SAVE_ROW_URL, row)
|
2020-10-08 11:42:53 +02:00
|
|
|
|
|
|
|
const json = await response.json()
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
store.update(state => {
|
2020-10-09 19:49:23 +02:00
|
|
|
state[table] = state[table] ? [...state[table], json] : [json]
|
2020-10-08 11:42:53 +02:00
|
|
|
return state
|
|
|
|
})
|
|
|
|
|
|
|
|
errors = {}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
// wipe form, if new row, otherwise update
|
2020-10-09 19:49:23 +02:00
|
|
|
// table to get new _rev
|
2020-10-09 20:10:28 +02:00
|
|
|
row = isNew ? { tableId: table } : json
|
2020-10-08 11:42:53 +02:00
|
|
|
|
|
|
|
// set saved, and unset after 1 second
|
|
|
|
// i.e. make the success notifier appear, then disappear again after time
|
|
|
|
saved = true
|
|
|
|
setTimeout(() => {
|
|
|
|
saved = false
|
|
|
|
}, 3000)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.status === 400) {
|
|
|
|
errors = Object.keys(json.errors)
|
|
|
|
.map(k => ({ dataPath: k, message: json.errors[k] }))
|
|
|
|
.flat()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
onMount(async () => {
|
|
|
|
const routeParams = _bb.routeParams()
|
2020-10-09 20:10:28 +02:00
|
|
|
rowId =
|
2020-10-08 11:42:53 +02:00
|
|
|
Object.keys(routeParams).length > 0 && (routeParams.id || routeParams[0])
|
2020-10-09 20:10:28 +02:00
|
|
|
isNew = !rowId || rowId === "new"
|
2020-10-08 11:42:53 +02:00
|
|
|
|
|
|
|
if (isNew) {
|
2020-10-09 20:10:28 +02:00
|
|
|
row = { tableId: table }
|
2020-10-08 11:42:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
const GET_ROW_URL = `/api/${table}/rows/${rowId}`
|
|
|
|
const response = await _bb.api.get(GET_ROW_URL)
|
|
|
|
row = await response.json()
|
2020-10-08 11:42:53 +02:00
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<form class="form" on:submit|preventDefault>
|
|
|
|
{#if title}
|
|
|
|
<h1>{title}</h1>
|
|
|
|
{/if}
|
|
|
|
<div class="form-content">
|
|
|
|
<ErrorsBox {errors} />
|
|
|
|
{#each fields as field}
|
|
|
|
<div class="form-field" class:wide>
|
|
|
|
{#if !(schema[field].type === 'boolean' && !wide)}
|
|
|
|
<Label extraSmall={!wide} grey={!wide}>
|
|
|
|
{capitalise(schema[field].name)}
|
|
|
|
</Label>
|
|
|
|
{/if}
|
|
|
|
{#if schema[field].type === 'options'}
|
2020-10-09 20:10:28 +02:00
|
|
|
<Select secondary bind:value={row[field]}>
|
2020-10-08 11:42:53 +02:00
|
|
|
<option value="">Choose an option</option>
|
|
|
|
{#each schema[field].constraints.inclusion as opt}
|
|
|
|
<option>{opt}</option>
|
|
|
|
{/each}
|
|
|
|
</Select>
|
|
|
|
{:else if schema[field].type === 'datetime'}
|
2020-10-09 20:10:28 +02:00
|
|
|
<DatePicker bind:value={row[field]} />
|
2020-10-08 11:42:53 +02:00
|
|
|
{:else if schema[field].type === 'boolean'}
|
|
|
|
<Toggle
|
|
|
|
text={wide ? null : capitalise(schema[field].name)}
|
2020-10-09 20:10:28 +02:00
|
|
|
bind:checked={row[field]} />
|
2020-10-08 11:42:53 +02:00
|
|
|
{:else if schema[field].type === 'number'}
|
2020-10-09 20:10:28 +02:00
|
|
|
<Input type="number" bind:value={row[field]} />
|
2020-10-08 11:42:53 +02:00
|
|
|
{:else if schema[field].type === 'string'}
|
2020-10-09 20:10:28 +02:00
|
|
|
<Input bind:value={row[field]} />
|
2020-10-08 11:42:53 +02:00
|
|
|
{:else if schema[field].type === 'attachment'}
|
2020-10-09 20:10:28 +02:00
|
|
|
<Dropzone bind:files={row[field]} />
|
2020-10-08 11:42:53 +02:00
|
|
|
{:else if schema[field].type === 'link'}
|
2020-10-09 20:10:28 +02:00
|
|
|
<LinkedRowSelector
|
2020-10-08 11:42:53 +02:00
|
|
|
secondary
|
|
|
|
showLabel={false}
|
2020-10-09 20:10:28 +02:00
|
|
|
bind:linkedRows={row[field]}
|
2020-10-08 11:42:53 +02:00
|
|
|
schema={schema[field]} />
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
<div class="buttons">
|
|
|
|
<Button primary on:click={save} green={saved}>
|
|
|
|
{#if saved}Success{:else}{buttonText || 'Submit Form'}{/if}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.form {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.form-content {
|
|
|
|
margin-bottom: var(--spacing-xl);
|
|
|
|
display: grid;
|
|
|
|
gap: var(--spacing-xl);
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.form-field {
|
|
|
|
display: grid;
|
|
|
|
}
|
|
|
|
.form-field.wide {
|
|
|
|
align-items: center;
|
|
|
|
grid-template-columns: 30% 1fr;
|
|
|
|
gap: var(--spacing-xl);
|
|
|
|
}
|
|
|
|
.form-field.wide :global(label) {
|
|
|
|
margin-bottom: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.buttons {
|
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
}
|
|
|
|
</style>
|