Hydrate view filters and allow filter changes to be saved to views

This commit is contained in:
Andrew Kingston 2023-08-08 09:29:05 +01:00
parent af2fac6958
commit a72c94426f
5 changed files with 52 additions and 29 deletions

View File

@ -26,7 +26,9 @@
showAvatars={false} showAvatars={false}
on:updatedatasource={handleGridViewUpdate} on:updatedatasource={handleGridViewUpdate}
> >
<svelte:fragment slot="filter" /> <svelte:fragment slot="filter">
<GridFilterButton />
</svelte:fragment>
<svelte:fragment slot="controls"> <svelte:fragment slot="controls">
<GridCreateEditRowModal /> <GridCreateEditRowModal />
</svelte:fragment> </svelte:fragment>

View File

@ -4,9 +4,6 @@
const { columns, datasource, filter, definition } = getContext("grid") const { columns, datasource, filter, definition } = getContext("grid")
// Wipe filter whenever table ID changes to avoid using stale filters
$: $datasource, filter.set([])
const onFilter = e => { const onFilter = e => {
filter.set(e.detail || []) filter.set(e.detail || [])
} }

View File

@ -3,12 +3,9 @@
import { Input, notifications, ModalContent } from "@budibase/bbui" import { Input, notifications, ModalContent } from "@budibase/bbui"
import { goto } from "@roxi/routify" import { goto } from "@roxi/routify"
import { viewsV2 } from "stores/backend" import { viewsV2 } from "stores/backend"
import { LuceneUtils } from "@budibase/frontend-core"
const { filter, sort, definition } = getContext("grid") const { filter, sort, definition } = getContext("grid")
$: query = LuceneUtils.buildLuceneQuery($filter)
let name let name
$: views = Object.keys($definition?.views || {}) $: views = Object.keys($definition?.views || {})
@ -20,7 +17,7 @@
const newView = await viewsV2.create({ const newView = await viewsV2.create({
name, name,
tableId: $definition._id, tableId: $definition._id,
query, query: $filter,
sort: { sort: {
field: $sort.column, field: $sort.column,
order: $sort.order, order: $sort.order,

View File

@ -61,38 +61,49 @@ export const createActions = context => {
export const initialise = context => { export const initialise = context => {
const { datasource, fetch, filter, sort, definition } = context const { datasource, fetch, filter, sort, definition } = context
// Wipe filter whenever table ID changes to avoid using stale filters
definition.subscribe(() => {
if (get(datasource)?.type !== "table") {
return
}
filter.set([])
})
// Update fetch when filter changes // Update fetch when filter changes
filter.subscribe($filter => { filter.subscribe($filter => {
if (get(datasource)?.type === "table") { if (get(datasource)?.type !== "table") {
get(fetch)?.update({ return
filter: $filter,
})
} }
get(fetch)?.update({
filter: $filter,
})
}) })
// Update fetch when sorting changes // Update fetch when sorting changes
sort.subscribe($sort => { sort.subscribe($sort => {
if (get(datasource)?.type === "table") { if (get(datasource)?.type !== "table") {
get(fetch)?.update({ return
sortOrder: $sort.order,
sortColumn: $sort.column,
})
} }
get(fetch)?.update({
sortOrder: $sort.order,
sortColumn: $sort.column,
})
}) })
// Ensure sorting UI reflects the fetch state whenever we reset the fetch, // Ensure sorting UI reflects the fetch state whenever we reset the fetch,
// which triggers a new definition // which triggers a new definition
definition.subscribe(() => { definition.subscribe(() => {
if (get(datasource)?.type === "table") { if (get(datasource)?.type !== "table") {
const $fetch = get(fetch) return
if (!$fetch) {
return
}
const { sortColumn, sortOrder } = get($fetch)
sort.set({
column: sortColumn,
order: sortOrder,
})
} }
const $fetch = get(fetch)
if (!$fetch) {
return
}
const { sortColumn, sortOrder } = get($fetch)
sort.set({
column: sortColumn,
order: sortOrder,
})
}) })
} }

View File

@ -82,9 +82,9 @@ export const createActions = context => {
} }
export const initialise = context => { export const initialise = context => {
const { definition, datasource, sort, rows } = context const { definition, datasource, sort, rows, filter } = context
// For views, keep sort state in line with the view definition // Keep sort and filter state in line with the view definition
definition.subscribe($definition => { definition.subscribe($definition => {
if (!$definition || get(datasource)?.type !== "viewV2") { if (!$definition || get(datasource)?.type !== "viewV2") {
return return
@ -93,6 +93,7 @@ export const initialise = context => {
column: $definition.sort?.field, column: $definition.sort?.field,
order: $definition.sort?.order, order: $definition.sort?.order,
}) })
filter.set($definition.query || [])
}) })
// When sorting changes, ensure view definition is kept up to date // When sorting changes, ensure view definition is kept up to date
@ -115,4 +116,19 @@ export const initialise = context => {
await rows.actions.refreshData() await rows.actions.refreshData()
} }
}) })
// When filters change, ensure view definition is kept up to date
filter.subscribe(async $filter => {
const $view = get(definition)
if (!$view || get(datasource)?.type !== "viewV2") {
return
}
if (JSON.stringify($filter) !== JSON.stringify($view.query)) {
await datasource.actions.saveDefinition({
...$view,
query: $filter,
})
await rows.actions.refreshData()
}
})
} }