Skip to content

Commit 5c7a25f

Browse files
Closing redirecting modals fix (#19420)
* correct for fewer rejected promises * move set new is new * enable router slot to back out of a redirect * hacky fix for redirect controller * Update src/Umbraco.Web.UI.Client/src/packages/core/workspace/controllers/workspace-is-new-redirect.controller.ts Co-authored-by: Copilot <[email protected]> * Update src/Umbraco.Web.UI.Client/src/packages/core/workspace/controllers/workspace-is-new-redirect.controller.ts Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 290334b commit 5c7a25f

File tree

7 files changed

+72
-15
lines changed

7 files changed

+72
-15
lines changed

src/Umbraco.Web.UI.Client/src/packages/content/content/workspace/content-detail-workspace-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ export abstract class UmbContentDetailWorkspaceContextBase<
918918
variantIdsIncludingInvariant,
919919
);
920920
this._data.setCurrent(newCurrentData);
921+
this.setIsNew(false);
921922

922923
const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
923924
if (!eventContext) {
@@ -928,7 +929,6 @@ export abstract class UmbContentDetailWorkspaceContextBase<
928929
unique: parent.unique,
929930
});
930931
eventContext.dispatchEvent(event);
931-
this.setIsNew(false);
932932
}
933933

934934
async #update(variantIds: Array<UmbVariantId>, saveData: DetailModelType) {

src/Umbraco.Web.UI.Client/src/packages/core/repository/item/item-repository-base.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ export class UmbItemRepositoryBase<ItemType extends { unique: string }>
3636
*/
3737
async requestItems(uniques: Array<string>) {
3838
if (!uniques) throw new Error('Uniques are missing');
39-
await this._init;
39+
try {
40+
await this._init;
41+
} catch {
42+
return {};
43+
}
4044

4145
const { data, error: _error } = await this.#itemSource.getItems(uniques);
4246

@@ -59,7 +63,11 @@ export class UmbItemRepositoryBase<ItemType extends { unique: string }>
5963
* @memberof UmbItemRepositoryBase
6064
*/
6165
async items(uniques: Array<string>) {
62-
await this._init;
66+
try {
67+
await this._init;
68+
} catch {
69+
return undefined;
70+
}
6371
return this._itemStore!.items(uniques);
6472
}
6573
}

src/Umbraco.Web.UI.Client/src/packages/core/repository/item/item-repository.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ export interface UmbItemRepository<ItemType> extends UmbApi {
88
error?: UmbProblemDetails | undefined;
99
asObservable?: () => Observable<Array<ItemType>>;
1010
}>;
11-
items: (uniques: string[]) => Promise<Observable<Array<ItemType>>>;
11+
items: (uniques: string[]) => Promise<Observable<Array<ItemType>> | undefined>;
1212
}

src/Umbraco.Web.UI.Client/src/packages/core/router/router-slot/router-slot.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ export class RouterSlot<D = any, P = any> extends HTMLElement implements IRouter
176176
* Tears down the element.
177177
*/
178178
override disconnectedCallback() {
179+
this._setParent(null);
180+
this._cancelNavigation?.();
179181
this.detachListeners();
180182
}
181183

@@ -413,6 +415,11 @@ export class RouterSlot<D = any, P = any> extends HTMLElement implements IRouter
413415
return cancel();
414416
}
415417
}
418+
// Check if the current route is still matching the browser URL.:
419+
if (!window.location.href.includes(this.constructAbsolutePath(''))) {
420+
// If the parent is active, we should not redirect.
421+
return cancel();
422+
}
416423
handleRedirect(this, route);
417424
return false;
418425
}

src/Umbraco.Web.UI.Client/src/packages/core/workspace/controllers/workspace-is-new-redirect.controller.ts

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ export const UmbWorkspaceIsNewRedirectControllerAlias = Symbol('IsNewRedirectCon
1616
* @param router
1717
*/
1818
export class UmbWorkspaceIsNewRedirectController extends UmbControllerBase {
19+
/**
20+
* TODO: Figure out why we need this timeout. [NL]
21+
* The problem this fixes it when save & publishing a document open in a modal, like from a collection.
22+
* The redirect triggers something that ends up re-setting the path, making the modal path the one in the browser despite the modal is closed.
23+
*/
24+
timeout: any | undefined;
25+
1926
constructor(
2027
host: UmbControllerHost,
2128
workspaceContext: UmbSubmittableWorkspaceContextBase<unknown>,
@@ -25,21 +32,45 @@ export class UmbWorkspaceIsNewRedirectController extends UmbControllerBase {
2532

2633
// Navigate to edit route when language is created:
2734
this.observe(workspaceContext.isNew, (isNew) => {
35+
if (this.timeout) {
36+
clearTimeout(this.timeout);
37+
}
2838
if (isNew === false) {
29-
const unique = workspaceContext.getUnique();
30-
if (router && unique) {
31-
const routerPath = router.absoluteRouterPath;
32-
if (routerPath) {
33-
const newPath: string = umbUrlPatternToString(ensurePathEndsWithSlash(routerPath) + 'edit/:id', {
34-
id: unique,
35-
});
36-
this.destroy();
37-
window.history.replaceState(null, '', newPath);
39+
this.timeout = setTimeout(() => {
40+
const unique = workspaceContext.getUnique();
41+
if (router && unique) {
42+
const routerPath = router.absoluteRouterPath;
43+
if (routerPath) {
44+
const newPath: string = umbUrlPatternToString(ensurePathEndsWithSlash(routerPath) + 'edit/:id', {
45+
id: unique,
46+
});
47+
this.destroy();
48+
// get current url:
49+
const currentUrl = window.location.href;
50+
if (
51+
router.localActiveViewPath === undefined ||
52+
router.localActiveViewPath === '' ||
53+
!currentUrl.includes(router.localActiveViewPath)
54+
) {
55+
return;
56+
}
57+
// Check that we are still part of the DOM and thereby relevant:
58+
window.history.replaceState(null, '', newPath);
59+
}
3860
}
39-
}
61+
this.timeout = undefined;
62+
}, 500);
4063
}
4164
});
4265

4366
// TODO: If workspace route changes cause of other reasons then this controller should be destroyed.
4467
}
68+
69+
override destroy() {
70+
super.destroy();
71+
if (this.timeout) {
72+
clearTimeout(this.timeout);
73+
this.timeout = undefined;
74+
}
75+
}
4576
}

src/Umbraco.Web.UI.Client/src/packages/core/workspace/entity-detail/entity-detail-workspace-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ export abstract class UmbEntityDetailWorkspaceContextBase<
373373
this.#entityContext.setUnique(data.unique);
374374
this._data.setPersisted(data);
375375
this._data.setCurrent(data);
376+
this.setIsNew(false);
376377

377378
const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
378379
if (!eventContext) throw new Error('Event context not found.');
@@ -381,7 +382,6 @@ export abstract class UmbEntityDetailWorkspaceContextBase<
381382
unique: parent.unique,
382383
});
383384
eventContext.dispatchEvent(event);
384-
this.setIsNew(false);
385385
}
386386

387387
protected async _update(currentData: DetailModelType) {

src/Umbraco.Web.UI.Client/src/packages/core/workspace/submittable/submittable-workspace-context-base.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ export abstract class UmbSubmittableWorkspaceContextBase<WorkspaceDataModelType>
162162
protected invalidSubmit(reason?: any): Promise<void> {
163163
return Promise.reject(reason);
164164
}
165+
166+
override destroy(): void {
167+
this.#isNew.destroy();
168+
this.routes.destroy();
169+
super.destroy();
170+
this.#submitPromise = undefined;
171+
this.#submitResolve = undefined;
172+
this.#submitReject = undefined;
173+
this.#validationContexts.forEach((context) => context.destroy());
174+
this.#validationContexts = [];
175+
}
165176
}
166177

167178
/*

0 commit comments

Comments
 (0)