From e3c2d57b0ed1bc64c45c9668dd5531e516da49e2 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 14 Jul 2022 15:31:45 +0100 Subject: [PATCH] Add screen patch store method and reduce state updates when deleting screens --- .../builder/src/builderStore/store/frontend.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/builder/src/builderStore/store/frontend.js b/packages/builder/src/builderStore/store/frontend.js index 291057143d..fd862dbc80 100644 --- a/packages/builder/src/builderStore/store/frontend.js +++ b/packages/builder/src/builderStore/store/frontend.js @@ -228,6 +228,14 @@ export const getFrontendStore = () => { }) return savedScreen }, + patch: async (screenId, patch) => { + const state = get(store) + const screen = state.screens.find(screen => screen._id === screenId) + if (!screen) { + return + } + return store.actions.screens.save({ ...screen, ...patch }) + }, delete: async screens => { const screensToDelete = Array.isArray(screens) ? screens : [screens] @@ -249,20 +257,24 @@ export const getFrontendStore = () => { promises.push(store.actions.links.delete(deleteUrls)) await Promise.all(promises) const deletedIds = screensToDelete.map(screen => screen._id) + const routesResponse = await API.fetchAppRoutes() store.update(state => { // Remove deleted screens from state state.screens = state.screens.filter(screen => { return !deletedIds.includes(screen._id) }) + // Deselect the current screen if it was deleted if (deletedIds.includes(state.selectedScreenId)) { state.selectedScreenId = null + state.selectedComponentId = null } + + // Update routing + state.routes = routesResponse.routes + return state }) - - // Refresh routes - await store.actions.routing.fetch() }, updateHomeScreen: async (screen, makeHomeScreen = true) => { let promises = []