forked from finos/vuu
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Manage layout persistence via interface #55
Merged
pling-scottlogic
merged 2 commits into
main
from
feature/layout-management/VUU20-layoutPersistence-new
Sep 22, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
vuu-ui/packages/vuu-layout/src/layout-persistence/LayoutPersistenceManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { LayoutJSON } from "@finos/vuu-layout"; | ||
import { LayoutMetadata } from "@finos/vuu-shell"; | ||
|
||
export interface LayoutPersistenceManager { | ||
/** | ||
* Saves a new layout | ||
* | ||
* @param metadata - Metadata about the layout to be saved | ||
* @param layout - Full JSON representation of the layout to be saved | ||
* | ||
* @returns ID assigned to the saved layout | ||
*/ | ||
createLayout: (metadata: Omit<LayoutMetadata, "id">, layout: LayoutJSON) => string; | ||
|
||
/** | ||
* Overwrites an existing layout with a new one | ||
* | ||
* @param id - Unique identifier of the existing layout to be updated | ||
* @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) => void; | ||
|
||
/** | ||
* Deletes an existing layout | ||
* | ||
* @param id - Unique identifier of the existing layout to be deleted | ||
*/ | ||
deleteLayout: (id: string) => void; | ||
|
||
/** | ||
* Retrieves an existing layout | ||
* | ||
* @param id - Unique identifier of the existing layout to be retrieved | ||
* | ||
* @returns the layout corresponding to provided metadata | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we say "a json representation of the layout" rather than just "layout" for consistency with the other descriptions? |
||
*/ | ||
loadLayout: (id: string) => LayoutJSON; | ||
|
||
/** | ||
* Retrieves metadata for all existing layouts | ||
* | ||
* @returns an array of all persisted layout metadata | ||
*/ | ||
loadMetadata: () => LayoutMetadata[]; | ||
} |
79 changes: 79 additions & 0 deletions
79
vuu-ui/packages/vuu-layout/src/layout-persistence/LocalLayoutPersistenceManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Layout, LayoutMetadata } from "@finos/vuu-shell"; | ||
import { LayoutJSON, LayoutPersistenceManager } from "@finos/vuu-layout"; | ||
|
||
import { getLocalEntity, saveLocalEntity } from "@finos/vuu-filters"; | ||
import { getUniqueId } from "@finos/vuu-utils"; | ||
|
||
const metadataSaveLocation = "layouts/metadata"; | ||
const layoutsSaveLocation = "layouts/layouts"; | ||
|
||
export class LocalLayoutPersistenceManager implements LayoutPersistenceManager { | ||
createLayout(metadata: Omit<LayoutMetadata, "id">, layout: LayoutJSON): string { | ||
console.log(`Saving layout as ${metadata.name} to group ${metadata.group}...`); | ||
|
||
const existingLayouts = this.loadLayouts(); | ||
const existingMetadata = this.loadMetadata(); | ||
|
||
const id = getUniqueId(); | ||
|
||
this.appendAndPersist(id, metadata, layout, existingLayouts, existingMetadata); | ||
|
||
return id; | ||
} | ||
|
||
updateLayout(id: string, metadata: Omit<LayoutMetadata, "id">, newLayoutJson: LayoutJSON): void { | ||
const existingLayouts = this.loadLayouts().filter(layout => layout.id !== id); | ||
const existingMetadata = this.loadMetadata().filter(metadata => metadata.id !== id); | ||
|
||
this.appendAndPersist(id, metadata, newLayoutJson, existingLayouts, existingMetadata); | ||
} | ||
|
||
deleteLayout(id: string): void { | ||
const layouts = this.loadLayouts().filter(layout => layout.id !== id); | ||
const metadata = this.loadMetadata().filter(metadata => metadata.id !== id); | ||
|
||
this.saveLayoutsWithMetadata(layouts, metadata); | ||
} | ||
|
||
loadLayout(id: string): LayoutJSON { | ||
const layout = this.loadLayouts().filter(layout => layout.id === id); | ||
|
||
switch (layout.length) { | ||
case 1: { | ||
return layout[0].json; | ||
} | ||
case 0: { | ||
console.log(`WARNING: no layout exists for ID "${id}"; returning empty layout`); | ||
return {} as LayoutJSON; | ||
} | ||
default: { | ||
console.log(`WARNING: multiple layouts exist for ID "${id}"; returning first instance`) | ||
return layout[0].json; | ||
} | ||
} | ||
} | ||
|
||
loadMetadata(): LayoutMetadata[] { | ||
return getLocalEntity<LayoutMetadata[]>(metadataSaveLocation) || []; | ||
} | ||
|
||
private loadLayouts(): Layout[] { | ||
return getLocalEntity<Layout[]>(layoutsSaveLocation) || []; | ||
} | ||
|
||
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[]): void { | ||
saveLocalEntity<Layout[]>(layoutsSaveLocation, layouts); | ||
saveLocalEntity<LayoutMetadata[]>(metadataSaveLocation, metadata); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './LayoutPersistenceManager'; | ||
export * from './LocalLayoutPersistenceManager'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 28 additions & 29 deletions
57
vuu-ui/packages/vuu-shell/src/layout-management/useLayoutManager.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on update I think we can just say metadata: LayoutMetadata rather than Omit<...> as we already have a fully formed metadata object by this stage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still prefer it this way, because it makes it clear that they're being used for different purposes: the ID is existing information used for retrieval, whereas the metadata is new information to be saved. It also makes the documentation a lot clearer, avoiding the need for a lengthy explanation against
@param metadata
.