Simplify, strengthen and make consistent filter drawer validation logic
This commit is contained in:
parent
143ee04d9e
commit
7909745fb5
|
@ -20,6 +20,8 @@
|
||||||
import { createEventDispatcher, onMount } from "svelte"
|
import { createEventDispatcher, onMount } from "svelte"
|
||||||
|
|
||||||
const dispatch = createEventDispatcher()
|
const dispatch = createEventDispatcher()
|
||||||
|
const { OperatorOptions } = Constants
|
||||||
|
const { getValidOperatorsForType } = LuceneUtils
|
||||||
|
|
||||||
export let schemaFields
|
export let schemaFields
|
||||||
export let filters = []
|
export let filters = []
|
||||||
|
@ -45,7 +47,7 @@
|
||||||
{
|
{
|
||||||
id: generate(),
|
id: generate(),
|
||||||
field: null,
|
field: null,
|
||||||
operator: Constants.OperatorOptions.Equals.value,
|
operator: OperatorOptions.Equals.value,
|
||||||
value: null,
|
value: null,
|
||||||
valueType: "Value",
|
valueType: "Value",
|
||||||
},
|
},
|
||||||
|
@ -66,73 +68,60 @@
|
||||||
return schemaFields.find(field => field.name === filter.field)
|
return schemaFields.find(field => field.name === filter.field)
|
||||||
}
|
}
|
||||||
|
|
||||||
const onFieldChange = (expression, field) => {
|
const validateTypes = filter => {
|
||||||
// Update the field types
|
// Update type based on field
|
||||||
expression.type = enrichedSchemaFields.find(x => x.name === field)?.type
|
const fieldSchema = enrichedSchemaFields.find(x => x.name === filter.field)
|
||||||
expression.externalType = getSchema(expression)?.externalType
|
filter.type = fieldSchema?.type
|
||||||
|
|
||||||
// Ensure a valid operator is set
|
// Update external type based on field
|
||||||
const validOperators = LuceneUtils.getValidOperatorsForType(
|
filter.externalType = getSchema(filter)?.externalType
|
||||||
expression.type
|
|
||||||
).map(x => x.value)
|
|
||||||
if (!validOperators.includes(expression.operator)) {
|
|
||||||
expression.operator =
|
|
||||||
validOperators[0] ?? Constants.OperatorOptions.Equals.value
|
|
||||||
onOperatorChange(expression, expression.operator)
|
|
||||||
}
|
|
||||||
|
|
||||||
// if changed to an array, change default value to empty array
|
|
||||||
const idx = filters.findIndex(x => x.id === expression.id)
|
|
||||||
if (expression.type === "array") {
|
|
||||||
filters[idx].value = []
|
|
||||||
} else {
|
|
||||||
filters[idx].value = null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const onOperatorChange = (expression, operator) => {
|
const validateOperator = filter => {
|
||||||
|
// Ensure a valid operator is selected
|
||||||
|
const operators = getValidOperatorsForType(filter.type).map(x => x.value)
|
||||||
|
if (!operators.includes(filter.operator)) {
|
||||||
|
filter.operator = operators[0] ?? OperatorOptions.Equals.value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the noValue flag if the operator does not take a value
|
||||||
const noValueOptions = [
|
const noValueOptions = [
|
||||||
Constants.OperatorOptions.Empty.value,
|
OperatorOptions.Empty.value,
|
||||||
Constants.OperatorOptions.NotEmpty.value,
|
OperatorOptions.NotEmpty.value,
|
||||||
]
|
]
|
||||||
expression.noValue = noValueOptions.includes(operator)
|
filter.noValue = noValueOptions.includes(filter.operator)
|
||||||
if (expression.noValue) {
|
}
|
||||||
expression.value = null
|
|
||||||
|
const validateValue = filter => {
|
||||||
|
// Check if the operator allows a value at all
|
||||||
|
if (filter.noValue) {
|
||||||
|
filter.value = null
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if (
|
|
||||||
operator === Constants.OperatorOptions.In.value &&
|
// Ensure array values are properly set and cleared
|
||||||
!Array.isArray(expression.value) &&
|
if (Array.isArray(filter.value)) {
|
||||||
expression.valueType === "Value"
|
if (filter.valueType !== "Value" || filter.type !== "array") {
|
||||||
) {
|
filter.value = null
|
||||||
if (expression.value) {
|
|
||||||
expression.value = [expression.value]
|
|
||||||
} else {
|
|
||||||
expression.value = []
|
|
||||||
}
|
}
|
||||||
} else if (
|
} else if (filter.type === "array" && filter.valueType === "Value") {
|
||||||
operator !== Constants.OperatorOptions.In.value &&
|
filter.value = []
|
||||||
Array.isArray(expression.value)
|
|
||||||
) {
|
|
||||||
expression.value = null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const onValueTypeChange = (expression, valueType) => {
|
const onFieldChange = filter => {
|
||||||
if (Array.isArray(expression.value) && valueType === "Binding") {
|
validateTypes(filter)
|
||||||
expression.value = null
|
validateOperator(filter)
|
||||||
} else if (
|
validateValue(filter)
|
||||||
expression.operator === Constants.OperatorOptions.In.value &&
|
}
|
||||||
!Array.isArray(expression.value) &&
|
|
||||||
valueType === "Value"
|
const onOperatorChange = filter => {
|
||||||
) {
|
validateOperator(filter)
|
||||||
if (typeof expression.value === "string") {
|
validateValue(filter)
|
||||||
expression.value = expression.value.split(",")
|
}
|
||||||
} else if (expression.value) {
|
|
||||||
expression.value = [expression.value]
|
const onValueTypeChange = filter => {
|
||||||
} else {
|
validateValue(filter)
|
||||||
expression.value = []
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const getFieldOptions = field => {
|
const getFieldOptions = field => {
|
||||||
|
@ -177,24 +166,24 @@
|
||||||
<Select
|
<Select
|
||||||
bind:value={filter.field}
|
bind:value={filter.field}
|
||||||
options={fieldOptions}
|
options={fieldOptions}
|
||||||
on:change={e => onFieldChange(filter, e.detail)}
|
on:change={() => onFieldChange(filter)}
|
||||||
placeholder="Column"
|
placeholder="Column"
|
||||||
/>
|
/>
|
||||||
<Select
|
<Select
|
||||||
disabled={!filter.field}
|
disabled={!filter.field}
|
||||||
options={LuceneUtils.getValidOperatorsForType(filter.type)}
|
options={getValidOperatorsForType(filter.type)}
|
||||||
bind:value={filter.operator}
|
bind:value={filter.operator}
|
||||||
on:change={e => onOperatorChange(filter, e.detail)}
|
on:change={() => onOperatorChange(filter)}
|
||||||
placeholder={null}
|
placeholder={null}
|
||||||
/>
|
/>
|
||||||
<Select
|
<Select
|
||||||
disabled={filter.noValue || !filter.field}
|
disabled={filter.noValue || !filter.field}
|
||||||
options={valueTypeOptions}
|
options={valueTypeOptions}
|
||||||
bind:value={filter.valueType}
|
bind:value={filter.valueType}
|
||||||
|
on:change={() => onValueTypeChange(filter)}
|
||||||
placeholder={null}
|
placeholder={null}
|
||||||
on:change={e => onValueTypeChange(filter, e.detail)}
|
|
||||||
/>
|
/>
|
||||||
{#if filter.valueType === "Binding"}
|
{#if filter.field && filter.valueType === "Binding"}
|
||||||
<DrawerBindableInput
|
<DrawerBindableInput
|
||||||
disabled={filter.noValue}
|
disabled={filter.noValue}
|
||||||
title={`Value for "${filter.field}"`}
|
title={`Value for "${filter.field}"`}
|
||||||
|
@ -275,7 +264,7 @@
|
||||||
column-gap: var(--spacing-l);
|
column-gap: var(--spacing-l);
|
||||||
row-gap: var(--spacing-s);
|
row-gap: var(--spacing-s);
|
||||||
align-items: center;
|
align-items: center;
|
||||||
grid-template-columns: 1fr 120px 120px 1fr auto auto;
|
grid-template-columns: 1fr 150px 120px 1fr 16px 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.filter-label {
|
.filter-label {
|
||||||
|
|
Loading…
Reference in New Issue