diff --git a/cypress/component/store/tablesStore.cy.js b/cypress/component/store/tablesStore.cy.js index 024e49cd72..58b2525e03 100644 --- a/cypress/component/store/tablesStore.cy.js +++ b/cypress/component/store/tablesStore.cy.js @@ -44,4 +44,29 @@ describe('tables store setters', () => { expect(store.contexts.map(context => context.id)).to.deep.equal([1, 2]) expect(hasStrayIndex(store.contexts)).to.equal(false) }) + + it('setView keeps the copy inside table.views in sync', () => { + const store = useTablesStore() + const view = { id: 7, tableId: 4, title: 'Board' } + store.setTables([{ id: 4, title: 'T', views: [view] }]) + store.setViews([view]) + + store.setView({ id: 7, tableId: 4, title: 'Board renamed' }) + + expect(store.views[0].title).to.equal('Board renamed') + expect(store.tables[0].views[0].title).to.equal('Board renamed') + }) + + it('removeView drops the view from table.views as well', () => { + cy.intercept('DELETE', '**/apps/tables/view/7', { statusCode: 200, body: true }).as('del') + const store = useTablesStore() + const view = { id: 7, tableId: 4, title: 'Board' } + store.setTables([{ id: 4, title: 'T', views: [view] }]) + store.setViews([view]) + + cy.wrap(null).then(() => store.removeView({ viewId: 7 })).then(() => { + expect(store.views).to.have.length(0) + expect(store.tables[0].views).to.have.length(0) + }) + }) }) diff --git a/src/store/store.js b/src/store/store.js index eea587f898..9fc1d47052 100644 --- a/src/store/store.js +++ b/src/store/store.js @@ -126,6 +126,12 @@ export const useTablesStore = defineStore('store', { if (index !== -1) { this.views[index] = view } + + const tableViews = this.tables.find(t => t.id === view.tableId)?.views + const tableIndex = tableViews?.findIndex(v => v.id === view.id) ?? -1 + if (tableIndex !== -1) { + tableViews[tableIndex] = view + } }, setContext(context) { const index = this.contexts.findIndex(c => c.id === context.id) @@ -262,6 +268,11 @@ export const useTablesStore = defineStore('store', { const views = this.views const index = views.findIndex(v => v.id === viewId) if (index !== -1) { + const tableViews = this.tables.find(t => t.id === views[index].tableId)?.views + const tableIndex = tableViews?.findIndex(v => v.id === viewId) ?? -1 + if (tableIndex !== -1) { + tableViews.splice(tableIndex, 1) + } views.splice(index, 1) } this.setViews([...views])