diff --git a/packages/builder/src/builderStore/index.js b/packages/builder/src/builderStore/index.js index f1914efab2..cf668670bb 100644 --- a/packages/builder/src/builderStore/index.js +++ b/packages/builder/src/builderStore/index.js @@ -4,7 +4,7 @@ import { getAutomationStore } from "./store/automation/" import { getThemeStore } from "./store/theme" import { derived, writable } from "svelte/store" import analytics from "analytics" -import { LAYOUT_NAMES } from "../constants" +import { FrontendTypes, LAYOUT_NAMES } from "../constants" import { makePropsSafe } from "components/userInterface/assetParsing/createProps" export const store = getFrontendStore() @@ -13,18 +13,12 @@ export const automationStore = getAutomationStore() export const themeStore = getThemeStore() export const currentAsset = derived(store, $store => { - const layout = $store.layouts - ? $store.layouts.find(layout => layout._id === $store.currentAssetId) - : null - - if (layout) return layout - - const screen = $store.screens - ? $store.screens.find(screen => screen._id === $store.currentAssetId) - : null - - if (screen) return screen - + const type = $store.currentFrontEndType + if (type === FrontendTypes.SCREEN) { + return $store.screens.find(screen => screen._id === $store.selectedScreenId) + } else if (type === FrontendTypes.LAYOUT) { + return $store.layouts.find(layout => layout._id === $store.selectedLayoutId) + } return null }) @@ -59,8 +53,14 @@ export const selectedComponent = derived( } ) -export const currentAssetName = derived(store, () => { - return currentAsset.name +export const currentAssetId = derived(store, $store => { + return $store.currentFrontEndType === FrontendTypes.SCREEN + ? $store.selectedScreenId + : $store.selectedLayoutId +}) + +export const currentAssetName = derived(currentAsset, $currentAsset => { + return $currentAsset?.name }) // leave this as before for consistency diff --git a/packages/builder/src/builderStore/store/frontend.js b/packages/builder/src/builderStore/store/frontend.js index df04bcf8f9..263041aadd 100644 --- a/packages/builder/src/builderStore/store/frontend.js +++ b/packages/builder/src/builderStore/store/frontend.js @@ -32,7 +32,8 @@ const INITIAL_FRONTEND_STATE = { screens: [], components: [], currentFrontEndType: "none", - currentAssetId: "", + selectedScreenId: "", + selectedLayoutId: "", selectedComponentId: "", errors: [], hasAppPackage: false, @@ -89,8 +90,13 @@ export const getFrontendStore = () => { let screen = screens.find(screen => screen._id === screenId) || screens[0] if (!screen) return state + + // Update role to the screen's role setting so that it will always + // be visible + selectedAccessRole.set(screen.routing.roleId) + state.currentFrontEndType = FrontendTypes.SCREEN - state.currentAssetId = screen._id + state.selectedScreenId = screen._id state.currentView = "detail" state.selectedComponentId = screen.props?._id return state @@ -99,7 +105,7 @@ export const getFrontendStore = () => { create: async screen => { screen = await store.actions.screens.save(screen) store.update(state => { - state.currentAssetId = screen._id + state.selectedScreenId = screen._id state.selectedComponentId = screen.props._id state.currentFrontEndType = FrontendTypes.SCREEN selectedAccessRole.set(screen.routing.roleId) @@ -144,8 +150,8 @@ export const getFrontendStore = () => { `/api/screens/${screenToDelete._id}/${screenToDelete._rev}` ) ) - if (screenToDelete._id === state.currentAssetId) { - state.currentAssetId = "" + if (screenToDelete._id === state.selectedScreenId) { + state.selectedScreenId = null } } return state @@ -168,11 +174,12 @@ export const getFrontendStore = () => { layouts: { select: layoutId => { store.update(state => { - const layout = store.actions.layouts.find(layoutId) + const layout = + store.actions.layouts.find(layoutId) || get(store).layouts[0] if (!layout) return state.currentFrontEndType = FrontendTypes.LAYOUT state.currentView = "detail" - state.currentAssetId = layout._id + state.selectedLayoutId = layout._id state.selectedComponentId = layout.props?._id return state }) @@ -215,16 +222,17 @@ export const getFrontendStore = () => { const response = await api.delete( `/api/layouts/${layoutToDelete._id}/${layoutToDelete._rev}` ) - if (response.status !== 200) { const json = await response.json() throw new Error(json.message) } - store.update(state => { state.layouts = state.layouts.filter( layout => layout._id !== layoutToDelete._id ) + if (layoutToDelete._id === state.selectedLayoutId) { + state.selectedLayoutId = get(mainLayout)._id + } return state }) }, diff --git a/packages/builder/src/components/userInterface/ComponentNavigationTree/ComponentTree.svelte b/packages/builder/src/components/userInterface/ComponentNavigationTree/ComponentTree.svelte index 485af84e74..320a81f00d 100644 --- a/packages/builder/src/components/userInterface/ComponentNavigationTree/ComponentTree.svelte +++ b/packages/builder/src/components/userInterface/ComponentNavigationTree/ComponentTree.svelte @@ -1,6 +1,6 @@ diff --git a/packages/builder/src/components/userInterface/FrontendNavigatePane.svelte b/packages/builder/src/components/userInterface/FrontendNavigatePane.svelte index 25f939d2f1..698476dcd8 100644 --- a/packages/builder/src/components/userInterface/FrontendNavigatePane.svelte +++ b/packages/builder/src/components/userInterface/FrontendNavigatePane.svelte @@ -3,6 +3,7 @@ import { goto, params, url } from "@sveltech/routify" import { store, + allScreens, currentAsset, backendUiStore, selectedAccessRole, @@ -29,11 +30,38 @@ let routes = {} let tab = $params.assetType - function navigate({ detail }) { - if (!detail) return + const navigate = ({ detail }) => { + if (!detail) { + return + } $goto(`../${detail.heading.key}`) } + const updateAccessRole = event => { + const role = event.target.value + + // Select a valid screen with this new role - otherwise we'll not be + // able to change role at all because ComponentNavigationTree will kick us + // back the current role again because the same screen ID is still selected + const firstValidScreenId = $allScreens.find( + screen => screen.routing.roleId === role + )?._id + if (firstValidScreenId) { + store.actions.screens.select(firstValidScreenId) + } + + // Otherwise clear the selected screen ID so that the first new valid screen + // can be selected by ComponentNavigationTree + else { + store.update(state => { + state.selectedScreenId = null + return state + }) + } + + selectedAccessRole.set(role) + } + onMount(() => { store.actions.routing.fetch() }) @@ -46,24 +74,21 @@ on:click={modal.show} data-cy="new-screen" class="ri-add-circle-fill" /> -