diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/index.ts b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/index.ts index d6ffbc4792..57b04ad199 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/index.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/index.ts @@ -1,3 +1,2 @@ export * from './default-workspace.context.js'; -export * from './submittable-workspace-context-base.js'; export * from './tokens/index.js'; diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/submittable-workspace-context-base.ts b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/submittable-workspace-context-base.ts deleted file mode 100644 index 0b7224ab3c..0000000000 --- a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/submittable-workspace-context-base.ts +++ /dev/null @@ -1,160 +0,0 @@ -import { UmbWorkspaceRouteManager } from '../controllers/workspace-route-manager.controller.js'; -import { UMB_WORKSPACE_CONTEXT } from './tokens/workspace.context-token.js'; -import type { UmbSubmittableWorkspaceContext } from './tokens/submittable-workspace-context.interface.js'; -import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; -import { UmbContextBase } from '@umbraco-cms/backoffice/class-api'; -import { UmbBooleanState } from '@umbraco-cms/backoffice/observable-api'; -import type { UmbModalContext } from '@umbraco-cms/backoffice/modal'; -import { UMB_MODAL_CONTEXT } from '@umbraco-cms/backoffice/modal'; -import type { Observable } from '@umbraco-cms/backoffice/external/rxjs'; -import type { UmbValidationController } from '@umbraco-cms/backoffice/validation'; - -export abstract class UmbSubmittableWorkspaceContextBase - extends UmbContextBase> - implements UmbSubmittableWorkspaceContext -{ - public readonly workspaceAlias: string; - - // TODO: We could make a base type for workspace modal data, and use this here: As well as a base for the result, to make sure we always include the unique (instead of the object type) - public readonly modalContext?: UmbModalContext<{ preset: object }>; - - //public readonly validation = new UmbValidationContext(this); - #validationContexts: Array = []; - - /** - * Appends a validation context to the workspace. - * @param context - */ - addValidationContext(context: UmbValidationController) { - this.#validationContexts.push(context); - } - - #submitPromise: Promise | undefined; - #submitResolve: (() => void) | undefined; - #submitReject: (() => void) | undefined; - - abstract readonly unique: Observable; - - #isNew = new UmbBooleanState(undefined); - isNew = this.#isNew.asObservable(); - - readonly routes = new UmbWorkspaceRouteManager(this); - - /* - Concept notes: [NL] - Considerations are, if we bring a dirty state (observable) we need to maintain it all the time. - This might be too heavy process, so we might want to consider just having a get dirty state method. - */ - //#isDirty = new UmbBooleanState(undefined); - //isDirty = this.#isNew.asObservable(); - - constructor(host: UmbControllerHost, workspaceAlias: string) { - super(host, UMB_WORKSPACE_CONTEXT.toString()); - this.workspaceAlias = workspaceAlias; - // TODO: Consider if we can move this consumption to #resolveSubmit, just as a getContext, but it depends if others use the modalContext prop.. [NL] - this.consumeContext(UMB_MODAL_CONTEXT, (context) => { - (this.modalContext as UmbModalContext) = context; - }); - } - - protected resetState() { - //this.validation.reset(); - this.#validationContexts.forEach((context) => context.reset()); - this.#isNew.setValue(undefined); - } - - getIsNew() { - return this.#isNew.getValue(); - } - - protected setIsNew(isNew: boolean) { - this.#isNew.setValue(isNew); - } - - /** - * If a Workspace has multiple validation contexts, then this method can be overwritten to return the correct one. - * @returns Promise that resolves to void when the validation is complete. - */ - async validate(): Promise> { - //return this.validation.validate(); - return Promise.all(this.#validationContexts.map((context) => context.validate())); - } - - async requestSubmit(): Promise { - return this.validateAndSubmit( - () => this.submit(), - () => this.invalidSubmit(), - ); - } - - protected async validateAndSubmit(onValid: () => Promise, onInvalid: () => Promise): Promise { - if (this.#submitPromise) { - return this.#submitPromise; - } - this.#submitPromise = new Promise((resolve, reject) => { - this.#submitResolve = resolve; - this.#submitReject = reject; - }); - this.validate().then( - async () => { - onValid().then(this.#completeSubmit, this.#rejectSubmit); - }, - async () => { - onInvalid().then(this.#resolveSubmit, this.#rejectSubmit); - }, - ); - - return this.#submitPromise; - } - - #rejectSubmit = () => { - if (this.#submitPromise) { - // TODO: Capture the validation contexts messages on open, and then reset to them in this case. [NL] - - this.#submitReject?.(); - this.#submitPromise = undefined; - this.#submitResolve = undefined; - this.#submitReject = undefined; - } - }; - - #resolveSubmit = () => { - // Resolve the submit promise: - this.#submitResolve?.(); - this.#submitPromise = undefined; - this.#submitResolve = undefined; - this.#submitReject = undefined; - - // If we do not want to close a modal when saving something with errors, then move this part down to #completeSubmit method. [NL] - if (this.modalContext) { - this.modalContext?.setValue(this.getData()); - this.modalContext?.submit(); - } - }; - - #completeSubmit = () => { - this.#resolveSubmit(); - - // Calling reset on the validation context here. [NL] - // TODO: Capture the validation messages on open, and then reset to that. - //this.validation.reset(); - }; - - //abstract getIsDirty(): Promise; - abstract getUnique(): string | undefined; - abstract getEntityType(): string; - abstract getData(): WorkspaceDataModelType | undefined; - protected abstract submit(): Promise; - protected invalidSubmit(): Promise { - return Promise.reject(); - } -} - -/* - * @deprecated Use UmbSubmittableWorkspaceContextBase instead — Will be removed before RC. - * Rename `save` to `submit` and return a promise that resolves to true when save is complete. - * TODO: Delete before RC. - */ -export abstract class UmbEditableWorkspaceContextBase< - WorkspaceDataModelType, -> extends UmbSubmittableWorkspaceContextBase {} diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/data-manager/index.ts b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/data-manager/index.ts new file mode 100644 index 0000000000..ad3eea1ea7 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/data-manager/index.ts @@ -0,0 +1 @@ +export type * from './workspace-data-manager.interface.js'; diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/data-manager/workspace-data-manager.interface.ts b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/data-manager/workspace-data-manager.interface.ts new file mode 100644 index 0000000000..d9c497b44d --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/data-manager/workspace-data-manager.interface.ts @@ -0,0 +1,9 @@ +import type { UmbController } from '@umbraco-cms/backoffice/controller-api'; +import type { UmbEntityModel } from '@umbraco-cms/backoffice/entity'; + +export interface UmbWorkspaceDataManager extends UmbController { + getPersistedData(): ModelType | undefined; + getCurrentData(): ModelType | undefined; + setPersistedData(data: ModelType | undefined): void; + setCurrentData(data: ModelType | undefined): void; +}