diff --git a/vuu-ui/packages/vuu-layout/src/layout-persistence/LocalLayoutPersistenceManager.ts b/vuu-ui/packages/vuu-layout/src/layout-persistence/LocalLayoutPersistenceManager.ts index 08620e766..e687af696 100644 --- a/vuu-ui/packages/vuu-layout/src/layout-persistence/LocalLayoutPersistenceManager.ts +++ b/vuu-ui/packages/vuu-layout/src/layout-persistence/LocalLayoutPersistenceManager.ts @@ -5,12 +5,26 @@ import { getLocalEntity, saveLocalEntity } from "@finos/vuu-filters"; import { getUniqueId } from "@finos/vuu-utils"; export class LocalLayoutPersistenceManager implements LayoutPersistenceManager { - saveLayout(layouts: Layout[]): string { - console.log(`Saving layout as ${layouts[0]?.metadata.name} to group ${layouts[0]?.metadata.group}...`); + saveLayout(inputLayouts: Layout[]): string { + const layouts = Array.from(inputLayouts); + + if (layouts.length === 0) { + return ""; + } + + const layout = this.deepCopy(layouts[layouts.length - 1]); + console.log(`Saving layout as ${layout.metadata.name} to group ${layout.metadata.group}...`); + const id = getUniqueId(); + layout.metadata.id = id; + layouts[layouts.length - 1] = layout; saveLocalEntity("layouts", layouts); - return layouts[0]?.metadata.id; + return id; } + private deepCopy(object: T): T { + return JSON.parse(JSON.stringify(object)); + }; + updateLayout(id: string, newMetadata: Omit, newLayoutJson: LayoutJSON): void { const layouts = this.getExistingLayouts(); const layoutJson = getLocalEntity("api/vui"); diff --git a/vuu-ui/packages/vuu-shell/src/layout-management/useLayoutManager.tsx b/vuu-ui/packages/vuu-shell/src/layout-management/useLayoutManager.tsx index 0e3bea13c..f8f42a690 100644 --- a/vuu-ui/packages/vuu-shell/src/layout-management/useLayoutManager.tsx +++ b/vuu-ui/packages/vuu-shell/src/layout-management/useLayoutManager.tsx @@ -1,7 +1,6 @@ import React, { useState, useCallback, useContext, useEffect } from "react"; import { getLocalEntity } from "@finos/vuu-filters"; import { LayoutJSON, LayoutPersistenceManager } from "@finos/vuu-layout"; -import { getUniqueId } from "@finos/vuu-utils"; import { LayoutMetadata, Layout } from "./layoutTypes"; export const LayoutManagementContext = React.createContext<{ @@ -14,31 +13,29 @@ export const LayoutManagementProvider = (props: { children: JSX.Element | JSX.Element[] }) => { const [layouts, setLayouts] = useState([]); + const [tempLayouts, setTempLayouts] = useState([]); useEffect(() => { - const layouts = props.persistenceManager.loadLayouts(); - setLayouts(layouts || []) + const loadedLayouts = props.persistenceManager.loadLayouts(); + setLayouts(loadedLayouts || []) }, []) useEffect(() => { - props.persistenceManager.saveLayout(layouts); - }, [layouts]) + if (tempLayouts.length !== 0) { + // Persist layouts + const generatedId = props.persistenceManager.saveLayout([...layouts, ...tempLayouts]); + + // Update state + const newLayout = tempLayouts[0]; + newLayout.metadata.id = generatedId; + setLayouts(prev => [...prev, newLayout]); + } + }, [tempLayouts]) const saveLayout = useCallback((metadata: Omit) => { const json = getLocalEntity("api/vui"); if (json) { - setLayouts(prev => - [ - ...prev, - { - metadata: { - ...metadata, - id: getUniqueId() - }, - json - } - ] - ) + setTempLayouts([...layouts, { metadata: { ...metadata, id: "" }, json }]) } }, [])