Add ability to update views, create views with existing filters and sorting applied

This commit is contained in:
Andrew Kingston 2023-07-30 12:49:07 +01:00
parent 1aea6fce09
commit 8204935dfa
5 changed files with 30 additions and 9 deletions

View File

@ -1,11 +1,17 @@
<script> <script>
import { getContext } from "svelte"
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 { tables } from "stores/backend" import { tables } from "stores/backend"
import { LuceneUtils } from "@budibase/frontend-core"
const { filter, sort, table } = getContext("grid")
$: query = LuceneUtils.buildLuceneQuery($filter)
$: console.log($table.schema)
let name let name
let field
$: views = Object.keys($tables.selected?.views || {}) $: views = Object.keys($tables.selected?.views || {})
$: nameExists = views.includes(name?.trim()) $: nameExists = views.includes(name?.trim())
@ -16,7 +22,12 @@
const newView = await viewsV2.create({ const newView = await viewsV2.create({
name, name,
tableId: $tables.selected._id, tableId: $tables.selected._id,
field, query,
sort: {
field: $sort.column,
order: $sort.order,
},
columns: $table.schema,
}) })
notifications.success(`View ${name} created`) notifications.success(`View ${name} created`)
$goto(`../../view/v2/${newView.id}`) $goto(`../../view/v2/${newView.id}`)

View File

@ -14,7 +14,10 @@ export const createGridWebsocket = context => {
const appId = API.getAppID() const appId = API.getAppID()
socket.emit( socket.emit(
GridSocketEvent.SelectDatasource, GridSocketEvent.SelectDatasource,
{ datasource, appId }, {
datasource,
appId,
},
({ users: gridUsers }) => { ({ users: gridUsers }) => {
users.set(gridUsers) users.set(gridUsers)
} }

View File

@ -69,7 +69,8 @@ export const deriveStores = context => {
} }
export const createActions = context => { export const createActions = context => {
const { table, columns, stickyColumn, API, dispatch, config } = context const { table, columns, stickyColumn, API, dispatch, config, datasource } =
context
// Checks if we have a certain column by name // Checks if we have a certain column by name
const hasColumn = column => { const hasColumn = column => {
@ -137,12 +138,19 @@ export const createActions = context => {
} }
const saveTable = async newTable => { const saveTable = async newTable => {
const $config = get(config)
const $datasource = get(datasource)
// Update local state // Update local state
table.set(newTable) table.set(newTable)
// Update server // Update server
if (get(config).canSaveSchema) { if ($config.canSaveSchema) {
await API.saveTable(newTable) if ($datasource.type === "table") {
await API.saveTable(newTable)
} else if ($datasource.type === "viewV2") {
await API.viewV2.update({ ...newTable })
}
} }
// Broadcast change to external state can be updated, as this change // Broadcast change to external state can be updated, as this change

View File

@ -8,11 +8,9 @@ export default class ViewV2Fetch extends DataFetch {
async getDefinition(datasource) { async getDefinition(datasource) {
try { try {
const table = await this.API.fetchTableDefinition(datasource.tableId) const table = await this.API.fetchTableDefinition(datasource.tableId)
const view = Object.values(table.views || {}).find( return Object.values(table.views || {}).find(
view => view.id === datasource.id view => view.id === datasource.id
) )
const { schema } = view
return { schema }
} catch (error) { } catch (error) {
this.store.update(state => ({ this.store.update(state => ({
...state, ...state,

View File

@ -45,6 +45,7 @@ export async function update(tableId: string, view: ViewV2): Promise<ViewV2> {
throw new HTTPError(`View ${view.id} not found in table ${tableId}`, 404) throw new HTTPError(`View ${view.id} not found in table ${tableId}`, 404)
} }
console.log("set to", view)
delete table.views[existingView.name] delete table.views[existingView.name]
table.views[view.name] = view table.views[view.name] = view
await db.put(table) await db.put(table)