-
Notifications
You must be signed in to change notification settings - Fork 50
[WC-2896] Gallery personalisation #1730
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
Open
iobuhov
wants to merge
5
commits into
main
Choose a base branch
from
wc-2896-gallery-personalisation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+299
−31
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ee789dc
feat: add gallery persistent state controller
iobuhov a26387f
feat: add serialize methods
iobuhov a25f4d6
feat: add gallery state storage
iobuhov ece388a
fix: prevent type miscast
iobuhov 35c7ea3
chore: remove formatting
iobuhov 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 hidden or 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
51 changes: 51 additions & 0 deletions
51
packages/pluggableWidgets/gallery-web/src/stores/AttributeStorage.ts
This file contains hidden or 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,51 @@ | ||
import { PlainJs } from "@mendix/filter-commons/typings/settings"; | ||
import { DerivedPropsGate } from "@mendix/widget-plugin-mobx-kit/props-gate"; | ||
import { ReactiveController, ReactiveControllerHost } from "@mendix/widget-plugin-mobx-kit/reactive-controller"; | ||
import { EditableValue } from "mendix"; | ||
import { computed, makeObservable } from "mobx"; | ||
import { ObservableStorage } from "src/typings/storage"; | ||
|
||
type Gate = DerivedPropsGate<{ | ||
stateStorageAttr: EditableValue<string>; | ||
}>; | ||
|
||
export class AttributeStorage implements ObservableStorage, ReactiveController { | ||
private readonly _gate: Gate; | ||
|
||
constructor(host: ReactiveControllerHost, gate: Gate) { | ||
host.addController(this); | ||
|
||
this._gate = gate; | ||
makeObservable<this, "_attribute">(this, { | ||
_attribute: computed, | ||
data: computed.struct | ||
}); | ||
} | ||
|
||
setup(): () => void { | ||
return () => {}; | ||
} | ||
|
||
private get _attribute(): EditableValue<string> { | ||
return this._gate.props.stateStorageAttr; | ||
} | ||
|
||
get data(): PlainJs { | ||
const jsonString = this._attribute.value; | ||
if (!jsonString) { | ||
return null; | ||
} | ||
try { | ||
return JSON.parse(jsonString) as PlainJs; | ||
} catch { | ||
console.warn("Invalid JSON configuration in the attribute. Resetting configuration."); | ||
this._attribute.setValue(""); | ||
return null; | ||
} | ||
} | ||
|
||
setData(data: PlainJs): void { | ||
data = data === "" ? null : data; | ||
this._attribute.setValue(JSON.stringify(data)); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/pluggableWidgets/gallery-web/src/stores/BrowserStorage.ts
This file contains hidden or 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,18 @@ | ||
import { PlainJs } from "@mendix/filter-commons/typings/settings"; | ||
import { ObservableStorage } from "src/typings/storage"; | ||
|
||
export class BrowserStorage implements ObservableStorage { | ||
constructor(private readonly _storageKey: string) {} | ||
|
||
get data(): PlainJs { | ||
try { | ||
return JSON.parse(localStorage.getItem(this._storageKey) ?? "null"); | ||
} catch { | ||
return null; | ||
} | ||
} | ||
|
||
setData(data: PlainJs): void { | ||
localStorage.setItem(this._storageKey, JSON.stringify(data)); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
packages/pluggableWidgets/gallery-web/src/stores/GalleryPersistentStateController.ts
This file contains hidden or 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,91 @@ | ||
import { PlainJs, Serializable } from "@mendix/filter-commons/typings/settings"; | ||
import { disposeBatch } from "@mendix/widget-plugin-mobx-kit/disposeBatch"; | ||
import { ReactiveControllerHost } from "@mendix/widget-plugin-mobx-kit/reactive-controller"; | ||
import { action, comparer, computed, makeObservable, reaction } from "mobx"; | ||
import { ObservableStorage } from "src/typings/storage"; | ||
|
||
interface GalleryPersistentStateControllerSpec { | ||
filtersHost: Serializable; | ||
sortHost: Serializable; | ||
storage: ObservableStorage; | ||
} | ||
|
||
export class GalleryPersistentStateController { | ||
private readonly _storage: ObservableStorage; | ||
private readonly _filtersHost: Serializable; | ||
private readonly _sortHost: Serializable; | ||
|
||
readonly schemaVersion: number = 1; | ||
|
||
constructor(host: ReactiveControllerHost, spec: GalleryPersistentStateControllerSpec) { | ||
host.addController(this); | ||
this._storage = spec.storage; | ||
this._filtersHost = spec.filtersHost; | ||
this._sortHost = spec.sortHost; | ||
|
||
makeObservable<this, "_persistentState">(this, { | ||
_persistentState: computed, | ||
fromJSON: action | ||
}); | ||
} | ||
|
||
setup(): () => void { | ||
const [add, disposeAll] = disposeBatch(); | ||
|
||
// Write state to storage | ||
const clearWrite = reaction( | ||
() => this._persistentState, | ||
data => this._storage.setData(data), | ||
{ delay: 250, equals: comparer.structural } | ||
); | ||
|
||
// Update state from storage | ||
const clearRead = reaction( | ||
() => this._storage.data, | ||
data => { | ||
if (data == null) { | ||
return; | ||
} | ||
if (this._validate(data)) { | ||
this.fromJSON(data); | ||
} else { | ||
console.warn("Invalid gallery settings. Reset storage to avoid conflicts."); | ||
this._storage.setData(null); | ||
} | ||
}, | ||
{ fireImmediately: true, equals: comparer.structural } | ||
); | ||
|
||
add(clearWrite); | ||
add(clearRead); | ||
|
||
return disposeAll; | ||
} | ||
|
||
private get _persistentState(): PlainJs { | ||
return this.toJSON(); | ||
} | ||
|
||
private _validate(data: PlainJs): data is { [key: string]: PlainJs } { | ||
if (data == null || typeof data !== "object" || !("version" in data) || data.version !== this.schemaVersion) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
fromJSON(data: PlainJs): void { | ||
if (!this._validate(data)) { | ||
return; | ||
} | ||
this._filtersHost.fromJSON(data.filters); | ||
this._sortHost.fromJSON(data.sort); | ||
} | ||
|
||
toJSON(): PlainJs { | ||
return { | ||
version: 1, | ||
filters: this._filtersHost.toJSON(), | ||
sort: this._sortHost.toJSON() | ||
}; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,6 @@ | ||
import { PlainJs } from "@mendix/filter-commons/typings/settings"; | ||
|
||
export interface ObservableStorage { | ||
data: PlainJs; | ||
setData(data: PlainJs): void; | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.