don't expose last url to root VM so it isn't confused by empty path

instead, check the length of the path in a dedicated method in
the router
This commit is contained in:
Bruno Windels 2021-05-18 14:27:09 +02:00
parent dd880529ac
commit 2c7ea44afd
2 changed files with 31 additions and 18 deletions

View file

@ -35,10 +35,10 @@ export class RootViewModel extends ViewModel {
async load() { async load() {
this.track(this.navigation.observe("login").subscribe(() => this._applyNavigation())); this.track(this.navigation.observe("login").subscribe(() => this._applyNavigation()));
this.track(this.navigation.observe("session").subscribe(() => this._applyNavigation())); this.track(this.navigation.observe("session").subscribe(() => this._applyNavigation()));
this._applyNavigation(this.urlCreator.getLastUrl()); this._applyNavigation(true);
} }
async _applyNavigation(restoreUrlIfAtDefault) { async _applyNavigation(shouldRestoreLastUrl) {
const isLogin = this.navigation.observe("login").get(); const isLogin = this.navigation.observe("login").get();
const sessionId = this.navigation.observe("session").get(); const sessionId = this.navigation.observe("session").get();
if (isLogin) { if (isLogin) {
@ -67,9 +67,7 @@ export class RootViewModel extends ViewModel {
} }
} else { } else {
try { try {
if (restoreUrlIfAtDefault) { if (!(shouldRestoreLastUrl && this.urlCreator.tryRestoreLastUrl())) {
this.urlCreator.pushUrl(restoreUrlIfAtDefault);
} else {
const sessionInfos = await this.platform.sessionInfoStorage.getAll(); const sessionInfos = await this.platform.sessionInfoStorage.getAll();
if (sessionInfos.length === 0) { if (sessionInfos.length === 0) {
this.navigation.push("login"); this.navigation.push("login");

View file

@ -24,20 +24,22 @@ export class URLRouter {
this._pathSubscription = null; this._pathSubscription = null;
this._isApplyingUrl = false; this._isApplyingUrl = false;
this._defaultSessionId = this._getLastSessionId(); this._defaultSessionId = this._getLastSessionId();
} }
_getLastSessionId() { _getLastSessionId() {
const urlPath = this._history.urlAsPath(this._history.getLastUrl()); const navPath = this._urlAsNavPath(this._history.getLastUrl() || "");
const navPath = this._navigation.pathFrom(this._parseUrlPath(urlPath, this._navigation.path)); const sessionId = navPath.get("session")?.value;
return navPath.get("session")?.value; if (typeof sessionId === "string") {
return sessionId;
}
return null;
} }
attach() { attach() {
this._subscription = this._history.subscribe(url => this._applyUrl(url)); this._subscription = this._history.subscribe(url => this._applyUrl(url));
// subscribe to path before applying initial url // subscribe to path before applying initial url
// so redirects in _applyNavigationPath are reflected in url bar // so redirects in _applyNavPathToHistory are reflected in url bar
this._pathSubscription = this._navigation.pathObservable.subscribe(path => this._applyNavigationPath(path)); this._pathSubscription = this._navigation.pathObservable.subscribe(path => this._applyNavPathToHistory(path));
this._applyUrl(this._history.get()); this._applyUrl(this._history.get());
} }
@ -46,7 +48,7 @@ export class URLRouter {
this._pathSubscription = this._pathSubscription(); this._pathSubscription = this._pathSubscription();
} }
_applyNavigationPath(path) { _applyNavPathToHistory(path) {
const url = this.urlForPath(path); const url = this.urlForPath(path);
if (url !== this._history.get()) { if (url !== this._history.get()) {
if (this._isApplyingUrl) { if (this._isApplyingUrl) {
@ -58,10 +60,8 @@ export class URLRouter {
} }
} }
_applyUrl(url) { _applyNavPathToNavigation(navPath) {
const urlPath = this._history.urlAsPath(url) // this will cause _applyNavPathToHistory to be called,
const navPath = this._navigation.pathFrom(this._parseUrlPath(urlPath, this._navigation.path, this._defaultSessionId));
// this will cause _applyNavigationPath to be called,
// so set a flag whether this request came from ourselves // so set a flag whether this request came from ourselves
// (in which case it is a redirect if the url does not match the current one) // (in which case it is a redirect if the url does not match the current one)
this._isApplyingUrl = true; this._isApplyingUrl = true;
@ -69,12 +69,27 @@ export class URLRouter {
this._isApplyingUrl = false; this._isApplyingUrl = false;
} }
_urlAsNavPath(url) {
const urlPath = this._history.urlAsPath(url);
return this._navigation.pathFrom(this._parseUrlPath(urlPath, this._navigation.path, this._defaultSessionId));
}
_applyUrl(url) {
const navPath = this._urlAsNavPath(url);
this._applyNavPathToNavigation(navPath);
}
pushUrl(url) { pushUrl(url) {
this._history.pushUrl(url); this._history.pushUrl(url);
} }
getLastUrl() { tryRestoreLastUrl() {
return this._history.getLastUrl(); const lastNavPath = this._urlAsNavPath(this._history.getLastUrl() || "");
if (lastNavPath.segments.length !== 0) {
this._applyNavPathToNavigation(navPath);
return true;
}
return false;
} }
urlForSegments(segments) { urlForSegments(segments) {