Skip to content

Commit

Permalink
SLVUU-117 add type guard for layoutJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
vferraro-scottlogic committed Dec 14, 2023
1 parent b57cee5 commit f9c7477
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
4 changes: 3 additions & 1 deletion vuu-ui/packages/vuu-layout/src/utils/typeOf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReactElement } from 'react';
import { LayoutModel, WithType } from '../layout-reducer';
import { LayoutJSON, LayoutModel, WithType } from '../layout-reducer';

export function typeOf(element?: LayoutModel | WithType): string | undefined {
if (element) {
Expand All @@ -23,3 +23,5 @@ export function typeOf(element?: LayoutModel | WithType): string | undefined {
}

export const isTypeOf = (element: ReactElement, type: string) => typeOf(element) === type;

export const isLayoutJSON = (layout: LayoutJSON): layout is LayoutJSON=> "type" in layout;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
RemoteLayoutPersistenceManager,
resolveJSONPath,
defaultApplicationJson,
isLayoutJSON,
} from "@finos/vuu-layout";
import { NotificationLevel, useNotifications } from "@finos/vuu-popups";
import { LayoutMetadata, LayoutMetadataDto } from "./layoutTypes";
Expand Down Expand Up @@ -154,8 +155,12 @@ export const LayoutManagementProvider = (

const saveApplicationLayout = useCallback(
(layout: LayoutJSON) => {
setApplicationLayout(layout, false);
getPersistenceManager().saveApplicationJSON(applicationJSONRef.current);
if (isLayoutJSON(layout)) {
setApplicationLayout(layout, false);
getPersistenceManager().saveApplicationJSON(applicationJSONRef.current);
} else {
console.error("Tried to save invalid application layout", layout);
}
},
[setApplicationLayout]
);
Expand All @@ -167,7 +172,7 @@ export const LayoutManagementProvider = (
"#main-tabs.ACTIVE_CHILD"
);

if (layoutToSave) {
if (layoutToSave && isLayoutJSON(layoutToSave)) {
getPersistenceManager()
.createLayout(metadata, ensureLayoutHasTitle(layoutToSave, metadata))
.then((metadata) => {
Expand All @@ -187,10 +192,11 @@ export const LayoutManagementProvider = (
console.error("Error occurred while saving layout", error);
});
} else {
console.error("Tried to save invalid layout", layoutToSave);
notify({
type: NotificationLevel.Error,
header: "Failed to Save Layout",
body: "Cannot save undefined layout",
body: "Cannot save invalid layout",
});
}
},
Expand Down

0 comments on commit f9c7477

Please sign in to comment.