Skip to content

Commit

Permalink
VUU-27 refactor to not use appendAndPersist
Browse files Browse the repository at this point in the history
  • Loading branch information
vferraro-scottlogic committed Oct 24, 2023
1 parent 2c548ad commit cb49a88
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface LayoutPersistenceManager {
* @param metadata - Metadata describing the new layout to overwrite with
* @param layout - Full JSON representation of the new layout to overwrite with
*/
updateLayout: (id: string, metadata: Omit<LayoutMetadata, "id">, layout: LayoutJSON) => Promise<void>;
updateLayout: (id: string, metadata: LayoutMetadataDto, layout: LayoutJSON) => Promise<void>;

/**
* Deletes an existing layout and its corresponding metadata
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Layout, LayoutMetadata, LayoutMetadataDto, WithId } from "@finos/vuu-shell";
import {
Layout,
LayoutMetadata,
LayoutMetadataDto,
WithId,
} from "@finos/vuu-shell";
import { getLocalEntity, saveLocalEntity } from "@finos/vuu-filters";
import { formatDate, getUniqueId } from "@finos/vuu-utils";

Expand Down Expand Up @@ -27,12 +32,10 @@ export class LocalLayoutPersistenceManager implements LayoutPersistenceManager {
id,
created: formatDate(new Date(), "dd.mm.yyyy"),
};
this.appendAndPersist(
id,
newMetadata,
layout,
existingLayouts,
existingMetadata

this.saveLayoutsWithMetadata(
[...existingLayouts, { id, json: layout }],
[...existingMetadata, newMetadata]
);
resolve(newMetadata);
}
Expand All @@ -42,18 +45,21 @@ export class LocalLayoutPersistenceManager implements LayoutPersistenceManager {

updateLayout(
id: string,
newMetadata: Omit<LayoutMetadata, "id">,
newMetadata: LayoutMetadataDto,
newLayout: LayoutJSON
): Promise<void> {
return new Promise((resolve, reject) => {
this.validateIds(id)
.then(() => Promise.all([this.loadLayouts(), this.loadMetadata()]))
.then(([existingLayouts, existingMetadata]) => {
const layouts = existingLayouts.filter((layout) => layout.id !== id);
const metadata = existingMetadata.filter(
(metadata) => metadata.id !== id
this.saveLayoutsWithMetadata(
existingLayouts.map((layout) =>
layout.id === id ? { ...layout, json: newLayout } : layout
),
existingMetadata.map((metadata) =>
metadata.id === id ? { ...metadata, ...newMetadata } : metadata
)
);
this.appendAndPersist(id, newMetadata, newLayout, layouts, metadata);
resolve();
})
.catch((e) => reject(e));
Expand Down Expand Up @@ -130,19 +136,6 @@ export class LocalLayoutPersistenceManager implements LayoutPersistenceManager {
});
}

private appendAndPersist(
newId: string,
newMetadata: Omit<LayoutMetadata, "id">,
newLayout: LayoutJSON,
existingLayouts: Layout[],
existingMetadata: LayoutMetadata[]
) {
existingLayouts.push({ id: newId, json: newLayout });
existingMetadata.push({ id: newId, ...newMetadata });

this.saveLayoutsWithMetadata(existingLayouts, existingMetadata);
}

private saveLayoutsWithMetadata(
layouts: Layout[],
metadata: LayoutMetadata[]
Expand Down

0 comments on commit cb49a88

Please sign in to comment.