validation for delete index and records

This commit is contained in:
Michael Shanks 2020-03-25 16:57:04 +00:00
parent f3f8026e6f
commit 9ee636092a
8 changed files with 40 additions and 27 deletions

View File

@ -13,6 +13,8 @@ import {
constructHierarchy, constructHierarchy,
templateApi, templateApi,
isIndex, isIndex,
canDeleteIndex,
canDeleteRecord
} from "../../common/core" } from "../../common/core"
export const getBackendUiStore = () => { export const getBackendUiStore = () => {
@ -202,14 +204,27 @@ export const deleteCurrentNode = store => () => {
? state.hierarchy.children.find(node => node !== state.currentNode) ? state.hierarchy.children.find(node => node !== state.currentNode)
: nodeToDelete.parent() : nodeToDelete.parent()
const recordOrIndexKey = hierarchyFunctions.isRecord(nodeToDelete) ? "children" : "indexes"; const isRecord = hierarchyFunctions.isRecord(nodeToDelete)
const check = isRecord
? canDeleteRecord(nodeToDelete)
: canDeleteIndex(nodeToDelete)
if (!check.canDelete) {
state.errors = check.errors.map(e => ({ error: e }))
return state
}
const recordOrIndexKey = isRecord ? "children" : "indexes"
// remove the selected record or index // remove the selected record or index
nodeToDelete.parent()[recordOrIndexKey] = remove( const newCollection = remove(
nodeToDelete.parent()[recordOrIndexKey], node => node.nodeId === nodeToDelete.nodeId,
node => node.nodeId === nodeToDelete.nodeId nodeToDelete.parent()[recordOrIndexKey]
) )
nodeToDelete.parent()[recordOrIndexKey] = newCollection
state.errors = [] state.errors = []
saveBackend(state) saveBackend(state)
return state return state

View File

@ -5,25 +5,14 @@
</script> </script>
{#if hasErrors} {#if hasErrors}
<div class="error-container"> <div uk-alert class="uk-alert-danger">
{#each errors as error} {#each errors as error}
<div class="error-row"> <div>
{error.field ? `${error.field}: ` : ''}{error.error} {error.field ? `${error.field}: ` : ''}{error.error}
</div> </div>
{/each} {/each}
</div> </div>
{/if} {/if}
<style>
.error-container {
padding: 10px;
border-style: solid;
border-color: var(--deletion100);
border-radius: var(--borderradiusall);
background: var(--deletion75);
}
.error-row {
padding: 5px 0px;
}
</style>

View File

@ -9,6 +9,8 @@ import { find, filter, keyBy, flatten, map } from "lodash/fp"
import { generateSchema } from "../../../core/src/indexing/indexSchemaCreator" import { generateSchema } from "../../../core/src/indexing/indexSchemaCreator"
import { generate } from "shortid" import { generate } from "shortid"
export { canDeleteIndex } from "../../../core/src/templateApi/canDeleteIndex"
export { canDeleteRecord } from "../../../core/src/templateApi/canDeleteRecord"
export { userWithFullAccess } from "../../../core/src/index" export { userWithFullAccess } from "../../../core/src/index"
export const pipe = common.$ export const pipe = common.$

View File

@ -31,9 +31,6 @@
</ActionButton> </ActionButton>
</div> </div>
{#if $store.errors && $store.errors.length > 0}
<ErrorsBox errors={$store.errors} />
{/if}
</div> </div>
<style> <style>

View File

@ -6,6 +6,7 @@
import { store } from "../builderStore" import { store } from "../builderStore"
import { filter, some, map, compose } from "lodash/fp" import { filter, some, map, compose } from "lodash/fp"
import { hierarchy as hierarchyFunctions, common } from "../../../core/src" import { hierarchy as hierarchyFunctions, common } from "../../../core/src"
import ErrorsBox from "../common/ErrorsBox.svelte"
const SNIPPET_EDITORS = { const SNIPPET_EDITORS = {
MAP: "Map", MAP: "Map",
@ -49,6 +50,9 @@
</heading> </heading>
<form class="uk-form-stacked root"> <form class="uk-form-stacked root">
<h4 class="budibase__label--big">Settings</h4> <h4 class="budibase__label--big">Settings</h4>
{#if $store.errors && $store.errors.length > 0}
<ErrorsBox errors={$store.errors} />
{/if}
<div class="uk-grid-small" uk-grid> <div class="uk-grid-small" uk-grid>
<div class="uk-width-1-2@s"> <div class="uk-width-1-2@s">
<Textbox bind:text={index.name} label="Name" /> <Textbox bind:text={index.name} label="Name" />

View File

@ -10,6 +10,7 @@
import { common, hierarchy } from "../../../core/src" import { common, hierarchy } from "../../../core/src"
import { templateApi, pipe, validate } from "../common/core" import { templateApi, pipe, validate } from "../common/core"
import ActionsHeader from "./ActionsHeader.svelte" import ActionsHeader from "./ActionsHeader.svelte"
import ErrorsBox from "../common/ErrorsBox.svelte"
let record let record
let getIndexAllowedRecords let getIndexAllowedRecords
@ -99,6 +100,11 @@
</heading> </heading>
{#if !editingField} {#if !editingField}
<h4 class="budibase__label--big">Settings</h4> <h4 class="budibase__label--big">Settings</h4>
{#if $store.errors && $store.errors.length > 0}
<ErrorsBox errors={$store.errors} />
{/if}
<form class="uk-form-stacked"> <form class="uk-form-stacked">
<div class="horizontal-stack"> <div class="horizontal-stack">

View File

@ -23,7 +23,7 @@ export const canDeleteIndex = indexNode => {
} }
return obj return obj
},[]), },[]),
map(f => `field ${f.name} on record ${f.record.name} uses this index as a reference`) map(f => `field "${f.name}" on record "${f.record.name}" uses this index as a reference`)
]) ])
const lookupIndexes = $(flatHierarchy,[ const lookupIndexes = $(flatHierarchy,[
@ -37,7 +37,7 @@ export const canDeleteIndex = indexNode => {
} }
return obj return obj
},[]), },[]),
map(f => `field ${f.name} on record ${f.record.name} uses this index as a lookup`) map(f => `field "${f.name}" on record "${f.record.name}" uses this index as a lookup`)
]) ])
const errors = [ const errors = [

View File

@ -27,7 +27,7 @@ export const canDeleteRecord = recordNode => {
filter(i => isAncestorIndex(i) filter(i => isAncestorIndex(i)
&& belongsToAncestor(i) && belongsToAncestor(i)
&& includes(node.nodeId)(i.allowedRecordNodeIds)), && includes(node.nodeId)(i.allowedRecordNodeIds)),
map(i => `index ${i.name} indexes this record. Please remove the record from allowedRecordIds, or delete the index`) map(i => `index "${i.name}" indexes this record. Please remove the record from the index, or delete the index`)
]) ])
for (let child of node.children) { for (let child of node.children) {