Skip to content

Commit

Permalink
VUU80: Merge persistence manager generator approaches
Browse files Browse the repository at this point in the history
  • Loading branch information
cfisher-scottlogic committed Nov 13, 2023
1 parent b58f9a9 commit cfcb996
Showing 1 changed file with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ import React, {
useState,
} from "react";
import {
defaultLayout,
LayoutJSON,
LayoutPersistenceManager,
LocalLayoutPersistenceManager,
RemoteLayoutPersistenceManager,
resolveJSONPath,
} from "@finos/vuu-layout";
import {
LayoutMetadata,
LayoutMetadataDto
} from "./layoutTypes";
import { defaultLayout } from "@finos/vuu-layout/";
import { LayoutMetadata, LayoutMetadataDto } from "./layoutTypes";

const local = process.env.LOCAL ?? true;

const persistenceManager = local
? new LocalLayoutPersistenceManager()
: new RemoteLayoutPersistenceManager();
let _persistenceManager: LayoutPersistenceManager;

const getPersistenceManager = () => {
if (_persistenceManager !== undefined) return _persistenceManager;

_persistenceManager = local
? new LocalLayoutPersistenceManager()
: new RemoteLayoutPersistenceManager();

return _persistenceManager;
};

export const LayoutManagementContext = React.createContext<{
layoutMetadata: LayoutMetadata[];
Expand Down Expand Up @@ -61,7 +67,8 @@ export const LayoutManagementProvider = (
);

useEffect(() => {
persistenceManager.loadMetadata()
getPersistenceManager()
.loadMetadata()
.then((metadata) => {
setLayoutMetadata(metadata);
})
Expand All @@ -70,20 +77,24 @@ export const LayoutManagementProvider = (
console.error("Error occurred while retrieving metadata", error);
});

persistenceManager.loadApplicationLayout()
getPersistenceManager()
.loadApplicationLayout()
.then((layout: LayoutJSON) => {
setApplicationLayout(layout);
})
.catch((error: Error) => {
//TODO: Show error toaster
console.error("Error occurred while retrieving application layout", error);
console.error(
"Error occurred while retrieving application layout",
error
);
});
}, [setApplicationLayout]);

const saveApplicationLayout = useCallback(
(layout: LayoutJSON) => {
setApplicationLayout(layout, false);
persistenceManager.saveApplicationLayout(layout);
getPersistenceManager().saveApplicationLayout(layout);
},
[setApplicationLayout]
);
Expand All @@ -95,7 +106,7 @@ export const LayoutManagementProvider = (
);

if (layoutToSave) {
persistenceManager
getPersistenceManager()
.createLayout(metadata, layoutToSave)
.then((metadata) => {
//TODO: Show success toast
Expand All @@ -111,14 +122,16 @@ export const LayoutManagementProvider = (

const loadLayoutById = useCallback(
(id: string) => {
persistenceManager.loadLayout(id).then((layoutJson) => {
const { current: prev } = applicationLayoutRef;
setApplicationLayout({
...prev,
active: prev.children?.length ?? 0,
children: [...(prev.children || []), layoutJson],
getPersistenceManager()
.loadLayout(id)
.then((layoutJson) => {
const { current: prev } = applicationLayoutRef;
setApplicationLayout({
...prev,
active: prev.children?.length ?? 0,
children: [...(prev.children || []), layoutJson],
});
});
});
},
[setApplicationLayout]
);
Expand Down

0 comments on commit cfcb996

Please sign in to comment.