reset state to be called when load/create

This commit is contained in:
Niels Lyngsø
2024-02-27 20:24:00 +01:00
parent e6644bf17f
commit 3b42c97eea
4 changed files with 16 additions and 5 deletions

View File

@@ -27,11 +27,15 @@ export abstract class UmbEditableWorkspaceContextBase<WorkspaceDataModelType>
});
}
protected resetState() {
this.#isNew.setValue(undefined);
}
getIsNew() {
return this.#isNew.getValue();
}
setIsNew(isNew: boolean) {
protected setIsNew(isNew: boolean) {
this.#isNew.setValue(isNew);
}

View File

@@ -4,7 +4,6 @@ import type { Observable } from '@umbraco-cms/backoffice/external/rxjs';
export interface UmbSaveableWorkspaceContextInterface extends UmbWorkspaceContextInterface {
isNew: Observable<boolean | undefined>;
getIsNew(): boolean | undefined;
setIsNew(value: boolean): void;
save(): Promise<void>;
setValidationErrors?(errorMap: any): void; // TODO: temp solution to bubble validation errors to the UI
destroy(): void;

View File

@@ -26,11 +26,11 @@ export class UmbWorkspaceIsNewRedirectController extends UmbBaseController {
workspaceContext.isNew,
(isNew) => {
if (isNew === false) {
const id = workspaceContext.getEntityId();
if (router && id) {
const unique = workspaceContext.getEntityId();
if (router && unique) {
const routerPath = router.absoluteRouterPath;
if (routerPath) {
const newPath = createRoutePathBuilder(ensurePathEndsWithSlash(routerPath) + 'edit/:id')({ id });
const newPath = createRoutePathBuilder(ensurePathEndsWithSlash(routerPath) + 'edit/:id')({ id: unique });
window.history.pushState({}, '', newPath);
this.destroy();

View File

@@ -80,6 +80,12 @@ export class UmbDocumentWorkspaceContext
this.loadLanguages();
}
resetState() {
super.resetState();
this.#persistedData.setValue(undefined);
this.#currentData.setValue(undefined);
}
async loadLanguages() {
// TODO: If we don't end up having a Global Context for languages, then we should at least change this into using a asObservable which should be returned from the repository. [Nl]
const { data } = await this.#languageRepository.requestCollection({});
@@ -87,6 +93,7 @@ export class UmbDocumentWorkspaceContext
}
async load(unique: string) {
this.resetState();
this.#getDataPromise = this.repository.requestByUnique(unique);
const { data } = await this.#getDataPromise;
if (!data) return undefined;
@@ -98,6 +105,7 @@ export class UmbDocumentWorkspaceContext
}
async create(parentUnique: string | null, documentTypeUnique: string) {
this.resetState();
this.#getDataPromise = this.repository.createScaffold(parentUnique, {
documentType: {
unique: documentTypeUnique,