Enable dynamic filter component to update data provider queries
This commit is contained in:
parent
327ee5726d
commit
a1b2f9d57d
|
@ -1,22 +1,50 @@
|
||||||
<script>
|
<script>
|
||||||
import { Button, ModalContent, Modal } from "@budibase/bbui"
|
import { get } from "svelte/store"
|
||||||
import { getContext } from "svelte"
|
import { getContext, onDestroy } from "svelte"
|
||||||
|
import { ModalContent, Modal } from "@budibase/bbui"
|
||||||
import FilterModal from "./FilterModal.svelte"
|
import FilterModal from "./FilterModal.svelte"
|
||||||
|
import { buildLuceneQuery } from "builder/src/helpers/lucene"
|
||||||
|
import Button from "../Button.svelte"
|
||||||
|
|
||||||
export let dataProvider
|
export let dataProvider
|
||||||
export let allowedFields
|
export let allowedFields
|
||||||
export let buttonText
|
export let buttonText
|
||||||
|
|
||||||
const component = getContext("component")
|
const component = getContext("component")
|
||||||
const { styleable } = getContext("sdk")
|
const { builderStore, ActionTypes, getAction } = getContext("sdk")
|
||||||
|
|
||||||
let modal
|
let modal
|
||||||
let tmpFilters = []
|
let tmpFilters = []
|
||||||
let filters = []
|
let filters = []
|
||||||
|
|
||||||
|
$: dataProviderId = dataProvider?.id
|
||||||
|
$: addExtension = getAction(
|
||||||
|
dataProviderId,
|
||||||
|
ActionTypes.AddDataProviderQueryExtension
|
||||||
|
)
|
||||||
|
$: removeExtension = getAction(
|
||||||
|
dataProviderId,
|
||||||
|
ActionTypes.RemoveDataProviderQueryExtension
|
||||||
|
)
|
||||||
$: schema = dataProvider?.schema
|
$: schema = dataProvider?.schema
|
||||||
$: schemaFields = getSchemaFields(schema, allowedFields)
|
$: schemaFields = getSchemaFields(schema, allowedFields)
|
||||||
$: text = buttonText || "Edit filters"
|
$: text = getText(buttonText, filters)
|
||||||
|
|
||||||
|
// Add query extension to data provider
|
||||||
|
$: {
|
||||||
|
if (filters?.length) {
|
||||||
|
const queryExtension = buildLuceneQuery(filters)
|
||||||
|
addExtension?.($component.id, queryExtension)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const getText = (buttonText, filters) => {
|
||||||
|
let text = buttonText || "Edit filters"
|
||||||
|
if (filters?.length) {
|
||||||
|
text += ` (${filters.length})`
|
||||||
|
}
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
const getSchemaFields = (schema, allowedFields) => {
|
const getSchemaFields = (schema, allowedFields) => {
|
||||||
let clonedSchema = {}
|
let clonedSchema = {}
|
||||||
|
@ -31,6 +59,9 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
const openEditor = () => {
|
const openEditor = () => {
|
||||||
|
if (get(builderStore).inBuilder) {
|
||||||
|
return
|
||||||
|
}
|
||||||
tmpFilters = [...filters]
|
tmpFilters = [...filters]
|
||||||
modal.show()
|
modal.show()
|
||||||
}
|
}
|
||||||
|
@ -38,15 +69,16 @@
|
||||||
const updateQuery = () => {
|
const updateQuery = () => {
|
||||||
filters = [...tmpFilters]
|
filters = [...tmpFilters]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onDestroy(() => {
|
||||||
|
removeExtension?.($component.id)
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div use:styleable={$component.styles}>
|
<Button onClick={openEditor} {text} />
|
||||||
<Button on:click={openEditor} secondary>
|
|
||||||
{text}
|
<Modal bind:this={modal}>
|
||||||
</Button>
|
<ModalContent title={text} size="XL" onConfirm={updateQuery}>
|
||||||
<Modal bind:this={modal}>
|
<FilterModal bind:filters={tmpFilters} {schemaFields} />
|
||||||
<ModalContent title={text} size="XL" onConfirm={updateQuery}>
|
</ModalContent>
|
||||||
<FilterModal bind:filters={tmpFilters} {schemaFields} />
|
</Modal>
|
||||||
</ModalContent>
|
|
||||||
</Modal>
|
|
||||||
</div>
|
|
||||||
|
|
Loading…
Reference in New Issue