Enable dynamic filter component to update data provider queries

This commit is contained in:
Andrew Kingston 2021-11-18 20:32:00 +00:00
parent b2a58d1c35
commit 0aa0f6a07d
1 changed files with 46 additions and 14 deletions

View File

@ -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}
</Button>
<Modal bind:this={modal}> <Modal bind:this={modal}>
<ModalContent title={text} size="XL" onConfirm={updateQuery}> <ModalContent title={text} size="XL" onConfirm={updateQuery}>
<FilterModal bind:filters={tmpFilters} {schemaFields} /> <FilterModal bind:filters={tmpFilters} {schemaFields} />
</ModalContent> </ModalContent>
</Modal> </Modal>
</div>