diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateViewModal.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateViewModal.svelte
index 8f2679f874..6d0a672bc7 100644
--- a/packages/builder/src/components/backend/DataTable/modals/CreateViewModal.svelte
+++ b/packages/builder/src/components/backend/DataTable/modals/CreateViewModal.svelte
@@ -16,7 +16,7 @@
return
}
try {
- await viewsStore.save({
+ await viewsStore.create({
name,
tableId: $tables.selected._id,
field,
diff --git a/packages/builder/src/components/backend/TableNavigator/TableNavigator.svelte b/packages/builder/src/components/backend/TableNavigator/TableNavigator.svelte
index fea80474bf..e641bd99a5 100644
--- a/packages/builder/src/components/backend/TableNavigator/TableNavigator.svelte
+++ b/packages/builder/src/components/backend/TableNavigator/TableNavigator.svelte
@@ -45,7 +45,7 @@
selected={$isActive("./view") && $views.selected?.name === viewName}
on:click={() => {
if (view.version === 2) {
- $goto(`./view/v2/${view._id}`)
+ $goto(`./view/v2/${view.id}`)
} else {
$goto(`./view/${encodeURIComponent(viewName)}`)
}
diff --git a/packages/builder/src/pages/builder/app/[application]/data/view/v2/[id]/_layout.svelte b/packages/builder/src/pages/builder/app/[application]/data/view/v2/[id]/_layout.svelte
index f296a563d9..66bcd2efe2 100644
--- a/packages/builder/src/pages/builder/app/[application]/data/view/v2/[id]/_layout.svelte
+++ b/packages/builder/src/pages/builder/app/[application]/data/view/v2/[id]/_layout.svelte
@@ -11,12 +11,12 @@
const stopSyncing = syncURLToState({
urlParam: "id",
stateKey: "selectedViewId",
- validate: id => $views.list?.some(view => view._id === id),
+ validate: id => $views.list?.some(view => view.id === id),
update: id => {
- const view = $views.list.find(v => v._id === id)
+ const view = $views.list.find(v => v.id === id)
views.select(view.name)
},
- fallbackUrl: "../",
+ fallbackUrl: "../../",
store: views,
routify,
decode: decodeURIComponent,
diff --git a/packages/builder/src/pages/builder/app/[application]/data/view/v2/[id]/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/view/v2/[id]/index.svelte
index 354e516af3..774f7181f1 100644
--- a/packages/builder/src/pages/builder/app/[application]/data/view/v2/[id]/index.svelte
+++ b/packages/builder/src/pages/builder/app/[application]/data/view/v2/[id]/index.svelte
@@ -9,7 +9,7 @@
{
+ const savedView = await API.viewV2.create(view.tableId, view)
+
+ // Update tables
+ tables.update(state => {
+ const table = state.list.find(table => table._id === view.tableId)
+ if (table) {
+ if (view.originalName) {
+ delete table.views[view.originalName]
+ }
+ table.views[view.name] = savedView
+ }
+ return { ...state }
+ })
+ }
+
const save = async view => {
const savedView = await API.saveView(view)
@@ -60,6 +76,7 @@ export function createViewsStore() {
subscribe: derivedStore.subscribe,
select,
delete: deleteView,
+ create,
save,
}
}
diff --git a/packages/frontend-core/src/api/viewsV2.js b/packages/frontend-core/src/api/viewsV2.js
index f2a886bacb..4282a2a3fb 100644
--- a/packages/frontend-core/src/api/viewsV2.js
+++ b/packages/frontend-core/src/api/viewsV2.js
@@ -1,23 +1,29 @@
export const buildViewV2Endpoints = API => ({
/**
- * Fetches all rows in a view
- * @param id the id of the view
+ * Create a new view
+ * @param tableId the id of the table where the view will be created
+ * @param view the view object
*/
- get: async id => {
- return await API.get({ url: `/api/v2/views/${id}` })
+ create: async (tableId, view) => {
+ return await API.post({
+ url: `/api/v2/views/${tableId}`,
+ body: view,
+ })
},
/**
- * Get a view information
- * @param id the id of the view
+ * Fetches all rows in a view
+ * @param tableId the id of the table
+ * @param viewId the id of the view
*/
- fetch: async id => {
- return await API.get({ url: `/api/v2/views/${id}/search` })
+ fetch: async (tableId, viewId) => {
+ return await API.get({ url: `/api/v2/views/${tableId}/${viewId}/search` })
},
/**
* Delete a view
- * @param id the id of the view to delete
+ * @param tableId the id of the table
+ * @param viewId the id of the view
*/
- delete: async id => {
- return await API.delete({ url: `/api/v2/views/${id}` })
+ delete: async (tableId, viewId) => {
+ return await API.delete({ url: `/api/v2/views/${tableId}/${viewId}` })
},
})
diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.js b/packages/frontend-core/src/fetch/ViewV2Fetch.js
index 356f94c8f0..fb801d8e2d 100644
--- a/packages/frontend-core/src/fetch/ViewV2Fetch.js
+++ b/packages/frontend-core/src/fetch/ViewV2Fetch.js
@@ -7,9 +7,8 @@ export default class ViewV2Fetch extends DataFetch {
async getDefinition(datasource) {
try {
- const viewResponse = await this.API.viewV2.get(datasource.tableId)
const result = await this.API.fetchTableDefinition(
- viewResponse.data.tableId
+ datasource.tableId.tableId
)
return result
} catch (error) {
@@ -24,7 +23,10 @@ export default class ViewV2Fetch extends DataFetch {
async getData() {
const { datasource } = this.options
try {
- const res = await this.API.viewV2.fetch(datasource.tableId)
+ const res = await this.API.viewV2.fetch(
+ datasource.tableId.tableId,
+ datasource.tableId.viewId
+ )
return { rows: res?.rows || [] }
} catch (error) {
return { rows: [] }